我正在通过appium自动化Amazon应用程序。在产品说明页面中,它使用Webview。 但是在appium中使用getContextHandles()时,它仅返回NATIVE_APP作为上下文。 甚至直接使用driver.context(“ WEBVIEW_1”)进行切换也会引发异常,表明不存在此类上下文。
如何在Amazon应用中切换到Webview。
public class AmazonTest {
AndroidDriver<MobileElement> driver;
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("VERSION", "9");
capabilities.setCapability("deviceName","Mi A2");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
//Url where appium server is running
URL url = new URL("http://0.0.0.0:4723/wd/hub");
driver = new AndroidDriver<>(url, capabilities);
}
public void amazonTest() throws InterruptedException {
//Skip sign in
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement skipSignInButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/skip_sign_in_button"));
skipSignInButton.click();
//Search for IPhone
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement searchTextElement = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/rs_search_src_text"));
searchTextElement.sendKeys("iPhone X"+"\n");
//click on first result
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement firstResult = (MobileElement) driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Apple iPhone X (64GB) - Space Grey')]"));
firstResult.click();
//Click on Enter Pincode button
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement pincodeButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_gps_enter_pincode"));
pincodeButton.click();
//Enter pincode
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement pinTextBox = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_pin_code_text_pt1"));
pinTextBox.sendKeys("248002");
MobileElement applyButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_update_pin_code"));
applyButton.click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Swipe up to show the buy now button
TouchAction ta = new TouchAction(driver);
ta.press(PointOption.point(540, 1420)).moveTo(PointOption.point(540, 200)).release().perform();
Set<String> con = driver.getContextHandles(); //Get all context associated with this app
for(String s : con) { //only showing native app
System.out.println(s);
}
}
//close the application
public void teardown(){
driver.quit();
}
public static void main(String args[]) {
AmazonTest d = new AmazonTest();
//simulating TestNg Flow
try {
d.setUp();
d.amazonTest();
d.teardown();
}catch(Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
对于混合应用程序,您将需要一个调试应用程序以使用appium运行自动化测试。如果将发布版本用于混合应用程序,则将能够自动执行本机上下文。但是要运行非本机内容,您将需要一个调试应用程序。
本地应用:无需调试应用。
混合应用程序:需要使用调试应用程序来检测Web元素,两种方式都可以使用本机内容。