我不熟悉通过appium Java进行iOS自动化测试。我编写此代码以包含谓词文本。由于某种原因,xpath可以正常工作,但是当我使用谓词文本时,没有得到这样的元素异常
我使用Eclipse,maven appium 1.7.2,并且我使用ioS模拟器
我的appium服务器是最新版本
protected IOSDriver<IOSElement> driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
@BeforeMethod
public void setUp() throws MalformedURLException {
dc.setCapability("deviceName", "iPhone X");
dc.setCapability("platformName", "iOS");
dc.setCapability("app", "xxx.app";
dc.setCapability("platformVersion", "12.2");
dc.setCapability("automationName", "XCUITest");
dc.setCapability("useNewWDA", true);
dc.setCapability("fullReset", false);
dc.setCapability("appiumVersion", "1.7.2");
//dc.setCapability("automationName", "XCUITest");
driver = new iOSDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
driver.setLogLevel(Level.INFO);
}
@Test
public void testUntitled() throws InterruptedException {
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Allow']")).click();
Thread.sleep(10000);
String selector= "type == 'XCUIElementTypeTextField' AND value ==
'Email'";
driver.findElement(MobileBy.iOSNsPredicateString(selector)).sendKeys("gjghhj"); driver.findElement(By.xpath("//XCUIElementTypeTextField[@value='Email']")).sendKeys("dssss");
}
我希望它能够通过iOS谓词文本检测到电子邮件字段
我正在获得异常请求帮助以下
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
*** Element info: {Using=-ios predicate string, value=type == 'XCUIElementTypeTextField' AND value == 'Email'}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.MobileBy.findElement(MobileBy.java:61)
at io.appium.java_client.MobileBy$ByIosNsPredicate.findElement(MobileBy.java:458)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:57)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at com.gtl.probatio.tests.iosLeap.testUntitled(iosLeap.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
您的谓词可能不正确。考虑使用name
代替value
String selector= "type == 'XCUIElementTypeTextField' AND name == 'Email'";
P.S。当您不知道确切的等待时间时,请勿使用Thread.sleep();
。试试这个实用程序功能:
public void waitForExistence(final String predicate) {
for (int attempt = 0; attempt < 10; attempt++) {
try {
driver.findElementByIosNsPredicate(predicate);
break;
} catch (Exception e) {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
}
}