1、什么是参数化
设计测试用例过程中,需要考虑正向和方向用例,比如设计登录用例,这时我们想连续做4次反向用例和1次正向用例,就可以用到参数化实现每次执行的用户名和密码都不一样。
2、数据驱动 Scenario Outline 和 Examples 关键字
参数化和数据驱动搭配使能使我们的框架更加灵活。
Scenario Outline
和 Examples
搭配使用,不能再使用 Scenario
。
在When
、Then
中使用 Examples
定义的列名,必须用"<>"包裹。
Examples
下面写准备好的数据。
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>
欢迎来到testingpai.com!
注册 关于