51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1359|回复: 0
打印 上一主题 下一主题

Selenium数据驱动

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-1-24 16:03:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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_百度搜索"}
    };
    }
}




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-3 15:17 , Processed in 0.073391 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表