慕課網自動化測試實戰
實現功能:賬號登錄
需求:
- 對前篇文章的代碼通過配置文件去加載標籤元素屬性數據
實現思路:
- 設置一個配置文件,用於存儲定位元素的屬性數據
- 通過Properties函數讀取配置文件中的數據
- 最後把數據填充到登錄腳本中
登錄腳本:
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.interactions.Actions;
import java.io.IOException;
/**
* 登錄慕課網自動化測試腳本
*/
public class Login_mooc {
public WebDriver driver;
public void InitDriver() throws InterruptedException {
driver = new FirefoxDriver();
driver.get("https://www.imooc.com/");
// driver.manage().window().maximize();
Thread.sleep(3000);
}
public void loginScript(String username,String pass) throws InterruptedException, IOException {
//解決廣告彈窗
this.element(this.byString("advert")).click();
//進入登錄頁面
this.element(this.byString("come_login")).click();
Thread.sleep(3000);
WebElement user=this.element(this.byString("userString"));
WebElement password=this.element(this.byString("passwordString"));
WebElement loginButton=this.element(this.byString("loginButtonString"));
System.out.println("賬號輸入框狀態:"+user.isDisplayed());
System.out.println("密碼輸入框狀態:"+password.isDisplayed());
System.out.println("登錄按鈕狀態:"+loginButton.isDisplayed());
user.sendKeys(username); //輸入內容
password.sendKeys(pass);
loginButton.click(); //點擊登錄
Thread.sleep(7000);
//點擊用戶頭像
WebElement header=this.element(this.byString("headString"));
System.out.println("用戶頭像狀態:"+header.isDisplayed());
//懸停到用戶頭像上
Actions action= new Actions(driver);
action.moveToElement(header).perform();
Thread.sleep(4000);
//獲取用戶名ID
String userID=this.element(this.byString("userIDString")).getText();
System.out.println("用戶名ID:"+userID);
if (userID.equals("慕仔2201893")){
System.out.println("登陸成功");
}else{
System.out.println("登錄失敗");
}
}
/**
*By定位API的封裝
*/
public By byString(String key) throws IOException {
//使用配置文件中的html元素屬性數據(key->value)
PropertyUtil propertyUtil=new PropertyUtil("element.properties");
String locator=propertyUtil.getProperty(key);
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 if (locatorType.equals("linkText")){
return By.linkText(locatorValue);
}else if (locatorType.equals("partialLinkText")){
return By.partialLinkText(locatorValue);
}else if(locatorType.equals("tagName")){
return By.tagName(locatorValue);
}else if (locatorType.equals("cssSelector")){
return By.cssSelector(locatorValue);
}else{
return By.xpath(locatorValue);
}
}
/**
* driver.findElement()的封裝
*/
public WebElement element(By by){
WebElement element=driver.findElement(by);
return element;
}
public void destroyDriver() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
public static void main(String[] args) throws InterruptedException, IOException {
Login_mooc mooc=new Login_mooc();
mooc.InitDriver();
mooc.loginScript("[email protected]","dpl12345");
mooc.destroyDriver();
}
}
配置文件:
element.properties
#key->value
advert=cssSelector>.redrain-closeBtn
come_login=xpath>.//*[@id='js-signin-btn']
userString=name>email
passwordString=name>password
loginButtonString=cssSelector>.moco-btn.moco-btn-red.moco-btn-lg.btn-full.xa-login
#autoSignin=id>auto-signin
headString=xpath>.//*[@id='header-avator']/img
userIDString=xpath>.//*[@id='header-user-card']/div/div/div[1]/div/a/span
配置文件的工具類
import java.io.*;
import java.util.Properties;
/**
* 定位元素數據存入的配置文件的工具類
*/
public class PropertyUtil {
private Properties properties;
private String filePath;//配置文件的路徑
public PropertyUtil(String filePath) throws IOException {
this.filePath=filePath;
this.properties=readProperties();
}
/**
* 讀取配置文件
*/
private Properties readProperties() throws IOException {
properties=new Properties();
InputStream inputStream=new FileInputStream(filePath);
BufferedInputStream inputStream1=new BufferedInputStream(inputStream);
//load方法其實就是傳進去一個輸入流,字節流或者字符流
properties.load(inputStream);
return properties;
}
/**
* 獲取配置文件中的指定數據
*/
public String getProperty(String key) throws IOException {
if (properties.containsKey(key)) {
String userString=properties.getProperty(key);
return userString;
}else {
System.out.println("你獲取的key不存在");
return "";
}
}
}