我们开发了一个应用程序,我们希望使用Appium和TestNG配置自动化测试。该应用程序包含本机页面和网页,但标识,其中包含徽标,汉堡包(在主页中)和后退按钮始终是原生的。
我从Chrome Inspector获取了网络资源ID,但命令:
WebElement usernameEditText = driver.findElement(By.id("username"));
或
WebElement login = driver.findElement(By.xpath("//input[@id='login-ico']"));
因此错误而失败:
org.openqa.selenium.NoSuchElementException: An element could not be
located on the page using the given search parameters. (WARNING: The
server did not provide any stacktrace information)
Command duration or timeout: 309 milliseconds
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision:
'61506a4624b13675f24581e453592342b7485d71', time: '2015-06-04 10:22:50'
System info: host: 'stefano', ip: '127.0.0.1', os.name: 'Mac OS X',
os.arch: 'x86_64', os.version: '10.11.4', java.version: '1.7.0_79'
*** Element info: {Using=xpath, value=//input[@id='login-ico']}
Session ID: 18098de4-ee19-42b5-9f91-30f6f7e01552
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{automationName=Appium, platform=LINUX,
javascriptEnabled=true,
appActivity=com.UnipolSaiApp.activity.MainActivity,
networkConnectionEnabled=true, desired={automationName=Appium,
platformVersion=5.0.1, platformName=Android, deviceName=Android,
appActivity=com.UnipolSaiApp.activity.MainActivity, device=Android,
appPackage=com.UnipolSaiApp}, locationContextEnabled=false,
appPackage=com.UnipolSaiApp, platformVersion=5.0.1,
databaseEnabled=false, deviceName=9b984ca9, platformName=Android,
webStorageEnabled=false, device=Android, warnings={},
takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:43)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:455)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:129)
at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:51)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at pages.LoginPage.login_success(LoginPage.java:25)
at scenarios.AppiumTest.loginTest(AppiumTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1178)
at org.testng.TestRunner.privateRun(TestRunner.java:757)
at org.testng.TestRunner.run(TestRunner.java:608)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
at org.testng.TestNG.run(TestNG.java:999)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
相反,检索本机元素会成功。
这是我的AndroidSetup的代码:
public class AndroidSetup {
protected AndroidDriver driver;
protected void prepareAndroidForAppium() throws MalformedURLException {
File appDir = new File("/path_of_apk");
File app = new File(appDir,"*****.apk");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("deviceName","Android");
dc.setCapability("platformVersion","5.0.1");
dc.setCapability("platformName","Android");
//dc.setCapability(CapabilityType.BROWSER_NAME, "browser");
dc.setCapability("device","Android");
dc.setCapability("appPackage","com.*******");
dc.setCapability("appActivity","com.*******.MainActivity");
//other caps
//dc.setCapability("app",app.getAbsolutePath());
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);
Set<String> contexts = driver.getContextHandles();
for (String ctx : contexts) {
System.out.println(ctx);
}
}
}
AppiumTest:
public class AppiumTest extends AndroidSetup {
@BeforeClass
public void setUp() throws Exception {
prepareAndroidForAppium();
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void loginTest() throws InterruptedException
{
new LoginPage(driver).login_success();
}
}
这里是BaePage类:
public class BasePage {
protected WebDriver driver;
String app_package_name = "com.****:id/";
public BasePage(WebDriver driver) {
this.driver = driver;
}
protected void waitForVisibilityOf(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
protected void waitForClickabilityOf(By locator) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(locator));
}
}
这里是LoginPage:
public class LoginPage extends BasePage {
public LoginPage(WebDriver driver) {
super(driver);
}
public LoginPage login_success() throws InterruptedException {
Thread.sleep(30000);
WebElement login = driver.findElement(By.xpath("//input[@id='login-ico']"));
login.click();
WebElement usernameEditText = driver.findElement(By.id("usernameET"));
usernameEditText.sendKeys("****");
WebElement passwordEditText = driver.findElement(By.id("passwordET"));
passwordEditText.sendKeys("*****");
WebElement loginButton = driver.findElement(By.id("submitBtn"));
loginButton.click();
return new LoginPage(driver);
}
}
以下代码:
Set<String> contexts = driver.getContextHandles();
for (String ctx : contexts) {
System.out.println(ctx);
}
只返回NATIVE_APP条目。
你能解释一下我的错吗?这是关于Appium配置的错误吗?