美文网首页
数据和脚本分离之读取properties文件

数据和脚本分离之读取properties文件

作者: 默默的joy | 来源:发表于2017-02-16 14:02 被阅读0次

为了方便管理测试数据,需要将数据和脚本分离,独立来管理数据。其中一个方法我们可以通过读取properties文件或是excel文件来读取测试数据

1.properties文件内容都是以键值对形式存在的,在工程文件下建立一个properties文件


properties文件.jpg

2.创建读取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.创建一个测试类读取properties文件,利用参数传值,运行

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,很繁琐

相关文章

网友评论

      本文标题:数据和脚本分离之读取properties文件

      本文链接:https://www.haomeiwen.com/subject/cjofmttx.html