如何使用 appium 在 android 中处理警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24635354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:32:13  来源:igfitidea点击:

how to handle alerts in android using appium

androidalertappium

提问by user3816956

How do I handle alerts in an Android web application using Appium server (1.0.1) and the Android SDK?

如何使用 Appium 服务器 (1.0.1) 和 Android SDK 在 Android Web 应用程序中处理警报?

The below code is not working on android:

以下代码不适用于android:

driver.switchTo().accept().alert();

Error message:

错误信息:

> -modal window does not get closed

回答by Jess

You need to get the Alert before you try and accept it

在尝试接受警报之前,您需要先获得警报

This is code from some of the Appium Java Client Tests:

这是来自一些Appium Java 客户端测试的代码

wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

This should work most of the time.

这应该在大部分时间都有效。

If accept()isn't working, replace the driver.switchTo().alert();and alert.accept();with code to find the button and then click it.

如果accept()不起作用,请将driver.switchTo().alert();和替换为alert.accept();代码以找到按钮,然后单击它。

If it's not finding the button wrap findElementBy(Method)code in a try/retry block, and thenclick on it.

如果它没有findElementBy(Method)在 try/retry 块中找到按钮包装代码,然后单击它。

回答by Rohan

The best way is to use the appium inspector. Click on the element and copy the resource-idfrom it. Use this resource id in findElement(By.id())method.

最好的方法是使用 appium 检查器。单击该元素并从中复制资源 ID。在findElement(By.id())方法中使用此资源 ID 。

For me resource-id: android:id/button1

对我来说资源 ID:android:id/button1

((AndroidDriver) driver).findElement(By.id("android:id/button1")).click();

This is for Android. For regular use you can use

这是针对安卓的。对于常规使用,您可以使用

driver.findElement(By.id("android:id/button1")).click();

回答by Ivan Pronin

Some alerts may be native Android's alerts, not generated by a browser. In this case the following code:

一些警报可能是原生 Android 的警报,不是由浏览器生成的。在这种情况下,以下代码:

Alert alert = driver.switchTo().alert(); alert.accept();

Alert alert = driver.switchTo().alert(); alert.accept();

may throw: WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"No JavaScript dialog to handle"}

可能抛出: WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"No JavaScript dialog to handle"}

To handle such alert, just switch to the native application context, make required actions, and then switch back to the browser:

要处理此类警报,只需切换到本机应用程序上下文,执行所需操作,然后切换回浏览器:

AppiumDriver<WebElement> appiumDriver = (AppiumDriver<WebElement>) webDriver;
String currentContext = appiumDriver.getContext();
appiumDriver.context("NATIVE_APP");

// actions within the alert
appiumDriver.findElements(By.xpath(OK_BUTTON_LOCATOR)).click(); // put locator instead of OK_BUTTON_LOCATOR
appiumDriver.context(currentContext);

// continue working

回答by mahesh deshmukh

WebElement btn = driver.findElement(By.xpath("//android.widget.Button[@content-desc='OK']"));
TouchAction act = new TouchAction(driver);
act.tap(241,320).perform();

(241,320) these are X & Y c ordinates of alert This work perfectly for me

(241,320) 这些是警报的 X 和 Y c 坐标这对我来说非常有用

回答by karthick23

Appium comes with a default capability to accept, dismiss alerts

Appium 具有接受、解除警报的默认功能

capabilities.SetCapability("autoAcceptAlerts", true);
capabilities.SetCapability("autoDismissAlerts", true);

回答by aashishpandey

Please use the below code, Add some wait before clicking on OK Button. After that pass the xpath of you OK Button.

请使用以下代码,在单击“确定”按钮之前添加一些等待。之后通过你的确定按钮的xpath。

synchronized (driver)
{
driver.wait(2000);
}
driver.context(NATIVE_APP);
driver.findElementByXPath{("//android.widget.Button[@resourceid=
‘android:id/button1']").click();

回答by aashishpandey

If the alert display on Ui , taking more time to display when we need to wait ..we can use fluent wait instead of this..

如果警告显示在 Ui 上,当我们需要等待时需要更多时间来显示..我们可以使用流畅的等待而不是这个..

回答by Droid Chris

So an updated answer on this is this: an AlertDialog is a system level element, so clicking on accept button you should use:

因此,对此的更新答案是: AlertDialog 是系统级元素,因此单击接受按钮应该使用:

androidDriver.findElementById("android:id/button1").click()

else for cancel do this:

否则取消执行此操作:

androidDriver.findElementById("android:id/button2").click()