简体   繁体   中英

Cucumber Selenium using Excel File as Data Table

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

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.

In case it helps others:

I my case this was very useful, as for each Scenario step I had to load Excel data (data from multiple rows having same group ID) in order to perform further validations. Like this the Cucumber feature file was a bit cleaner while the Excel had all the details under the hood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM