简体   繁体   中英

Read data from excel and write to feature file using specflow

I am currently using specflow for my unit testing, however my complexity is in the fact that I want to read(retrieve) my input data from an excel spread sheet, before running my tests.

For example:

When I get a customers product in XXXXX with product number 1234567

Then The following product details are returned

| Model    | Product Type | SerialNumber |
| blah     | zzzzz        | 12345        |   

So I want to basically be able to change the input data by reading from execel file and writing to my feature file. Has this been done before and is it possible? Do I need to install some sort of plugin?

You can implement your own step definition that reads up the data from an Excel sheet. The simplest way to do this is passing the Excel sheet name as a step parameter, and then implementing the code to read up the data from Excel. To learn more about writing step definitions, start here: http://www.specflow.org/documentation/Step-Definitions/

Alternatively, you can just install SpecFlow+Excel which provides that out of the box for you in many different ways (eg just linking Excel files to Gherkin scenarios that are written in plain text feature files, or writing and executing the entire Gherkin scenario in Excel). SpecFlow+Excel also supports specifying just the scenario outline example rows in (different) Excel files, which is not just reading up data from Excel in step definition, but also influencing the scenario generation and execution with Excel. More info about SpecFlow+Excel can be found here: http://www.specflow.org/plus/Excel/

Disclaimer: SpecFlow+Excel is a commercial plugin that builds on SpecFlow, maintained by the SpecFlow team. You can try it out free of charge.

Create Feature File as Below:

Feature: Signin
Test the Login functionality 
Will verify the credentials working as expected
@mytag
Scenario Outline: Signin_Functionality
Given Present at Signin Page
And entered <username> and <password>
When I press signin button
Then verify that login is successfull

@source:LoginData.xlsx
Examples: 
| username | password |

(Note: Create Excelsheet named as LoginData.xlsx where Feature File Located)

Write TEST method in page object File as Below:

   public void login(string username, string password){
       Username.SendKeys(username);
       Passwrd.SendKeys(password);
   }

    public void ClickLogin(){
        btnLogin.Click(); 
     }

Call Above mentioned Method in Step file as Below:

 [Given(@"entered (.*) and (.*)")]
 public void GivenEnteredAnd(string username, string password)
 {
    var signin = new LoginPage(driver);
    signin.login(username, password);
 }

[When(@"I press signin button")]
public void WhenIPressSigninButton()
{
   var submit = new LoginPage(driver);
   submit.ClickLogin();   
}

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