Cucumber Selenium using Excel File as Data Table

2020-02-06 10:54发布

I am using Cucumber-Selenium and Excel as my Data file, my question is how can I run my feature file multiple time based on the data I have on the Excel. For Example I have 10 rows of data in Excel and wanted to run it one by one, after the first row of data it will move to the next row and execute it.

Feature File: Scenario: Login

Given I open the browser and access this URL
When I enter the "<Username>" and "<Password>"
Then I am able to login

Step Definition: public class Login {

 WebDriver driver = null;
 String url;        


@Given("^I open the browser and access this URL$")
public void navigateToUrl() throws Throwable{

    System.setProperty("webdriver.chrome.driver", "");
    driver = new ChromeDriver();
    url = DataTable.getDataTableValue(0, 2, 2);
    driver.get(url);
    driver.manage().window().maximize();
}

@When("^I enter the \"([^\"]*)\" and \"([^\"]*)\"$")
public void enterCredentials(String userName, String password ) throws Throwable {

    userName = DataTable.getDataTableValue(0, 1, 1);
    password = DataTable.getDataTableValue(0, 1, 2);

    driver.findElement(By.id("username")).sendKeys(userName);
    driver.findElement(By.id("password")).sendKeys(password);   
}

@Then("^I am able to login$")
public void clickLoginButton() throws Throwable {
    driver.findElement(By.id("Login")).click();
}

}

Here is my Data Table(Excel File)

|ID | UserName | Password

|ID1 |username1 |password1

|ID2 | username2 | password2

|ID3 | username3 | password3

|ID4 | username4 | password4

1条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-06 11:18

If you want to iterate of the content in an Excel sheet you need to implement that in code in the step definition. There is no support for doing it in Gherkin.

Apache POI may be an option when implementing the iteration.

It is important to understand that the purpose of Behaviour-Driven Development, BDD, is communication. Gherkin is one way of communicating. The Gherkin scenarios can be read and understood by almost anyone understanding the problem.

If you have some of the truth in Gherkin and some in Excel you will end up in a situation where you don't use Cucumber and Gherkin for communication but rather as a test tool. This may be ok. But if you use Cucumber as a test tool, there are other tools that may be easier to use. JUnit is one of them.

查看更多
登录 后发表回答