自动化测试系列(4)—— testng登场

自动化测试系列(4)—— testng登场

上一篇通过chrome进行了登录操作,现在来验证一下是否登录成功。


和第一天一样,在pom.xml文件下写上引入testng的jar包的代码并导入


<dependency>
    <groupId>org.testnggroupId>
    <artifactId>testngartifactId>
    <version>6.14.3version>
    <scope>testscope>
dependency>

用例执行前后操作

自定义测试用例执行前后的操作,以下代码在用例执行前后打印和执行后关闭浏览器进程及驱动

    @BeforeClass
    public void beforeClass() {
        System.out.println("-------用例执行前-------");
    }

    @AfterClass
    public void afterClass(){
        System.out.println("-------用例执行后-------");
        //关闭浏览器进程及驱动
        driver.close();
    }

判断元素是否存在以判断是否登录成功,参考了以下博文的第一段代码
Selenium2(WebDriver)_如何判断WebElement元素对象是否存在

    public boolean doesWebElementExist(WebDriver driver, By selector)
    {

        try
        {
            driver.findElement(selector);
            return true;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }
    }

判断特定元素是否存在+断言以校验是否登录成功

    if(doesWebElementExist(driver,By.cssSelector(".title2"))){
        //断言 校验是否登录成功
        Assert.assertEquals(driver.findElement(By.cssSelector(".title2")).getText(),loanerLoginVo.getLoanerPhone());
    } else {
        throw new RuntimeException("登录失败");
    }

测试代码

测试代码如下

/**
 * @author xian
 * @date 2018/8/2 14:32
 */
public class H5LoanerLoginTest {

    public boolean doesWebElementExist(WebDriver driver, By selector)
    {

        try
        {
            driver.findElement(selector);
            return true;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }
    }

    WebDriver driver = getDriver();

    // 定义数据源
    @DataProvider(name = "list")
    public Iterator createData() throws FileNotFoundException, ExcelException {
        LinkedHashMap fieldMap = new LinkedHashMap();
        fieldMap.put("手机","loanerPhone");
        fieldMap.put("密码","loanerPassword");
        LoanerLoginVo loanerLoginVo = new LoanerLoginVo();
        //从excel中获取数据
        return ExcelUtil.excelIn(fieldMap, "loanerLogin", "登录信息", loanerLoginVo.getClass());
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("用例前执行打印本句!");
        System.out.println("每条Test用例是互不相干的");
        System.out.println("用例开始执行…………");
    }

    @AfterClass
    public void afterClass(){
        System.out.println("用例结束后运行");
        //关闭浏览器进程及驱动
        driver.close();
    }

    public WebDriver getDriver(){
        //设置驱动所在位置
        System.setProperty("webdriver.chrome.driver", "E:\\software\\driver\\chromedriver.exe");
        //设置chrome的属性
        ChromeOptions options = new ChromeOptions();
        List op = new ArrayList();
        //实现窗口最大化
        op.add("--start-maximized");
        //实现全屏
        //op.add("start-fullscreen");
        //op.add("allow-running-insecure-content");
        //op.add("--test-type");
        options.addArguments(op);
        //引用谷歌浏览器驱动
        WebDriver driver = new ChromeDriver(options);
        return driver;
    }

    @Test(dataProvider = "list")
    public void action(LoanerLoginVo loanerLoginVo){

        //自定义浏览器窗口大小
        driver.manage().window().setSize(new Dimension(375, 812));
        //打开网页
        driver.get("http://localhost/huinongloan2/");
        //输入手机号
        driver.findElement(By.cssSelector(".login-phone input")).sendKeys(loanerLoginVo.getLoanerPhone());
        //输入密码
        driver.findElement(By.cssSelector(".login-password input")).sendKeys(loanerLoginVo.getLoanerPassword());

        //点击登录
        driver.findElement(By.cssSelector(".login-submit button")).click();

        try {
            //页面等待
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(doesWebElementExist(driver,By.cssSelector(".title2"))){
            //断言 校验是否登录成功
            Assert.assertEquals(driver.findElement(By.cssSelector(".title2")).getText(),loanerLoginVo.getLoanerPhone());
        } else {
            throw new RuntimeException("登录失败");
        }

    }
}

有问题请留言。

你可能感兴趣的:(selenium)