Search code examples
javaappium

Switching Context in amazon app in Appium


I am automating Amazon app through appium. In product description page it uses webview. But when using getContextHandles() in appium it only returns NATIVE_APP as context. Even switching directly by using driver.context("WEBVIEW_1") throws exception saying no such context present.

How to switch to webview in amazon app.

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();
        }
    }
}

Solution

  • For hybrid application, you will need a debug application to run automated tests using appium. If you use the release build for hybrid app, you will be able to automate native context. But to run the non native content you will need a debug app.

    Native app : Debug app not needed.

    Hybrid app : Debug app is needed to detect web elements, native content can be used both ways.