Selenium-Java Web自动化-封装读取配置文件的方法-代码重构2

1:配置文件获取参数

1.1:LoginElement.properties

#This is login box locate methon and the element id's value.
userCountBox=id>usernam

1.2:封装读取配置文件 通过Key值获取Value的方法

public class RaadProperties {
	
	private String filePath = "LoginElement.properties";
	private Properties properties;
	
	/**
	 * 构造方法 创建对象时自动返回pro对象  在new该对象的时候会自动加载readProperties()方法
	 * */
	
	public RaadProperties(String filePath){
		this.filePath = filePath;
		//在new该对象的时候会自动加载readProperties()方法
		this.properties = readProperties();
	}
	
	/**
	 * 返回已经加载properties文件的pro对象
	 * */
	public Properties readProperties(){
		//创建对象
		Properties pro = new Properties();
		//读取properties文件到缓存
		try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePath));
			//加载缓存到pro对象
			pro.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//返回pro对象
		return pro;
	}
	
	/**
	 * 使用全局的properties对象获取key对应的value值
	 * @return 
	 * */
	public String getValue(String key){
		
		return properties.getProperty(key);
	}
}

1.3:测试方法

public class test {
	
	@Test
	public void testOnly(){
		//创建RaadProperties对象
		RaadProperties properties = new RaadProperties("LoginElement.properties");
		String value = properties.getValue("userCountBox");
		//对value进行拆分
		String LocateMethon = value.split(">")[0];
		String LocateEle = value.split(">")[1];		
		//调试输出
		System.out.println(LocateMethon+"====="+LocateEle);
	}
}

输出结果如下


猜你喜欢

转载自blog.csdn.net/hujyhfwfh2/article/details/81053415