场景说明:APP中上部通常都会有动态标签,当点击最后一个标签,标签会有总体左移的效果, 左移后,页面的最右侧标签不必定是最后一个,须要判断,若是不是最后一个, 继续点击一次,若是是,则说明 标签总体左移功能正常。 前端
吐槽点:使用xpath的定位方式是否是很low,稍后和前端商量下,看看能不能帮忙加个ID等。 java
吐槽点2:get_text()方法中我注释了几个xpath,主要是由于页面上使用相对地址地位元素时, 发现多个元素的xpath是如出一辙的,故将元素的fullxpath复制以后, 观察xpath路径规律,截取对应的xpath,就不会报错了! android
更新点:以前有人问过我,为何标签没有ID,定位不到,该怎么点击? 然而针对我下面的代码,我就很是须要内部文本层的ID了,不然使用xpath会致使代码执行效率很低。 一直不多和前端打太多交道,今天拜托前端在各个标签的文本层上都加个ID,前端人仍是不错的, 有了ID,相信代码书写就能更简单,执行也能更快了! this
package com.jiubei.homepage; import org.testng.annotations.Test; import com.jiubei.baseinterface.SleepOwn; import com.jiubei.testng.Init; import java.net.MalformedURLException; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; public class Homepage extends Init implements SleepOwn{ public int labs_id=2; @Test(enabled=false) //进入活动消息页面 public void home_page_f() throws MalformedURLException { sleep_own(2); driver.findElement(By.className("android.widget.ImageView")).click(); sleep_own(2); String message_title=driver.findElement(By.id("com.jiubei.shop:id/tv_center_text")).getText(); System.out.println(message_title); sleep_own(2); } @Test(enabled=false)//退出活动消息并返回首页 public void action_message_f(){ try { sleep_own(1); driver.findElement(By.id("com.jiubei.shop:id/tv_left_text")).click(); } catch (NoSuchElementException e) { action_message_f(); } } @Test//点击至最后一个标签 public void switch_end_class_f(){ sleep_own(6); System.out.println("begin"); for(labs_id=this.labs_id;labs_id<6;labs_id++){ if(labs_id==5){ String textx=get_text(); System.out.println(textx); click_labs_f(labs_id); String texty=get_text(); sleep_own(2); verify_same_labs_f(textx,texty); }else{ click_labs_f(labs_id); } } labs_id=labs_id-4;//将标签ID置为2 } @Test//须要确认点击第二个标签后,标签会不会总体右移动,即:点击后原第二个标签会不会变为第三个标签 public void switch_second_class_f(){ //为何是第二个而不是第一个,由于APP标签移动自己不够稳定,标签名过长时,显示的第一个标签的ID为2 click_labs_f(labs_id); String textx=get_text(); System.out.println(textx); click_labs_f(labs_id); String texty=get_text(); verify_same_labs_f(textx,texty); } //首页点击一个分类标签 public void click_labs_f(int labs_id){ labs_id=this.labs_id; driver .findElement(By /*.xpath("//android.widget.LinearLayout[@resource-id='com.jiubei.shop:id/groupView']/android.widget.LinearLayout["+labs_id+"]"))*/ .xpath("//android.widget.LinearLayout[@resource-id='com.jiubei.shop:id/groupView']/android.widget.LinearLayout["+labs_id+"]")) .click(); } public void click_back_labs_f(){ sleep_own(2); driver .findElement(By .xpath("//android.widget.LinearLayout[3]//android.widget.RelativeLayout[1]//android.widget.TextView[1]")) .click(); System.out.println(labs_id); } public String get_text(){ String lab=driver .findElement(By ////android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.HorizontalScrollView[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout["+labs_id+"]/android.widget.RelativeLayout[1]/android.widget.TextView[1] //android.widget.LinearLayout["+labs_id+"]//android.widget.RelativeLayout[1]//android.widget.TextView[1] .xpath("//android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[3]/android.widget.RelativeLayout[1]/android.widget.TextView[1]")) .getText(); return lab; } //确认是否为最后/第二一个标签 public void verify_same_labs_f(String text1,String text2){ if(text1.equals(text2)){ System.out.println("当前元素为:"+text2); //System.out.println("正常点击到最后一个标签"); }else{ text1=get_text(); System.out.println(text1); click_labs_f(labs_id); text2=get_text(); System.out.println(text2); verify_same_labs_f(text1, text2); } } //接口实现的方法 public void sleep_own(int i) { try { Thread.sleep(i*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
总结:能判断只能点击至最后一个标签,固然也能够只能点击至第一个标签,各位本身试试,我就不写出来了!
有疑问的话,能够加群:219537016.
.net