慕课网web自动化测试实战之账号登录(一)

慕课网自动化测试实战

实现功能:账号登录

需求:
  1. 验证登录业务流程(进入慕课网首页->登录->获取用户ID)
  2. 验证账号输入框、密码输入框、登录按钮、用户头像的状态
注意点:
  1. By定位api的封装
  2. 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
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值