智能點擊標簽至最后一個再返回至第二個


  • 場景說明:APP中上部一般都會有動態標簽,當點擊最后一個標簽,標簽會有整體左移的效果, 左移后,頁面的最右側標簽不一定是最后一個,需要判斷,如果不是最后一個, 繼續點擊一次,如果是,則說明 標簽整體左移功能正常。

  •    <li><p>吐槽點:使用xpath的定位方式是不是很low,稍后和前端商量下,看看能不能幫忙加個ID等。
            </p>
            <p>吐槽點2:get_text()方法中我注釋了幾個xpath,主要是因為頁面上使用相對地址地位元素時,
                                  發現多個元素的xpath是一模一樣的,故將元素的fullxpath復制之后,
                                  觀察xpath路徑規律,截取對應的xpath,就不會報錯了!
            </p>
       </li>
       <li><p>更新點:之前有人問過我,為什么標簽沒有ID,定位不到,該怎么點擊?
              然而針對我下面的代碼,我就非常需要內部文本層的ID了,否則使用xpath會導致代碼執行效率很低。
              一直很少和前端打太多交道,今天拜托前端在各個標簽的文本層上都加個ID,前端人還是不錯的,
              有了ID,相信代碼書寫就能更簡單,執行也能更快了!
            </p>
       
       </li>
    
``` 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.
![](https://img2018.cnblogs.com/blog/1209230/201811/1209230-20181105120558089-1791710672.png)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM