Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...