Cucumber 之参数化

本贴最后更新于 1031 天前,其中的信息可能已经时移世异

1、什么是参数化

设计测试用例过程中,需要考虑正向和方向用例,比如设计登录用例,这时我们想连续做4次反向用例和1次正向用例,就可以用到参数化实现每次执行的用户名和密码都不一样。

2、数据驱动 Scenario Outline 和 Examples 关键字

参数化和数据驱动搭配使能使我们的框架更加灵活。

Scenario OutlineExamples 搭配使用,不能再使用 Scenario

WhenThen中使用 Examples定义的列名,必须用"<>"包裹。

Examples下面写准备好的数据。

1.png

3、上案例

这一次还是使用上次百度搜索的案例。

3.1、LemonRunner类

@CucumberOptions(
        features = {"src/test/resources/lemon4.feature"},
        glue = "com.lemon.steps",
        monochrome = true
)
public class LemonRunner extends AbstractTestNGCucumberTests {

}

3.2、LemonSteps类

public class LemonSteps {
    static {
        //设置chrome驱动位置
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
    }

    //定义WebDriver对象
    private final WebDriver driver = new ChromeDriver();

    @Given("打开百度搜索")
    public void open() throws Exception {
        driver.get("https://www.baidu.com");
        Thread.sleep(2000);
    }

    @When("输入 {string}")
    public void searchElement(String kw) {
        WebElement element = driver.findElement(By.name("wd"));
        // 输入 testingpai
        element.sendKeys(kw);
        // 点击搜索
        element.submit();
    }

    @Then("显示 {string}")
    public void check(final String title) {
        new WebDriverWait(driver,5).until(
                ExpectedConditions.visibilityOfElementLocated(
                        By.xpath("//a[contains(text(),'"+title+"')]")));
    }

    @After()
    public void closeBrowser() throws Exception {
        Thread.sleep(3000);
        driver.quit();
    }
}

3.3、feature文件

Feature: 使用chrome浏览器访问百度搜索柠檬班论坛

  Scenario Outline: 百度搜索testingpai
    Given 打开百度搜索
    When 输入 "<kw>"
    Then 显示 "<title>"

    Examples:
      | kw         | title     |
      | testingpai | 柠檬班 - 测试派 |
      | 柠檬班        | 软件测试      |

3.4、testng.xml

<?xml version="1.0" encoding="UTF-8"?>

<suite name="futureloan" >
    <test name="登录测试">
        <classes>
            <class name="com.lemon.runner.LemonRunner"></class>
        </classes>
    </test>
</suite>

3.5、运行效果,连续执行两次

11.gif

回帖
请输入回帖内容 ...