元素的id/name/xpath表達(dá)式 ,例: locator=name>登錄 2,創(chuàng)建個(gè)讀取配置文件的類ProUtilTe..."/>
您好,登錄后才能下訂單哦!
思路:
1,創(chuàng)建一個(gè)配置文件,配置文件內(nèi)寫入 "元素的名稱=定位的方式>元素的id/name/xpath表達(dá)式",例:"locator=name>登錄"
2,創(chuàng)建個(gè)讀取配置文件的類ProUtilTest ,使用java中的Properties類,讀取Java的配置文件
3,判斷定位的方式,使用split()方法取出"locator=name>登錄"中的“name”和“登錄”
4,對(duì)split()方法取出來的定位方式,進(jìn)行判斷,返回相應(yīng)的By類型,用于之后的定位元素
5,繼續(xù)學(xué)習(xí)完善中。。。
/**判斷定位方式工具類*/
public class GetByLoctorTest {
/**
*讀取配置文件
*@param key String 定位方式
* */
public static By getLocator(String key){
//ProUtilTest類是讀取配置文件的(見下文的圖),使用構(gòu)造方法時(shí),需傳入配置文件的路徑
//文件的路徑可以單獨(dú)寫一個(gè)類,進(jìn)行配置
ProUtilTest properties = new ProUtilTest("configs/login.properties");
//從屬性配置文件中讀取相應(yīng)的配置對(duì)象
//配置文件寫的格式:"locator=name>登錄"
String locator = properties.getPro(key);
//將配置對(duì)象中的定位類型存在locatorType變量,將定位表達(dá)式的值存入到locatorValue變量
//[0]為>的左邊 [1]為>的右邊
String locatorType = locator.split(">")[0];//取出>前的name
String locatorValue = locator.split(">")[1];//取出登錄
//輸出locatorType變量值和locatorValue變量值,驗(yàn)證是否賦值成功
System.out.println("獲取的定位類型:"+locatorType+"獲取的定位表達(dá)式:"+locatorValue);
//根據(jù)locatorType的變量值內(nèi)容判斷,返回何種定位方式的By對(duì)象
//toLowerCase() 方法用于把字符串轉(zhuǎn)換為小寫
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;
}
}
/**配置文件讀取類*/
public class ProUtilTest {
private String filePath;
private Properties prop;//Properties類,讀取Java的配置文件
public ProUtilTest(String filePath){
this.filePath = filePath;
this.prop=readProperties();
}
public Properties readProperties(){
Properties properties = new Properties();
try {
InputStream ins = new FileInputStream(filePath);
BufferedReader bf=new BufferedReader(new InputStreamReader(ins,"utf-8"));
properties.load(bf);
ins.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
//containsKey()判斷是否有相對(duì)應(yīng)的key
public String getPro(String key){
if(prop.containsKey(key)){
return prop.getProperty(key);
}else{
System.out.println("獲取的鍵不存在");
}
return "";
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。