我正在尝试将可正常运行的Selenium Web自动化测试转换为android自动化测试。但是,我在切换到新窗口时遇到问题。单击自动链接后有两个打开的窗口,但是在执行driver.switchTo()。window(currentHandle)之后,即使窗口句柄正在切换,我仍然会获得第一个窗口的URL。我的切换方法正确吗?
private WebDriver androidTest() {
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "My Phone");
caps.setCapability("udid", "521094ceea16c35f");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "6.0.1");
caps.setCapability("browserName", "Chrome");
caps.setCapability("appPackage", "com.android.chrome");
caps.setCapability("noReset", true);
WebDriver driver = null;
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
logger.debug("==== WebDriver Capabilities ====", new Object[0]);
logger.debug("Browser Name = {0}", new Object[] { caps.getBrowserName() });
logger.debug("Version = {0}", new Object[] { caps.getVersion() });
return driver;
}
public String switchToNewWindow(WebDriver driver){
String currentHandle=null;
System.out.println("windowH"+ driver.getWindowHandles());
for (String handle : driver.getWindowHandles()) {
System.out.println("url :" + driver.getCurrentUrl() + handle);
If(!handles.contains(handle)){
currentHandle = handle;
handles.push(currentHandle);
driver.switchTo().window(currentHandle);
System.out.println("url :" + driver.getCurrentUrl() + handle);
break;
}
}
return currentHandle;
}