1. 程式人生 > >自動化框架學習二,判斷定位方式

自動化框架學習二,判斷定位方式

自動化學習

/**判斷定位方式工具類*/
public class GetByLoctorTest {

/**
*讀取配置文件
*@param key String 定位方式
* */
public static By getLocator(String key){

//ProUtilTest類是讀取配置文件的(自定義),使用構造方法時,需傳入配置文件的路徑
//文件的路徑可以單獨寫一個類,進行配置
ProUtilTest properties = new ProUtilTest("configs/login.properties");


//從屬性配置文件中讀取相應的配置對象
//locator=name>登錄
String locator = properties.getPro(key);

//將配置對象中的定位類型存在locatorType變量,將定位表達式的值存入到locatorValue變量
//[0]為>的左邊 [1]為>的右邊
String locatorType = locator.split(">")[0];//取出>前的name
String locatorValue = locator.split(">")[1];//取出登錄

//輸出locatorType變量值和locatorValue變量值,驗證是否賦值成功
System.out.println("獲取的定位類型:"+locatorType+"獲取的定位表達式:"+locatorValue);

//根據locatorType的變量值內容判斷,返回何種定位方式的By對象
//toLowerCase() 方法用於把字符串轉換為小寫
if(locatorType.toLowerCase().equals("id")){
return By.id(locatorValue);
}else if(locatorType.toLowerCase().equals("name")){
return By.name(locatorValue);
}else if ((locatorType.toLowerCase().equals("classname")) || (locatorType.toLowerCase().equals("class"))){
return By.className(locatorValue);
}else if((locatorType.toLowerCase().equals("tagname")) || (locatorType.toLowerCase().equals("tag"))){
return By.className(locatorValue);
}else if ((locatorType.toLowerCase().equals("linktext")) || (locatorType.toLowerCase().equals("link"))){
return By.linkText(locatorValue);
}else if (locatorType.toLowerCase().equals("partiallinktext")){
return By.partialLinkText(locatorValue);
}else if ((locatorType.toLowerCase().equals("cssselector")) || (locatorType.toLowerCase().equals("css"))){
return By.cssSelector(locatorValue);
}else if (locatorType.toLowerCase().equals("xpath")){
return By.xpath(locatorValue);
}else{
try{
throw new Exception("輸入的 locator type 未在程序中被定義:" + locatorType);
}catch(Exception e){
e.printStackTrace();
}
}
return null;

}
}

自動化框架學習二,判斷定位方式