7.selenium之验证测试

不管是在做功能测试还是自动化测试,最后一步需要拿实际结果与预期进行比较。这个比较的称之为断言。
我们通常可以通过获取title 、URL和text等信息进行断言。text方法在前面已经讲过,它用于获取标签对之间的文本信息。

  • getTitle(): 用于获得当前页面的title。
  • getCurrentUrl() : 用户获得当前页面的URL。
  • getText() 获取页面文本信息。

下面同样以百度为例,介绍如何获取这些信息。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;


import static org.testng.AssertJUnit.assertEquals;
import static org.testng.TestNGAntTask.Mode.testng;

public class AssertTestNg {

    String url = "http://www.baidu.com";
    WebDriver driver = new ChromeDriver();

    public String title(){
        driver.get(url);
        return driver.getTitle();
    }

    @Test
    public void verifyTitle(){
        assertEquals(title(),"百度一下,你就知道");
    }

    @AfterTest
    public void quit() throws InterruptedException {
        Thread.sleep(2000);
        driver.quit();
    }
}

结果:

原文地址:https://www.cnblogs.com/peiminer/p/13560705.html