数据驱动测试--使用TestNG进行数据驱动

测试逻辑:
(1)打开百度首页
(2)在搜索框输入关键词进行搜索
(3)点击搜索
(4)验证搜索结果页面是否包含搜索的两个关键词,包含则认为测试执行成功,否则测试执行失败。

代码:

package SearchTest;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class SearchDemo {
    private static WebDriver driver;
    @DataProvider(name = "searchWords")

    public static Object[][] words()
    {
    return new Object [][]
            {
            {"蝙蝠侠","主演","迈克尔"},
            {"生化危机","编剧","安德森"},
            };
    }
    @Test(dataProvider = "searchWords")
    public void test(String searchWord1,String searchWord2,String SearchResult) {
        System.setProperty("webdriver.ie.driver", "D:\\Java\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        //设定等待时间10s
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.baidu.com/");
        driver.findElement(By.id("kw")).sendKeys(searchWord1+""+searchWord2);
        driver.findElement(By.id("su")).click();

        //等待3秒显示搜索结果
        try{
        Thread.sleep(3000);
        }catch(InterruptedException e){
        e.printStackTrace();
        }

        //判断搜索结果的页面是否包含测试数据中期望的关键字
        Assert.assertTrue(driver.getPageSource().contains(SearchResult));
        driver.quit();
        }
}

测试结果:

PASSED: test("蝙蝠侠", "主演", "迈克尔")
PASSED: test("生化危机", "编剧", "安德森")

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

其中,TestNG参数化之DataProvider的解释资料参考:
https://blog.csdn.net/langsand/article/details/53895654

你可能感兴趣的:(Selenium,WebDriver)