數(shù)據(jù)和腳本分離之讀取properties文件

為了方便管理測試數(shù)據(jù),需要將數(shù)據(jù)和腳本分離,獨立來管理數(shù)據(jù)。其中一個方法我們可以通過讀取properties文件或是excel文件來讀取測試數(shù)據(jù)

1.properties文件內容都是以鍵值對形式存在的,在工程文件下建立一個properties文件


properties文件.jpg

2.創(chuàng)建讀取properties文件的java類

public class PropertyReader {
// 導入文件
    private static Properties props = new Properties();
    public static void init(String file) throws AssertionError{
        try {
            props.load(new FileInputStream(file));
        } catch (Exception e) {
            try {
                props.load(props.getClass().getResourceAsStream(file));

            } catch (FileNotFoundException e1) {
                throw new AssertionError("File with locator's information not found: " + e.toString());
            } catch (IOException e1) {
                throw new AssertionError("IO error while trying to reach locator's information file: " + e.toString());
            } catch (Exception ex) {
                try {
                    props.load(Reflection.getCallerClass(3).getResourceAsStream(file));
                } catch (Exception e1) {
                    throw new AssertionError("Unknown exception when calling throw Reflection: " + e1.toString());
                }
            }
        }
    }
// 獲取屬性值
    public static String getProperty(String key) {
        return props.getProperty(key);
    }

    public static Properties getProps() {
        return props;
    }
}

3.創(chuàng)建一個測試類讀取properties文件,利用參數(shù)傳值,運行

public class test {
    
    @Test
    public void test(){
        String path = System.getProperty("user.dir");
        String file = path + "/src/test/java/Data/data.properties";
        PropertyReader.init(file);
        driver = SeleniumDriver.openBrowser("firefox", "http://www.epwk.us/");
        Action.click(LoginPage.loginButton);
//        輸入賬號密碼登陸
        Action.sendkeys(LoginPage.account, PropertyReader.getProperty("account"));
        Action.sendkeys(LoginPage.password, PropertyReader.getProperty("password"));
        Action.click(LoginPage.submintButton);
    }
}

利用這個有個弊端就是每次傳入的值都要寫對應的key,很繁瑣

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,256評論 6 342
  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的...
    笨鳥慢飛閱讀 6,220評論 0 4
  • 轉自:http://www.linuxidc.com/Linux/2011-05/35723.htm 你是否遇到過...
    Devid閱讀 1,596評論 6 3
  • 關于什么是文化,我最最欣賞的回答,是作家梁曉聲的四句概括:根植于內心的修養(yǎng);無需提醒的自覺;以約束為前提的自由;為...
    Sherry曉閱讀 506評論 0 5

友情鏈接更多精彩內容