@base_flow
Feature: This is a simple test using Cucumber.
Scenario:
Given I enter my blog address "https://yaowenjie.github.io" and go to Home page
# Search Button Feature:
When I click the search button
And I enter "PowerShell" in the input field
And I click the first result of search
Then I go to the article page with title containing "PowerShell"
package com.wenjie;
import cucumber.api.java8.En;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.openqa.selenium.support.PageFactory.initElements;
public class BaseSteps implements En {
private WebDriver driver = new FirefoxDriver();
private BlogPage blogPage = new BlogPage();
public BaseSteps() {
blogPage = initElements(driver, BlogPage.class);
Given("^I enter my blog address \"([^\"]*)\" and go to Home page$",
(String url) -> { driver.get(url);}
);
When("^I click the search button$", () -> {
blogPage.clickSearchButton();
});
And("^I enter \"([^\"]*)\" in the input field$", (String keyword) -> {
blogPage.inputSearchWording(keyword);
});
And("^I click the first result of search$", () -> {
blogPage.clickFirstResultOfSearch();
});
Then("^I go to the article page with title containing \"([^\"]*)\"$",
(String keyword) -> {
assertContainsIngoreCase(blogPage.getArticleTitle(), keyword);
});
}
public static void assertContainsIngoreCase(String set, String subset) {
assertThat(set.toLowerCase(), containsString(subset.toLowerCase()));
}
}
可以看出来,我们这里采用了Java8中Lambda表达式的写法,并通过正则注入参数。
1
注意:java版本为1.8.0.71时,这种用法将会报这样的错误:java.lang.IllegalArgumentException: Wrong type at constant pool index;换成1.8.0.51后问题消失。