場景說明: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