如何在AndroidElement和WebElement之间切换?
问题描述:
我正在开发移动Web应用程序自动化。在这里我使用Java,Selenium和Appium。有两个字段我不能自动化。他们是日期和下拉字段。我可以自动化文本字段,复选框和单选按钮。当点击日期字段时,会出现Android默认日期选择器,我无法选择日期。这里是我下面的代码:如何在AndroidElement和WebElement之间切换?
class openBrowser() {
public static WebDriver driver;
public static AppiumDriver<MobileElement> androidDriver;
@Test
public static void launchBrowser(){
desiredCapabalities(...);
androidDriver = new AndroidDriver<MobileElement>(new
URL("http://localhost:4723/wd/hub", desiredCapabilities));
driver = androidDriver;
}
class pickDate() {
MobileElement element;
try{
element = (MobileElement)
androidDriver.findElementByXPath("//android.view.View[@content-desc='28 May
2017']").click();
}catch(Exception e) {
throw e;
}
}
“findElementByXPath()”仅查找网络元素,而不是寻找Android设备/移动元素。请参考日期选择器屏幕截图:Date picker
请向我建议在Web元素和Android/Mobile元素之间切换的任何解决方案。提前致谢。
答
我发现从以下链接的解决方案:
switch contexts i.e Native App and Web View
public static void switchToContext(String context) throws Exception {
try {
RemoteExecuteMethod executeMethod = new RemoteExecuteMethod((RemoteWebDriver) androidDriver);
Map<String,String> params = new HashMap<>();
params.put("name", context);
executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getCurrentContextHandle() throws Exception{
try{
RemoteExecuteMethod executeMethod = new RemoteExecuteMethod((RemoteWebDriver) androidDriver);
String context = (String) executeMethod.execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE, null);
return context;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static List<String> getContextHandles() throws Exception{
try {
RemoteExecuteMethod executeMethod = new RemoteExecuteMethod((RemoteWebDriver) androidDriver);
List<String> contexts = (List<String>) executeMethod.execute(DriverCommand.GET_CONTEXT_HANDLES, null);
return contexts;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
能否请您粘贴堆栈跟踪? – sjmach
因为,我解决了,dint拿走堆栈跟踪。但它声明'NoSuchElementException'以及驱动程序的细节和功能。 – Bala