Tích hợp cucumber với selenium

Trong phần trước, tôi đã thảo luận công cụ Cucumber, cách cài đặt, sử dụng và các tính năng cơ bản: https://viblo.asia/p/huong-dan-su-dung-tool-automation-cucumber-E375zb2j5GW .

Trong phần tiếp theo này tôi sẽ giới thiệu về các tích hợp công cụ test cucumber với selenium webdriver thần thánh.

Thiết lập dự án Cucumber với Maven trong java.

  1. Setup project cucumber trong maven Bước #1: Tạo mới 1 Maven Project:

Right Click -> New -> Others -> Maven -> Maven Project -> Next

Bước #2: Project sẽ giống như sau:

Bước #3: Thêm dependencies vào file pom.xml


<dependencies>
<dependency>

      <groupId>info.cukes</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>1.0.2</version>
      <scope>test</scope>

  </dependency>
<dependency>

     <groupId>info.cukes</groupId>
     <artifactId>cucumber-junit</artifactId>
     <version>1.0.2</version>
     <scope>test</scope>

</dependency>
<dependency>

     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.10</version>
     <scope>test</scope>

</dependency>
</dependencies>

Bước #4: Tạo mới 1 sample.feature file dưới folder src/test/resources.

@smokeTest Feature: To test my cucumber test is running I want to run a sample feature file.

Scenario: cucumber setup

Given sample feature file is ready When I run the feature file Then run should be successful

Bước #5: Tạo mới 1 class dưới folder src/test/java :

public class stepDefinition {

       @Given("^sample feature file is ready$")

       public void givenStatment(){

              System.out.println("Given statement executed successfully");

       }

       @When("^I run the feature file$")

       public void whenStatement(){

              System.out.println("When statement execueted successfully");

       }

       @Then("^run should be successful$")

       public void thenStatment(){

              System.out.println("Then statement executed successfully");

       }

}

Bước #6: Tạo mới 1 Junit runner để chạy test:

@RunWith(Cucumber.class)

@Cucumber.Options(format={"pretty","html:reports/test-report"},tags= "@smokeTest")

public class CucumberRunner {

}

Bước #7: Junit Result và Test Report:

  1. Cucumber và Selenium WebDriver:
Feature: Login Feature File
@selenium
Scenario: Login scenario test for Gmail

Given navigate to gmail page
When user logged in using username as “userA” and password as “password”
Then home page should be displayed

Thiếp lập WebDriver trong Cucumber stepDefinitions:

public class stepDefinition {

WebDriver dr;

@Given("^navigate to gmail page$")

public void navigate(){

       dr=new FirefoxDriver();
       dr.get("http://www.gmail.com");         

       }

@When ("^user logged in using username as \"(.*)\" and password as \"(.*)\"$")

public void login(String username,String password){

       dr.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
       dr.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
       dr.findElement(By.xpath("//*[@id='signIn']")).click();
       dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

       }

@Then("^home page should be displayed$")

public void verifySuccessful(){
      String expectedText="Gmail";
      String actualText=         dr.findElement(By.xpath("//*[@id='gbq1']/div/a/span")).getText();
      Assert.assertTrue("Login not successful",expectedText.equals(actualText));
      }
}