1:重构前的代码
Remark:这样的代码虽然看起来简单 但是实际上可操作性及其差 如果需要修改定位方式或者定位元素的改变将会大面积地修改源代码 增加了不可操作性。
public class BeforeBean {
private WebDriver driver;
@Test
public void WebTest(){
// 初始化浏览器
driver = new FirefoxDriver();
driver.get("https://www.csdn.net/");
// 最大化浏览器
driver.manage().window().maximize();
// 获取登录框
driver.findElement(By.linkText("登录")).click();
// 点击账号登录
driver.findElement(By.linkText("linkText")).click();
// 输入账号
driver.findElement(By.id("username")).sendKeys("你的账号");
// 输入密码
driver.findElement(By.id("password")).sendKeys("你的密号");
// 点击登录
driver.findElement(By.xpath(".//*[@id='fm1']/input[8")).click();
}
}
2:重构后的代码
2.1:配置文件
#用户名和密码
LoginInfo=你的账号>你的密码
#页面获取登录框的位置
userCountBox=xpath>.//*[@id='csdn-toolbar']/div/div/ul/li[5]/a[1]
#获取"账号登录"的位置
countLogin=xpath>html/body/div[3]/div/div/div[2]/div/h3/a
#输入账号的位置
loginUser=id>username
#输入密码框的位置
loginPassword=xpath>.//*[@id='password']
#登录按钮的位置
loginButtion=xpath>.//*[@id='fm1']/input[8]
2.2:封装读取配置文件的utils方法
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);
}
}
2.3:重构后的登录的代码
public class testDemos {
private WebDriver driver;
@Test
public void testDemo() throws Exception{
String URL = "https://www.csdn.net/";
String PropertiesPath = "LoginElement.properties";
//创建RaadProperties对象
String value = this.readProUtils(PropertiesPath , "LoginInfo");
//对value进行拆分
//对value进行拆分
String username = value.split(">")[0];
String password = value.split(">")[1];
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().window().maximize();
Thread.sleep(3000);
// 寻找登录按钮 弹出登录框
this.elementBean(this.bystr("userCountBox")).click();
// 使用账号登录
this.elementBean(this.bystr("countLogin")).click();
// 输入账号
this.elementBean(this.bystr("loginUser")).sendKeys(username);
// 输入密码
this.elementBean(this.bystr("loginPassword")).sendKeys(password);
// 点击登录进行登录操作
this.elementBean(this.bystr("loginButtion")).click();
}
/**
* 封装by
* @throws IOException
* */
public By bystr(String PropertiesKey){
String PropertiesPath = "LoginElement.properties";
//创建RaadProperties对象
String value = this.readProUtils(PropertiesPath , PropertiesKey);
//对value进行拆分
String LocateMethon = value.split(">")[0];
String LocateEle = value.split(">")[1];
//System.out.println(LocateMethon+"========="+LocateEle);
if(LocateMethon.equalsIgnoreCase("id")){
return By.id(LocateEle);
}else if(LocateMethon.equalsIgnoreCase("name")){
return By.name(LocateEle);
}else if(LocateMethon.equalsIgnoreCase("tagNmae")){
return By.tagName(LocateEle);
}else if(LocateMethon.equalsIgnoreCase("linkText")){
return By.linkText(LocateEle);
}else if(LocateMethon.equalsIgnoreCase("xpath")){
return By.xpath(LocateEle);
}else if(LocateMethon.equalsIgnoreCase("cssSelector")){
return By.cssSelector(LocateEle);
}else{
return By.partialLinkText(LocateEle);
}
}
/**
* 封装Element
* */
public WebElement elementBean(By by){
return driver.findElement(by);
}
/**
* 封装Elements集合
* */
public List<WebElement> elementList(By by){
return driver.findElements(by);
}
/**
* 封装读取properties文件的方法
* */
public String readProUtils(String PropertiesPath, String PropertiesKey){
RaadProperties properties = new RaadProperties(PropertiesPath);
return properties.getValue(PropertiesKey);
}
}
点评:重构后的代码虽然代码量多了很多 但是却极大地方便了tester的可操作性,tester只用改变配置文件的内容即可实现对定位方式 定位元素 账号 密码的改变。