How to get context and switch to webview context

Viewed 651

I'm testing a Windows hybrid application, I'm using the codes below to get contexts but I got an UnsupportedCommandException when I launch it.

Here's my code :

public void initialize() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Windows");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Max");
    capabilities.setCapability("app", "D:\\Users\\Max\\Desktop\\TI\\ti.exe");
    driver = new AppiumDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
    Set<String> contextNames = driver.getContextHandles();
    for (String contextName : contextNames) {
        System.out.println(contextName);
    }
    driver.context((String) contextNames.toArray()[1]);
}

Here's the output on the Appium Server: Appium server screen

It says that the command contexts is not recognized. I'm using :

  • Appium Desktop v1.20.2
  • Appium Java client v7.5.1
  • WinAppDriver v1.2.1
1 Answers

The first of all, you need to get all contexts. More info in official docs

Here how I implemented it in tests.

  1. In some Page Object files I keep Android contexts.

     ANDROID_CONTEXTS = {
         'webview': 'WEBVIEW_ru.myapp',
         'native': 'NATIVE_APP'
     }
    
  2. In some tests I switch context from Native to Webview.

     @allure.link(url='https://jira.project.tech/browse/TEST-1', name='TEST-1 - Fill payment form')
     @allure.title('Fill payment form')
     def test_card_standard_1_month(appdriver):
    
         Paywall(appdriver).press_one_month_button()
         PaymentsPage(appdriver).select_card_payment()
         PaymentsPage(appdriver).press_payment_button()
    
    
         appdriver.switch_to.context(ANDROID_CONTEXTS['webview'])
         Payform(appdriver).fill_and_submit_payform()
    
         appdriver.switch_to.context(ANDROID_CONTEXTS['native'])
    
         assert SuccessPayment(appdriver).get_successful_payment_title_text() == 'Your subscription is successful!'
    

You can check all available contexts in your app. More info here -> http://appium.io/docs/en/commands/context/set-context/

Related