cucumber & selenium & bddtest

1.Cucumber介绍

+ feature : read requirement
    +scenario : testing situation,including 
        + Given/ 
        + when/ 
        + then

Feature:用来描述我们需要测试的功能
Scenario: 用来描述测试场景
Given: 前置条件
When: 描述测试步骤
Then: 断言

features:用来存放自然语言的用例文件
step_definitions:用来存放java代码实现的测试用例
jars:cucumber-jvm的相关jar包都放在这里
implementation:存放被测试的代码,也就是项目的实现文件

https://docs.cucumber.io/guides/10-minute-tutorial/
https://docs.cucumber.io/guides/browser-automation/

2。使用步骤

2.1 引入依赖

<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>

2.2新建test.future文件

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"

2.3在resource目录下创建cucumber.bat,执行bat,cucumber会读取test2.feature文件的内容,生成step定义代码,把这个生成的内容,粘贴进去自己定义的java类里com/cucumber/hellocucumber/Stepdefs.java:

@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();
}

2。4编写测试类

在com/cucumber/hellocucumber目录下面编写com/cucumber/hellocucumber。java类,用junit执行run一下。会自动去读取

@RunWith(Cucumber.class)
@CucumberOptions(features={"src/main/resources/features"})
public class RunCucumberTest {
    
}

一般用junit执行run一下,都会发现很多PendingException这时候就需要编写代码,使测试通过。

2.5编写代码使TodoSteps.java方法执行通过

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";
        }
}

3.Using variables and examples

3.1 use

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 |

3.2 define the step class

    @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);
    }

4。Browser Automation

Cucumber本身不是一个浏览器自动化测试工具,但是通过和Selenium WebDriver结合可以完成一些简单的浏览器自动化测试工作。

4.1 依赖

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server-standalone</artifactId>
            <version>3.12.0</version>
        </dependency>

4.2prepare

如果你使用的时候chrom浏览器,那么需要在系统属性里面设置chromedriver.exe的路径

System.setProperty("webdriver.chrome.driver", "C:/swdtools/Chrome/chromedriver.exe");

4.3定义feature文件web.feature

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"

4.4定义step类

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();
         }
}

4.5编写测试类

@RunWith(Cucumber.class)
@CucumberOptions(features={"src/main/resources/features"})
public class RunCucumberTest {

}

猜你喜欢

转载自www.cnblogs.com/codetree/p/10362675.html