APP自動化框架學(xué)習(xí):讀取配置文件,并判斷定位方式

思路:

創(chuàng)新互聯(lián)主營薩爾圖網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),薩爾圖h5重慶小程序開發(fā)搭建,薩爾圖網(wǎng)站營銷推廣歡迎薩爾圖等地區(qū)企業(yè)咨詢

1,創(chuàng)建一個配置文件,配置文件內(nèi)寫入 "元素的名稱=定位的方式>元素的id/name/xpath表達式",例:"locator=name>登錄"

2,創(chuàng)建個讀取配置文件的類ProUtilTest,使用java中的Properties類,讀取Java的配置文件

3,判斷定位的方式,使用split()方法取出"locator=name>登錄"中的“name”和“登錄”

4,對split()方法取出來的定位方式,進行判斷,返回相應(yīng)的By類型,用于之后的定位元素

5,繼續(xù)學(xué)習(xí)完善中。。。


/**判斷定位方式工具類*/
public class GetByLoctorTest {
    
    /**
     *讀取配置文件
     *@param key String 定位方式
     * */
    public static By getLocator(String key){
        
        //ProUtilTest類是讀取配置文件的(見下文的圖),使用構(gòu)造方法時,需傳入配置文件的路徑
        //文件的路徑可以單獨寫一個類,進行配置
        ProUtilTest properties = new ProUtilTest("configs/login.properties");
        
        //從屬性配置文件中讀取相應(yīng)的配置對象
        //配置文件寫的格式:"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);
        
        //根據(jù)locatorType的變量值內(nèi)容判斷,返回何種定位方式的By對象
        //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()判斷是否有相對應(yīng)的key
    public String getPro(String key){
        if(prop.containsKey(key)){
            return prop.getProperty(key);
        }else{
            System.out.println("獲取的鍵不存在");
        }
        return "";
    }

網(wǎng)站題目:APP自動化框架學(xué)習(xí):讀取配置文件,并判斷定位方式
文章分享:http://muchs.cn/article46/jopeeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、手機網(wǎng)站建設(shè)、網(wǎng)站營銷、微信公眾號域名注冊、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司