本课程全部以慕课网为实例。
以登陆的自动化脚本为例:
1. 初始化环境变量;
2. 构建登陆脚本:
定位元素,通常为:
定位方法为By.name,name的值为“email”
WebElement user = driver.findElement(By.name("email"));
定位方式为By.className,值为“btn-red btn-full xa-login”,可只取btn-red
WebElement loginButton = driver.findElement(By.className("btn-red"));
但是,每一个元素的定位都是固定的格式,但定位方法却不一样,假设某天网页代码变化了,以前是By.name能定位到,现在换成了class,那么就要改变代码了。
所以,就要用到java的封装思想:
我们把value统一定义好,定位方式统一定义为一个方法,如下:
public By byStr (String by,String local){
if(by.equals("id")){
return By.id(local);
}else if(by.equals("name")){
return By.name(local);
}else if(by.equals("className")){
return By.className(local);
}else{
return By.xpath(local);
}
}
定义byStr方法,传入”定位方式“和”位置值“,那么就可以进行匹配判断。
封装后如下:
String userBy = "name";
String emailElement = "email";
WebElement user = driver.findElement(this.byStr(userBy, emailElement));
同样,每一个元素的定位时,均会用到driver.findElement(…..),也可以对其进行封装:
定义一个元素的方法element(),参数只有一个By(方式,如By.id),即element(By by),
public WebElement element(By by){
WebElement ele = driver.findElement(by);
return ele;
}
结果代买整理如下:
WebElement user = this.element(this.byStr(userBy, emailElement));
整体看起来,代码的可读性就大大提升了。完整代码如下:
package musi.selenium;
import java.io.File;
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(){
this.InitDriver();
String username = "co****[email protected]";
String userpass = "*********";
String emailElement = "email";
String passwordElement = "password";
String buttonElement = "btn-red";
String headerElement = "header-avator";
//对调用方法By进行赋值
String nameElement = "name";
String userBy = "name";
String passwordBy = "name";
String buttonBy = "className";
String userInfoBy = "className";
String headerBy = "id";
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// WebElement user = driver.findElement(By.name(emailElement));
// WebElement password = driver.findElement(By.name(passwordElement));
// WebElement loginButton = driver.findElement(By.className(buttonElement));
// WebElement header = driver.findElement(By.id(headerElement));
// String userInfo = driver.findElement(By.className(nameElement)).getText();
//对By的方法进行调用。
WebElement user = this.element(this.byStr(userBy, emailElement));
user.isDisplayed();
WebElement password = this.element(this.byStr(passwordBy, passwordElement));
password.isDisplayed();
WebElement loginButton = this.element(this.byStr(buttonBy, buttonElement));
loginButton.isDisplayed();
user.sendKeys(username);
password.sendKeys(userpass);
loginButton.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement header = this.element(this.byStr(headerBy, headerElement));
header.isDisplayed();
//鼠标事件:鼠标移动到头像(要点击的部位),然后定位到元素,然后提交(perform())
Actions actions = new Actions(driver);
actions.moveToElement(header).perform();
String userInfo = this.element(this.byStr(userInfoBy,nameElement)).getText();
if(userInfo.equals("SuperBadBoy")){
System.out.println(userInfo + ":" + "Successful");
}else{
System.out.println("Failed,Please check your infomation");
}
}
/**
*
* 封装By
*
* @param String by,String local
*
* @author couey
* @date 2017年8月14日 下午3:53:09
*/
public By byStr (String by,String local){
if(by.equals("id")){
return By.id(local);
}else if(by.equals("name")){
return By.name(local);
}else if(by.equals("className")){
return By.className(local);
}else{
return By.xpath(local);
}
}
/**
*
* 封装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 )
{
Login login = new Login();
login.loginScript();
}
}
以上代码,还可以再进一步封装,待续!
关于浏览器初始化相关内容,可彻底解决selenium启动firefox时总弹框的问题