1. 程式人生 > >行為驅動:Cucumber + Selenium + Java(四) - 實現測試用例的參數化

行為驅動:Cucumber + Selenium + Java(四) - 實現測試用例的參數化

pac webdriver tab back condition ons 直接 wait feature

在上一篇中,我們介紹了Selenium + Cucumber + Java框架下的使用Tags對測試用例分組的實現方法,這一篇我們用數據表格來實現測試用例參數化。

4.1 什麽是用例參數化

  實際測試中,我們可能經常會去測試幾個類似的場景,或者一些大同小異的測試點。

  比如說,測試用戶登錄的過程中,為了滿足測試的完整性,我們會要通過等價類劃分等基本方法,去測試登錄系統對於有效類--正確的用戶名密碼;和無效類--錯誤的用戶名密碼等場景。

  這一些場景的前序步驟都很類似,如果我們對於每一個這樣的用例都從頭到尾按照我們之前的例子那樣,從gherkin的用例編寫,到java代碼的解釋這麽寫下來,代碼量會很多而且沒有必要。

  所以我們就要想,對於這樣的測試,我們能不能將他們集合在一起,用參數化或者數據驅動的方式去實現?

4.2 Cucumber的數據驅動

  我們直接去我們的代碼裏新建一個test.feature特性文件,如果eclipse的cucumber插件正確安裝的話,那麽不但這個文件會有獨特的外觀,你還會發現文件中自帶了對於gherkin用例的一個模板:

  技術分享圖片

#Author: [email protected]
#Keywords Summary :
#Feature: List of scenarios.
#Scenario: Business rule through list of steps with arguments.
#Given: Some precondition step
#When: Some key actions
#Then: To observe outcomes or validation
#And,But: To enumerate more Given,When,Then steps
#Scenario Outline: List of steps 
for data-driven as an Examples and <placeholder> #Examples: Container for s table #Background: List of steps run before each of the scenarios #""" (Doc Strings) #| (Data Tables) #@ (Tags/Labels):To group Scenarios #<> (placeholder) #"" ## (Comments) #Sample Feature Definition Template @tag Feature: Title of your feature I want to use
this template for my feature file @tag1 Scenario: Title of your scenario Given I want to write a step with precondition And some other precondition When I complete action And some other action And yet another action Then I validate the outcomes And check more outcomes @tag2 Scenario Outline: Title of your scenario outline Given I want to write a step with <name> When I check for the <value> in step Then I verify the <status> in step Examples: | name | value | status | | name1 | 5 | success | | name2 | 7 | Fail |

  我們可以看到,@tag2這個標簽標記的測試用例用了一種我們之前沒有用過的格式來組織用例,並且下面還有一個Examples表格。這就是cucumber自帶的數據驅動表格。

  下面我們就用這種形式來實現數據驅動和參數化。

4.3 編寫參數化的feature特性文件

  我們來實現之前提到的登錄測試。

  先在features文件夾底下新建一個名為testLogin.feature的特性文件。文件中寫入如下gherkin代碼:

@tag
Feature: Test login feature of lemfix
  I want to use this case to test login functionality

  @tag1
  Scenario Outline: Test login feature of lemfix
    Given I navigated to lemfix site
    When I input <username>” and “<password>” to login
    Then I verify login result <result>”

    Examples: 
      | username        | password  | result  |
      | vincent20181030 | password1 | success |
      | vincent20000000 | password1 | fail    |

  這裏我們在一個用例裏,用數據表格的方式,分別想去測試用戶登錄成功/失敗的案例。數據表格的第一行是存在的用戶名和密碼,預計登錄成功;而第二行的用戶是不存在,預計登錄失敗。

4.4 將feature進行步驟定義

  在stepDefinitions文件夾下新建TestLogin.java,寫入如下代碼:

package stepDefinitions;

import static org.testng.Assert.assertTrue;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class TestLemfix {
    WebDriver driver;
    
    
    @Given("^I navigated to lemfix site$")
    public void i_navigated_to_lemfix_site() throws Throwable {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        
        driver.get("http://fm.lemfix.com");
    }

    @When("^I input \"([^\"]*)\" and \"([^\"]*)\" to login$")
    public void i_input_vincent_and_password_to_login(String us_name, String us_psswd) throws Throwable {
        WebElement loginTop;
        WebElement username;
        WebElement password;
        WebElement loginBTN;
        
        loginTop = driver.findElement(By.xpath("html/body/div[1]/div/div[3]/ul/li[2]/a"));
        loginTop.click();
        
        username = driver.findElement(By.id("user_login"));
        password = driver.findElement(By.id("user_password"));
        loginBTN = driver.findElement(By.xpath(".//*[@id=‘new_user‘]/div[4]/input"));
        
        username.sendKeys(us_name);
        password.sendKeys(us_psswd);
        loginBTN.click();
        
        Thread.sleep(1000);
    }

    @Then("^I verify login \"([^\"]*)\"")
    public void i_verify_login_result(String rs) throws Throwable {
        String title = driver.getTitle();
        String result;
        if(title.contains("登錄")){
            result = "fail";
        }else if(title.equals("Lemfix")){
            result = "success";
        }else{
            result = null;
        }
        System.out.println(title);
        System.out.println("result=" + result);
        Assert.assertTrue(result.equals(rs));
        
        Thread.sleep(1000);
        driver.quit();
    }
}

  註意在java代碼中,解釋方法也同樣對應的引入參數,如:

技術分享圖片

技術分享圖片

  運行runner類,測試通過,到此為止我們就實現了用參數化/數據驅動的形式來實現cucumber測試。

  下一篇我們改用maven對cucumber環境進行配置並實現測試報告輸出。

行為驅動:Cucumber + Selenium + Java(四) - 實現測試用例的參數化