Appium之编写H5应用测试脚本(切换到Webview)

App使用H5编写,默认方式找不到元素。启动后获取所有上下文,找到webivew_xxxx的,然后进行切换。

源码:

 1 package MyAppium;
 2 
 3 import io.appium.java_client.android.AndroidDriver;
 4 import org.openqa.selenium.By;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.remote.DesiredCapabilities;
 7 
 8 import java.net.MalformedURLException;
 9 import java.net.URL;
10 import java.util.Set;
11 
12 /**
13  * @Auther:GongXingRui
14  * @Date:2018/11/1
15  * @Description: 测试App应用(H5)
16  **/
17 public class ZBuyAppTest {
18     public static void main(String[] args) throws MalformedURLException, InterruptedException {
19 
20         DesiredCapabilities capabilities = new DesiredCapabilities();
21         capabilities.setCapability("deviceName", "Android Sum");
22         capabilities.setCapability("automationName", "Appium");
23         capabilities.setCapability("platformName", "Android");
24         capabilities.setCapability("noReset", true);
25 //        安卓版本号,保持默认即可
26 //        capabilities.setCapability("platformVersion", "7.0");
27         //启动包名,使用SDK的uiautomatorviewer查看
28         capabilities.setCapability("appPackage", "cn.com.shopping.halmar");
29         capabilities.setCapability("appActivity", "com.weshare.jiekuan.activity.LoadActivity");
30 
31         AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
32         Thread.sleep(5000);
33 
34 //        获取所有窗口
35         Set<String> contexts = driver.getContextHandles();
36         System.out.println("所有窗口上下文:" + contexts);
37 
38 //        切换窗口到WEBVIEW
39         driver.context("WEBVIEW_cn.com.shopping.halmar");
40 
41         WebElement webElement = (WebElement) driver.findElements(By.className("_2G-P4GHfk8agQ8HDMa3Who")).get(1);
42         webElement.click();
43         Thread.sleep(3000);
44 
45         String result = driver.findElement(By.className("_3vza4fqJTJUBxJqQm687vB")).getText();
46         System.out.println("结果:" + result);
47 
48         driver.quit();
49     }
50 }

 

Appium之编写H5应用测试脚本(切换到Webview)

上一篇:Android 集成百度地图实现设备定位


下一篇:6.简单提取小红书app数据保存txt-2