先参照封装
这里主要是对所定义的变量通过配置文件来读取,当有页面改动时,可直接改动配置文件的内容,而不用动代码!
先创建element.properties文件,默认在根目录下即/Selenium/下
创建PorUtil类
以下为ProUtil工具类
package musi.selenium;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ProUtil {
private Properties prop;
private String filePath;
/**
*
* 构造方法
* @author couey
* @date 2017年8月15日 上午10:48:51
*/
public ProUtil(String filePath){
this.filePath = filePath;
this.prop = readProperties();
}
/**
*
* 读取配置文件
* @author couey
* @date 2017年8月15日 上午10:57:18
*/
private Properties readProperties(){
Properties properties = new Properties();
try
{
InputStream inputStream = new FileInputStream(filePath);
BufferedInputStream in = new BufferedInputStream(inputStream);
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
public String getPro(String key) throws Exception{
//element.properties文件中,第一列为元素对象,第二列为定位方式By,第三列为定位的值
if(prop.containsKey(key)){
String valueElement = prop.getProperty(key);
return valueElement;
}else{
System.out.println("你获取的key不对");
return null;
}
}
}
知识点有:
1. 构造方法的使用;
- 文件输入及读取:InputStream和BufferedInputStream
InputStream inputStream = new FileInputStream(filePath);
BufferedInputStream in = new BufferedInputStream(inputStream);
properties.load(in);
- containsKey(key):判断是否包含提供的值的函数
- element.properties文件的格式及含义
其中:第一列为元素对象,第二列为定位方式By,第三列为定位的值
分隔符可以自定义
通过对配置文件的读取,达到对By方法的优化:
之前有两个参数:String by,String local
现在只有一个参数:String valueElement,即配置文件里的第一列值
然后通过getPro(valueElement),传入配置文件的第一列值,得到定位方式By及定位的值,即配置文件中的第二列和第三列值
进而修改元素定位的代码:
可以对比之前的代码,可以发现代码简洁了不少,已经看不到具体的定位方式。
至于参数化,也就是使用HashMap对帐号密码进行了简单的参数化了
以下为整个优化后的代码
package musi.selenium;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;
public class Login {
private static String baseUrl;
private static String path
= "C:\\Users\\couey\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\6vo0xq9s.default";
static FirefoxProfile profile = new FirefoxProfile(new File(path));
static WebDriver driver = new FirefoxDriver(profile);
public void InitDriver(){
baseUrl = "http://www.imooc.com/";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.manage().window().maximize();
driver.findElement(By.id("js-signin-btn")).click();
}
public void loginScript(String username,String pwd) throws Exception{
this.InitDriver();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//对By的方法进行调用。
WebElement user = this.element(this.byStr("emailElement"));
user.isDisplayed();
WebElement password = this.element(this.byStr("passwordElement"));
password.isDisplayed();
WebElement loginButton = this.element(this.byStr("buttonElement"));
loginButton.isDisplayed();
user.sendKeys(username);
password.sendKeys(pwd);
loginButton.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement header = this.element(this.byStr("headerElement"));
header.isDisplayed();
//鼠标事件:鼠标移动到头像(要点击的部位),然后定位到元素,然后提交(perform())
Actions actions = new Actions(driver);
actions.moveToElement(header).perform();
String userInfo = this.element(this.byStr("nameElement")).getText();
if(userInfo.equals("SuperBadBoy")){
System.out.println(userInfo + ":" + "Successful");
}else{
System.out.println("Failed,Please check your infomation");
}
driver.close();
}
/**
*
* 封装By
*
* @param String by,String local
*
* @author couey
* @throws Exception
* @date 2017年8月14日 下午3:53:09
*/
public By byStr (String valueElement) throws Exception{
//读取配置文件内容
ProUtil properties = new ProUtil("element.properties");
String locator = properties.getPro(valueElement);
//定位第一个值
String locatorType = locator.split(">")[0];
//定位第二个值
String locatorValue = locator.split(">")[1];
if(locatorType.equals("id")){
return By.id(locatorValue);
}else if(locatorType.equals("name")){
return By.name(locatorValue);
}else if(locatorType.equals("className")){
return By.className(locatorValue);
}else{
return By.xpath(locatorValue);
}
}
/**
*
* 封装Element
*
* @param By by
*
* @author couey
* @date 2017年8月14日 下午4:33:39
*/
public WebElement element(By by){
WebElement ele = driver.findElement(by);
return ele;
}
public static void main( String[] args ) throws Exception
{
Login login = new Login();
/**
* 用户名/密码参数化
* HashMap实现参数化
*/
HashMap<String,String> user = new HashMap<String,String>();
user.put("c******in@163.com", "********");
user.put("test", "123456");
Iterator us = user.entrySet().iterator();
while(us.hasNext()){
Map.Entry entry = (Entry) us.next();
String username = entry.getKey().toString();
String password = entry.getValue().toString();
login.loginScript(username,password);
}
}
}