简体   繁体   English

从Excel读取数据并使用Specflow写入功能文件

[英]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. 我目前正在使用specflow进行单元测试,但是我的复杂性在于,在运行测试之前,我想从excel电子表格读取(检索)我的输入数据。

For example: 例如:

When I get a customers product in XXXXX with product number 1234567 当我在XXXXX获得客户产品编号为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. 因此,我希望基本上能够通过从execel文件读取并写入我的功能文件来更改输入数据。 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. 您可以实现自己的步骤定义,该步骤定义从Excel工作表中读取数据。 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. 最简单的方法是将Excel工作表名称作为步骤参数传递,然后实现代码以从Excel读取数据。 To learn more about writing step definitions, start here: http://www.specflow.org/documentation/Step-Definitions/ 要了解有关编写步骤定义的更多信息,请从此处开始: 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,它以多种不同的方式为您提供即用即用的功能(例如,仅将Excel文件链接到以纯文本功能文件编写的Gherkin方案,或在Excel中编写和执行整个Gherkin方案) )。 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. SpecFlow + Excel还支持仅在(不同的)Excel文件中指定方案大纲示例行,这不仅是在步骤定义中从Excel读取数据,而且还会影响方案的生成和使用Excel的执行。 More info about SpecFlow+Excel can be found here: http://www.specflow.org/plus/Excel/ 有关SpecFlow + Excel的更多信息,请参见: http : //www.specflow.org/plus/Excel/

Disclaimer: SpecFlow+Excel is a commercial plugin that builds on SpecFlow, maintained by the SpecFlow team. 免责声明:SpecFlow + Excel是基于SpecFlow的商业插件,由SpecFlow团队维护。 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) (注意:在功能文件所在的位置创建名为LoginData.xlsx的Excelsheet)

Write TEST method in page object File as Below: 在页面对象文件中编写TEST方法,如下所示:

   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();   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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