最近半年来发现很多人在我之前发布的 webview 测试与微信小程序下面留言问问题,问的人太多了,我一直没精力回复,所以打算今天做一期公开课给大家科普下如何用 appium 测试 webview 与微信小程序。

课程内容

手机浏览器/webview/微信小程序自动化测试技术分享
主要的知识点

报名地址

https://ke.qq.com/course/263945

课程技术点预热

WebView 测试架构

分析 webview 进程的命令,用于问题定位和手工修复 chromedriver 的 ps 命令 bug

webview ()
{
    echo all webview socket;
    adb shell grep @webview /proc/net/unix;
    echo;
    echo pids with webview;
    pids=`adb shell grep @webview /proc/net/unix | awk -F_ '{print $NF}' `;
    adb shell ps $pids;
    echo;
    echo forward list with webview;
    adb forward --list | grep webview;
    echo;
    [ -n "$1" ] && pid=`adb shell ps | grep $1 | awk '{print $2}'`;
    echo $pid;
    echo pid=$pid match $1;
    [ -n "$2" ] && echo adb forward tcp:$2 localabstract:webview_devtools_remote_$pid && adb forward tcp:$2 localabstract:webview_devtools_remote_$pid
}

webview
webview appbrand0
webview appbrand0 $port

@Test
public void testBrowser() throws MalformedURLException {
    DesiredCapabilities caps = new DesiredCapabilities();

    caps.setCapability("deviceName", "seveniruby");
    caps.setCapability("browserName", "chrome");

    URL url = new URL("http://127.0.0.1:4723/wd/hub");
    AndroidDriver driver = new AndroidDriver(url, caps);

    driver.get("https://sogou.com");
    System.out.println(driver.getPageSource());
    driver.findElementByName("keyword").sendKeys("霍格沃兹测试学院");
    driver.findElementByXPath("//*[@id='searchform']//*[@type='submit']").click();

}

webview 测试


@Test
public void testWebView() throws MalformedURLException, InterruptedException {

    DesiredCapabilities caps = new DesiredCapabilities();

    caps.setCapability("deviceName", "emulator-5554");
    caps.setCapability("appPackage", "com.xueqiu.android");
    caps.setCapability("appActivity", ".view.WelcomeActivityAlias");
    caps.setCapability("chromedriverExecutableDir", "/Users/seveniruby/projects/chromedriver/2.20");


    URL url = new URL("http://127.0.0.1:4723/wd/hub");
    AndroidDriver driver = new AndroidDriver(url, caps);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //Thread.sleep(5000);

    if (driver.findElementsById("cancel").size() > 0) {
        driver.findElementById("cancel").click();
    }
    driver.findElementByXPath("//*[@text='交易']").click();

    System.out.println("等待webview上下文");
    for (int i = 0; i < 5; i++) {
        System.out.println(driver.getContextHandles());
        Thread.sleep(1000);
    }
    driver.getContextHandles().forEach(context -> {
        driver.context(context.toString());
        System.out.println("context=" + context);
        System.out.println(driver.getPageSource());
    });

}

小程序测试代码 demo,请使用 5.0.3 版本的 java-client,python 代码请自行对照编写。


@Test
public void testDianPing() throws IOException, InterruptedException {

    String processName = "com.tencent.mm:appbrand0";
    DesiredCapabilities caps = new DesiredCapabilities();

    caps.setCapability("deviceName", "dd");
    caps.setCapability("automationName", "uiautomator2");
    caps.setCapability("appPackage", "com.tencent.mm");
    caps.setCapability("appActivity", ".ui.LauncherUI");
    caps.setCapability("noReset", true);
    //caps.setCapability("dontStopAppOnReset", true);
    caps.setCapability("chromedriverExecutableDir", "/Users/seveniruby/projects/chromedriver/2.29");
    caps.setCapability("showChromedriverLog", true);
    //低版本的chromedriver在高版本的android上有个兼容性的bug,需要自己定制解决方案
    //caps.setCapability("adbPort", 5040);

    //todo: appium java client 6.0.0 有多个bug,待修复
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("androidProcess", processName);
    caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

    URL url = new URL("http://127.0.0.1:5723/wd/hub");
    AndroidDriver driver = new AndroidDriver(url, caps);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //todo: 6.0.*版本默认切换到webview
    Dimension size = driver.manage().window().getSize();
    Thread.sleep(5000);
    //滑动
    new TouchAction(driver)
            .press(
                    (int) (size.getWidth() * 0.5),
                    (int) (size.getHeight() * 0.3)
            )
            .waitAction(Duration.ofSeconds(2))
            .moveTo(

                    (int) (size.getWidth() * 0.5),
                    (int) (size.getHeight() * 0.9)
            )
            .release()
            .perform();


    System.out.println("点击小程序");
    driver.findElementByXPath("//*[contains(@text, '点评')]").click();

    System.out.println("打印上下文");
    for (int i = 0; i < 3; i++) {
        System.out.println(driver.getContextHandles());
        Thread.sleep(1000);
    }

    System.out.println("切换上下文到 " + "WEBVIEW_" + processName);
    driver.context("WEBVIEW_" + processName);
    System.out.println("打印上下文,寻找小程序界面");
    System.out.println(driver.getWindowHandles());
    for (String window : driver.getWindowHandles()) {
        driver.switchTo().window(window);
        System.out.println("windows=" + window);
        String source = driver.getPageSource();
        if (source.contains("<wx-view")) {
            System.out.println(source);
            break;
        }
    }
}


↙↙↙阅读原文可查看相关链接,并与作者交流