51Testing软件测试论坛

标题: Selenium数据驱动 [打印本页]

作者: yjlch1016    时间: 2018-1-24 16:03
标题: Selenium数据驱动
Selenium数据驱动
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNG {
    private WebDriver driver;

    @BeforeMethod
    //测试之前的准备工作
    public void beforeMethod() throws InterruptedException {
        System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe");
        String baiduHomePage;
        baiduHomePage = "https://www.baidu.com/";

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get(baiduHomePage);
        Thread.sleep(2000);
        Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
    }

    @Test(dataProvider = "word")
    //测试用例
    public void testNG(String keyword, String case_1, String searchTitle)
            throws InterruptedException {
        driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys(keyword);
        driver.findElement(By.xpath(".//*[@id='su']")).click();
        Thread.sleep(2000);

        Reporter.log(case_1);
        Assert.assertEquals(driver.getTitle(), searchTitle);
    }

    @AfterMethod
    //测试之后的清理工作
    public void afterMethod(){
        driver.close();
        driver.quit();
    }

    @DataProvider(name = "word")
    public static Object[][] data(){
        return new Object[][]{{"Selenium", "搜索Selenium的测试用例", "Selenium_百度搜索"},
                {"JMeter", "搜索JMeter的测试用例", "JMeter_百度搜索"},
                {"Appium", "搜索Appium的测试用例", "Appium_百度搜索"}
    };
    }
}









欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2