首页 > 解决方案 > 使用 appium 从任何 android 设备的通知栏中清除所有通知

问题描述

如何在任何 android 设备中使用 appium 从 android 通知栏中清除所有通知。

我试过了

    MobileElement clearallnotification=null;
    driver.openNotifications();
    try {
        clearallnotification= driver.findElement(By.xpath("//android.widget.ImageView[contains(@content-desc, 'Clear all notifications')]"));
        clearallnotification.click();
    }catch(ElementNotFoundException e) {
        clearallnotification= driver.findElementById("com.android.systemui:id/delete");
        clearallnotification.click();
    }
    driver.pressKeyCode(AndroidKeyCode.BACK);

但它仅适用于特定设备如何使其适用于所有类型的设备?

标签: javaandroidseleniumappium

解决方案


下面的代码将做到这一点。它对我有用。

从 appium 导入 webdriver 从 appium.webdriver.common.touch_action 导入 TouchAction 从时间导入睡眠导入 pytest

class TestScrollAndroid:“针对 Chess Free 应用程序运行测试的类”

@pytest.fixture(scope='function')
def driver(self, request):
    "Setup for the test"
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.1.0'
    desired_caps['deviceName'] = 'Realme 2 Pro'
    desired_caps['noReset'] = True
    desired_caps['udid'] = '2032609e'
    desired_caps['allowTestPackages'] = True
    # Returns abs path relative to this file and not cwd
    desired_caps['app'] = "D:\\Chess Free.apk"
    desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
    desired_caps['appActivity'] = '.ChessFreeActivity'
    #desired_caps['autoWebview'] = True

    #calling_request = request._pyfuncitem.name
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    driver.implicitly_wait(10)

    def tearDown():
        print("Tear down the test")
        driver.quit()

    request.addfinalizer(tearDown)
    return driver

def test_scroll_notificaion(self,driver):
    driver.open_notifications()
def test_clear_all_notification(self, driver):
    driver.open_notifications()
    if(driver.find_elements_by_id("com.android.systemui:id/clear_all_button"))
        element = driver.find_element_by_id("com.android.systemui:id/clear_all_button")
        element.click()
    assert(not driver.find_elements_by_id("android:id/title"))

推荐阅读