appium操作微信公众号H5 web页面

安卓微信公众号的H5页面是webview,一般操作需要切换context。

在执行如下步骤,就能直接像识别native样识别webview

 

  1、代码追加:

        ChromeOptions options = new ChromeOptions();
              options.setExperimentalOption("androidProcess", "com.tencent.mm:tools");
              capabilities.setCapability(ChromeOptions.CAPABILITY, options);

  2、在微信任何对话框 发送 "//deletetbs"

appium操作微信H5

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import com.fc.htgl.utils.ConfigUtil;

import io.appium.java_client.android.AndroidDriver;


public class TestWechat {
    public AndroidDriver<WebElement> driver;
    public WebDriverWait wait = null;
    public void apkStart(String udid, String packageName, String activityName, String appiumServer) {

        DesiredCapabilities capabilities = new DesiredCapabilities();        
        capabilities.setCapability("deviceName", udid);
        capabilities.setCapability("udid", udid);
        // 设置每次运行不重新启动被测试app
        capabilities.setCapability("noReset", "true");
        capabilities.setCapability("fullReset", "false");
        // 设置安卓系统版本
        capabilities.setCapability("platformVersion", "6.0");
        capabilities.setCapability("platformName", "Android");
        // session 延迟时间
        capabilities.setCapability("newCommandTimeout", 24000);
        capabilities.setCapability("device", "selendroid");
        // capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("unicodeKeyboard", "True");
        capabilities.setCapability("resetKeyboard", "True");
        // 设置app的主包名和主类名 微信程序包名和activity 名
        capabilities.setCapability("appPackage", packageName);
        capabilities.setCapability("appActivity", activityName);

        ChromeOptions options = new ChromeOptions();
              options.setExperimentalOption("androidProcess", "com.tencent.mm:tools");
              capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        
        // 启动
        try {
            System.out.println("http://" + ConfigUtil.getBoxApkAppiumServer() + "/wd/hub");
            driver = new AndroidDriver<WebElement>(new URL("http://" + appiumServer + "/wd/hub"), capabilities);
            wait = new WebDriverWait(driver, 10);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    @Test
    public void test(){
        this.apkStart("G6N7MBIZJ7YHLZO7", "com.tencent.mm", "com.tencent.mm.ui.LauncherUI", "127.0.0.1:4723");
        //点击公众号
         driver.findElementByAndroidUIAutomator("text(\"sdfsdfsd\")").click();
        //进入公众号的H5页面
        driver.findElementByAndroidUIAutomator("text(\"sssss\")").click();
        driver.findElementByAndroidUIAutomator("text(\"sfdfdfdf\")").click();
        //微信h5 操作
        driver.findElementByXPath("//android.widget.EditText[@content-desc='添加备注']").sendKeys("abababab");;
        driver.findElementByXPath("//android.view.View[@content-desc='sdfsdfsdfsdf']").click();

        Set<String> context = driver.getContextHandles();
        for (String contextName : context) {
            System.out.println(contextName);

        }
    }
    

}

 

posted @ 2017-06-13 17:20  testway  阅读(1877)  评论(0编辑  收藏  举报