Hybrid App WebView Automated Testing

 Everyone has read the configuration from the beginning. Your problem may not be in the webview configuration. It may appear in the driver. Another important point is that the mobile phone does not need to download Google Chrome. It only needs to configure the chromedriver driver version of appium and Android System WebView. As long as the version is the same (there are many cases on the Internet saying that Google Chrome needs to be installed, if the personal test fails, an error will be reported:

io.appium.java_client.NoSuchContextException: An unknown server-side error occurred while processing the command. Original error:

Hybrid and native apps

What is a hybrid app? In fact, this is self-evident. Normally, our apps should be native, but in actual work, they are not. Anyway, for various reasons, our apps will be native and there will be H5 in the middle. Page, this is actually what we often call hybrid. Of course, there is also pure H5, but we won’t explain too much here.

WebView element positioning tool

  • Use the driver.page_source method to write the obtained page content into an html file, then use a browser to open the html file, and use the F12 debugging tool for element positioning (code level)
  • For the Developer Tools, the debugging tool that comes with the chrome browser, enter: chrome://inspect#devices. This tool needs to access foreign websites, so it needs to bypass the wall.
  • To use UC developer debugging tools, you need to install them in advance and do not need to circumvent the firewall (Tool: UC-devtools) Official website address: https://dev.ucweb.com/docs/pwa/docs-zh/xy3whu

 It is recommended that you use UC-devtools. After downloading it from the official website, you can use it with simple settings. When using it, you need to connect your phone with USB via USB ; check it on the Home page.

 Preparation

 The first step is to open the phone settings and find application management.

This is the domestic chromedriver driver download address: http://npm.taobao.org/mirrors/chromedriver/

Download the corresponding driver according to the version number obtained in the mobile application management settings. The version number in the example here is (79.0.3945) and the corresponding driver is as shown below:

Click to download the compressed package as follows:

 Unzip the downloaded compressed package to a custom file directory. There is no need to replace the chromedriver driver that comes with appium; ( remember that the driver directory you downloaded is only for configuration later);

 Java appium webview automated test configuration

 Without further ado, let’s get straight to the code:

/**
 * @Author HLY
 * @Create 2020-03-23 17:41
 */
public class TTest {
    
    AndroidDriver driver;

    @Before
    public void befor(){
            DesiredCapabilities capabilities = new DesiredCapabilities();
            //appium的版本
            capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "v1.8.1");
            //android系统的版本
            capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "android_version_xx");
            //系统
            capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
            //驱动
            capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");//UiAutomator1和UiAutomator2使用区别在于版本限制,V1.14.0以上的版本建议使用UiAutomator1

            capabilities.setCapability(MobileCapabilityType.UDID, "deviceName_xx");
            //设备名称 模拟器名称
            capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "deviceName_xx");
            //capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
            //设置app的包名
            capabilities.setCapability("appPackage", "com.xx.xx");
            //设置app的启动activity
            capabilities.setCapability("appActivity", ".xxActivity");
            //capabilities.setCapability("newCommandTimeout", 2000);x
            //此处的地址是appium中的host地址,可以自己修改,只要与appium对应就可以
            capabilities.setCapability("noReset", true);
            //支持中文输入
            capabilities.setCapability("unicodeKeyboard", true);
            //重置输入法到原有状态
            capabilities.setCapability("resetKeyboard", true);
            //设置你下在的 chromedriver驱动路径,路径只写到ChromeDriver.exe的上层目录
            capabilities.setCapability("chromedriverExecutableDir","D:\\xx\\ChromeDriver\\chromedriver_win79.0.3945.36");
        try {
            this.driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1: 4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        TouchAction touchAction =new TouchAction(driver);
    }

    /**
     * @throws InterruptedException
     */
    @Test
    public void test() throws InterruptedException, IOException {
        //TODO 你的原生app自动化逻辑
        ...
        ...
        ...
        //TODO 切换到 webview 前提条件是当前页面必须是webview
        //这里才会获取到上下文呢信息,
        Set<String> contextNames = driver.getContextHandles();
        for (String contextName : contextNames) {
            System.out.println(contextName); //prints out something like NATIVE_APP \n WEBVIEW_1
        }
        //WebDriver webDriver = driver.switchTo().window((String) contextNames.toArray()[1]);
        driver.context((String) contextNames.toArray()[1]);// set context to WEBVIEW_1
        driver.findElement(By.xpath("//*[@id=\"btn\"]")).click();
        Thread.sleep(10000);
        //切换回原生
        driver.context((String) contextNames.toArray()[0]);
        System.out.println("WebView自动化测试完成");
    }

    @After
    public void after(){
        driver.quit();
    }
}

You also need to start the appium service. You can execute the use case here. It is effective in personal testing. If you don’t understand, you can leave a message or send a private message. I will reply to everyone as soon as I see it.

Guess you like

Origin blog.csdn.net/paroleg/article/details/107878240