慕课网自动化测试实战
实现功能:账号登录
需求:
- 验证登录业务流程(进入慕课网首页->登录->获取用户ID)
- 验证账号输入框、密码输入框、登录按钮、用户头像的状态
注意点:
- By定位api的封装
- driver.findElement()的封装
登录用例(多少有点出入)
自动化脚本:
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;
/**
* 登录慕课网自动化测试脚本
*/
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() throws InterruptedException {
String advert=".redrain-closeBtn";
String come_login=".//*[@id='js-signin-btn']";
String userString="email";
String passwordString="password";
String loginButtonString=".moco-btn.moco-btn-red.moco-btn-lg.btn-full.xa-login";
String headString=".//*[@id='header-avator']/img";
String userIDString=".//*[@id='header-user-card']/div/div/div[1]/div/a/span";
//解决广告弹窗
this.element(this.byString("cssSelector",advert)).click();
//进入登录页面
this.element(this.byString("xpath",come_login)).click();
Thread.sleep(3000);
WebElement user=this.element(this.byString("name",userString));
WebElement password=this.element(this.byString("name",passwordString));
WebElement loginButton=this.element(this.byString("cssSelector",loginButtonString));
System.out.println("账号输入框状态:"+user.isDisplayed());
System.out.println("密码输入框状态:"+password.isDisplayed());
System.out.println("登录按钮状态:"+loginButton.isDisplayed());
user.sendKeys("2794974296@qq.com"); //输入内容
password.sendKeys("dpl12345");
loginButton.click(); //点击登录
Thread.sleep(7000);
//点击用户头像
WebElement header=this.element(this.byString("xpath",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("xpath",userIDString)).getText();
System.out.println("用户名ID:"+userID);
}
/**
*By定位API的封装
*/
public By byString(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 if (by.equals("linkText")){
return By.linkText(local);
}else if (by.equals("partialLinkText")){
return By.partialLinkText(local);
}else if(by.equals("tagName")){
return By.tagName(local);
}else if (by.equals("cssSelector")){
return By.cssSelector(local);
}else{
return By.xpath(local);
}
}
/**
* 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 {
Login_mooc mooc=new Login_mooc();
mooc.InitDriver();
mooc.loginScript();
mooc.destroyDriver();
}
}
实现效果:
控制台输出结果:
账号输入框状态:true
密码输入框状态:true
登录按钮状态:true
用户头像状态:true
用户名ID:慕仔2201893