+ feature : read requirement +scenario : testing situation,including + Given/ + when/ + then
Feature:用來描述咱們須要測試的功能
Scenario: 用來描述測試場景
Given: 前置條件
When: 描述測試步驟
Then: 斷言web
features:用來存放天然語言的用例文件
step_definitions:用來存放java代碼實現的測試用例
jars:cucumber-jvm的相關jar包都放在這裏
implementation:存放被測試的代碼,也就是項目的實現文件chrome
https://docs.cucumber.io/guides/10-minute-tutorial/
https://docs.cucumber.io/guides/browser-automation/瀏覽器
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>gherkin</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- Cucumber Tag related jars (start) --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server-standalone</artifactId> <version>3.12.0</version> </dependency>
Feature: Is it Friday yet? Everybody wants to know when it's Friday Scenario: Sunday isn't Friday Given today is Sunday When I ask whether it's Friday yet Then I should be told "Nope"
@Given("^today is Sunday$") public void today_is_Sunday() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @When("^I ask whether it's Friday yet$") public void i_ask_whether_it_s_Friday_yet() throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); } @Then("^I should be told \"([^\"]*)\"$") public void i_should_be_told(String arg1) throws Throwable { // Write code here that turns the phrase above into concrete actions throw new PendingException(); }
在com/cucumber/hellocucumber目錄下面編寫com/cucumber/hellocucumber。java類,用junit執行run一下。會自動去讀取app
@RunWith(Cucumber.class) @CucumberOptions(features={"src/main/resources/features"}) public class RunCucumberTest { }
通常用junit執行run一下,都會發現不少PendingException這時候就須要編寫代碼,使測試經過。jvm
public class Stepdefs { private String today; private String actualAnswer; @Given("^today is Sunday$") public void today_is_Sunday() throws Throwable { this.today = "Sunday"; } @When("^I ask whether it's Friday yet$") public void i_ask_whether_it_s_Friday_yet() throws Throwable { this.actualAnswer = IsItFriday.isItFriday(today); } @Then("^I should be told \"([^\"]*)\"$") public void i_should_be_told(String expectedAnswer) throws Throwable { assertEquals(expectedAnswer, actualAnswer); } } IsItFriday。java 以下: public class IsItFriday { static String isItFriday(String today) { if (today.equals("Friday")) { return "TGIF"; } return "Nope"; } }
Feature: Is it Friday yet? Everybody wants to know when it's Friday Scenario Outline: Today is or is not Friday Given today is "<day>" When I ask whether it's Friday yet Then I should be told "<answer>" Examples: | day | answer | | Friday | TGIF | | Sunday | Nope | | anything else! | Nope |
@Given("^today is \"([^\"]*)\"$") public void today_is(String today) throws Throwable { // Write code here that turns the phrase above into concrete actions this.today = today; //throw new PendingException(); } @When("^I ask whether it's Friday yet$") public void i_ask_whether_it_s_Friday_yet() throws Throwable { this.actualAnswer = IsItFriday.isItFriday(today); } @Then("^I should be told \"([^\"]*)\"$") public void i_should_be_told(String expectedAnswer) throws Throwable { assertEquals(expectedAnswer, actualAnswer); }
Cucumber自己不是一個瀏覽器自動化測試工具,可是經過和Selenium WebDriver結合能夠完成一些簡單的瀏覽器自動化測試工做。ide
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server-standalone</artifactId> <version>3.12.0</version> </dependency>
若是你使用的時候chrom瀏覽器,那麼須要在系統屬性裏面設置chromedriver.exe的路徑工具
System.setProperty("webdriver.chrome.driver", "C:/swdtools/Chrome/chromedriver.exe");
Feature: search cheese on google Scenario: Finding some cheese Given I am on the Baidu search page When I search for "Cheese!" Then the page title should start with "cheese"
public class ExampleSteps { private final WebDriver driver = WebDriverFactory.createWebDriver(); @Given("^I am on the Baidu search page$") public void I_visit_google() { driver.get("https:\\www.baidu.com"); } @When("^I search for \"(.*)\"$") public void search_for(String query) { WebElement element = driver.findElement(By.id("kw")); // Enter something to search for element.sendKeys(query); // Now submit the form. WebDriver will find the form for us from the element element.submit(); } @Then("^the page title should start with \"(.*)\"$") public void checkTitle(String titleStartsWith) { // Google's search is rendered dynamically with JavaScript // Wait for the page to load timeout after ten seconds new WebDriverWait(driver,10L).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese"); // Should see: "cheese! -Google Search" } }); } @After() public void closeBrowser() { driver.quit(); } }
@RunWith(Cucumber.class)
@CucumberOptions(features={"src/main/resources/features"})
public class RunCucumberTest {測試
}ui