appium 3-4-1034等待、日志、性能数据、xpath定位、web driver协议

1.等待

1.1精确等待

sleep 不推荐

    @Test
    public void testWait1() throws InterruptedException{
        day_time();
        Thread.sleep(10000);
        day_time();
        WebElement e1 = driver.findElementById("com.xueqiu.android:id/user_profile_icon");
        System.out.println(e1.getTagName()+"\t"+e1.getLocation());
        e1.click();
    }
## 1.2全局隐式等待 一段时间内,会不断的查看当前页面是否有某个控件。存在就返回;否则就一直寻找,直到超时。默认值是0,即不等待 ```#java driver.manager().timeouts().implicitlyWait(10,TimeUnit.SECONDS); ``` ```#java @Test public void testWait2(){ driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); day_time(); driver.findElementById("com.xueqiu.android:id/user_profile_icon").click(); day_time(); } ``` 问题:隐式等待设置无效,没有继续寻找

1.3显式等待,针对单个元素

    WebDriverWait wait = new WebDriverWait(driver,10);
    WebElement element = wait.until(ExpectedConditions.elementToBe(By.id("someid"));</font>
    @Test
    public void testWait3(){
        WebDriverWait wait = new WebDriverWait(driver,10);
        day_time();
        WebElement e3 = wait.until(ExpectedConditions.presenceOfElementLocated( By.id("com.xueqiu.android:id/user_profile_icon")));
        day_time();
        System.out.println(e3.getTagName()+"\t"+e3.getLocation());
        e3.click();
    }
# 2.log ```#java driver.manage().logs().getAvailableLogTypes(); driver.manage().logs().get("logcat").getAll().toString() ``` ```#java public void testLog()throws InterruptedException { Thread.sleep(8000); driver.rotate(ScreenOrientation.LANDSCAPE); System.out.println(driver.manage().logs().getAvailableLogTypes()); } ```
    public void testLog()throws InterruptedException {
        Thread.sleep(8000);
        driver.rotate(ScreenOrientation.LANDSCAPE);
        System.out.println(driver.manage().logs().get("logcat").getAll().toString());
    }
```#java public void testLog()throws InterruptedException { Thread.sleep(8000); driver.rotate(ScreenOrientation.LANDSCAPE); for(Object l:driver.manage().logs().get("logcat").getAll().toArray()){ System.out.println(l); } } ``` # 3.手机性能数据 python目前不支持 ```#java public void testPerformace() throws Exception{ System.out.println(driver.getPerformanceData("com.xueqiu.android", "cpuinfo", 10)); WebDriverWait wait = new WebDriverWait(driver,10); WebElement e3 = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("com.xueqiu.android:id/user_profile_icon"))); System.out.println(driver.getSupportedPerformanceDataTypes());
    System.out.println(driver.getPerformanceData("com.xueqiu.android", "memoryinfo", 10));
    System.out.println(driver.getPerformanceData("com.xueqiu.android", "batteryinfo", 10));
    System.out.println(driver.getPerformanceData("com.xueqiu.android", "networkinfo", 10));
    Thread.sleep(3000);
}
<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181010101234618-100093198.png" width="600" />

#    4.xpath
    appium定位只支持3种,id,accessibility id,xpath。其中uiautomator2 accessibility id只针对content description。如果开发对与id的赋值不固定或简陋,每个包的id不一样,accessibility id也不一定存在。此时xpath就相当重要。
##    4.1.绝对定位不推荐 
<font  color=458B00>/xxx/ddd/dddd</font>
##    4.2.相对定位
* //*    获取所有控件
* //*[contains(@resource-id,'login')]
* //*[@text='登陆']
* //*[contains(@text,'登陆') and contains(@resource-id), 'login']
* //*[contains(@text, '登陆') or contains(@label,'登陆')] 获取text为登陆的控件
* //*[contains(@text, '看点')]/ancestor::*//*[contains(name(),'EditText')]    从看点的父类下面去寻找classname为EditText的控件
* //*[clickable="true"]//android.widget.TextView[string-length(@text)>0 and string-length(@text)<20]    所有元素下类名为android.widget.TextView且text长度在0-20之间的控件

##    4.3Android和iOS的属性对应,待补充

|android|ios|
----------|----------
tag class|tag
resourceId|null
content-desc|null
text|label
 
##    4.4.xpath定位验证
* 使用appium的inspector进行查找
* 将pagesource复制到https://www.freeformatter.com/xpath-tester.html/#ad-output网址去查找

#    5.webdriver 协议与手工模拟
##    5.1 .webdriver协议基础知识
##    5.2.session_id获取   
* session_id=`curl http://127.0.0.1:4723/wd/hub/sessions|awk -F \" '{print $8}'`

##    5.3.element id获取
 curl -X POST http://127.0.0.1:4723/wd/hub/session/$session_id/elements --data-binary '{"using":"xpath","value":"//*[@class=\"android.widget.Toast\"]"}' -H "Content-Type:application/json;charset=UTF-8"
* 元素属性获取
curl http://127.0.0.1:4723/wd/hub/session/${session_id}/element/${element-id}/attribute/text
* 元素动作
curl -X POST http://127.0.0.1:4723/wd/hub/session/${session_id}/element/${$element_id}/click

#    FAQ 
##    1.如1.2全局隐式等待中代码,运行无效果,没有持续查找元素
##    2.执行System.out.println(driver.getPerformanceData("com.xueqiu.android", "cpuinfo", 10))时,appium报错如下
<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181010095358396-1532949485.png" width="600" />
原因:1.应用信息更新不及 2.执行命令时该应用已停止
<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181010095953538-270790384.png" width="600" />
解决:将cpuinfo方法提前或增加等待时间防止应用被杀死

##    3.全局隐私等待无效果
posted on 2018-10-07 10:53  singleSpace  阅读(563)  评论(0编辑  收藏  举报