慕课网自动化测试实战
实现功能:账号登录
需求:
- 对前篇文章的代码通过配置文件去加载标签元素属性数据
实现思路:
- 设置一个配置文件,用于存储定位元素的属性数据
- 通过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 "";
}
}
}