在Specflow&中使用场景大纲硒C#

时间:2015-08-28 11:03:05

标签: c# selenium specflow

我正在尝试使用Specflow中的场景大纲,它将根据我拥有的示例数量生成测试,然后我可以通过C#绑定使用selenium编写。所以我创建了以下功能:

Scenario Outline: Login
Given I have navigated to the Login Page
When I enter a valid '<Login>'
And I enter the correct '<Password>'
And I press the Login CTA
Then I am logged into the Home Page

Examples: 
| Login  | Password   |
| LoginA | passwordA  | 
| LoginB | passwordB  |

当我生成步骤定义时,我得到以下内容:

    [When(@"I enter a valid '(.*)'")]
    public void WhenIEnterAValid(string p0)
    {
        ScenarioContext.Current.Pending();
    }

    [When(@"I enter the correct '(.*)'")]
    public void WhenIEnterTheCorrect(string p0)
    {
        ScenarioContext.Current.Pending();
    }

我已将它们放入SpecFlow步骤定义文件

我还获得2个名为的测试

登录(“LoginA”,“passwordA”,null)

登录(“LoginB”,“passwordB”,null)

到目前为止一切顺利(我认为)。接下来的步骤是编写代码以完成步骤定义,以便每个测试都运行并使用相关数据。这是我坚持的一点。

我知道,例如,如果数据位于我可以从表中调用的标准场景中的表中,那么像 -

这样的功能
Scenario: Login
Given I have navigated to the Login Page
When I enter a valid Login
| Login   |
| loginA  |
And I enter the correct Password
| Password   |
| passwordA |
And I press the Login CTA
Then I am logged into the Home Page

可以满足以下代码:

 public void WhenIEnterAValidLogin(Table table)
    {
        IWebElement loginField = WebBrowser.Current.FindElement(By.Name("loginBox"));
        string loginText = table.Rows[0]["Login"].ToString();
        usernameField.SendKeys(loginText);
    }

但基本上我不知道如何编写代码,以便它在“Examples”表中查找并获取每个测试的相关数据。这是可能的还是我必须为每个测试单独编写代码,即我输入“loginA”的步骤和我输入“loginB”的步骤?我环顾网络,但没有找到帮助我的例子。

提前感谢您的帮助!如果我没有说清楚或者是在做一些基本的错误,请告诉我,因为我是新手来堆叠溢出,这是我的第一篇文章。

2 个答案:

答案 0 :(得分:2)

我认为你的想法太深入了。如果您执行以下操作:

Scenario Outline: Login
Given I have navigated to the Login Page
When I enter a valid '<Login>'
And I enter the correct '<Password>'
And I press the Login CTA
Then I am logged into the Home Page

Examples: 
| Login  | Password   |
| LoginA | passwordA  | 
| LoginB | passwordB  |

您实际上是在做以下两种情况:

Scenario: Login_LoginA
Given I have navigated to the Login Page
When I enter a valid 'LoginA'
And I enter the correct 'PasswordA'
And I press the Login CTA
Then I am logged into the Home Page

Scenario: Login_LoginB
Given I have navigated to the Login Page
When I enter a valid 'LoginB'
And I enter the correct 'PasswordB'
And I press the Login CTA
Then I am logged into the Home Page

步骤代码被重复使用 场景大纲是一种很好的方法,可以在短时间内创建大量场景,只处理不同的数据。

您对创建的两个测试是正确的,这使得它在testrunner中更易于管理。这两个测试是实际TestMethods,它调用名为Login的内部方法:

   // this method is created to be called from a scenario outline
   // this contains the steps that one iteration of the outline should take.
   public virtual void Login(string login, string password, string[] exampleTags)
    {
        TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Login", exampleTags);
        #line 13
        this.ScenarioSetup(scenarioInfo);
        #line 14
        testRunner.Given("I have navigated to the Login Page", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
        #line 15
        testRunner.When(string.Format("I enter a valid \'{0}\'", login), ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
        #line 16
        testRunner.And(string.Format("I enter the correct \'{0}\'", password), ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
        #line 17
        testRunner.And("I press the Login CTA", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
        #line 18
        testRunner.Then("I am logged into the Home Page", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
        #line hidden
        this.ScenarioCleanup();
    }

    [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
    [Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("Login")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("VariantName", "LoginA")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Login", "LoginA")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Password", "passwordA")]
    public virtual void Login_LoginA()
    {
        // Calling the inner method Login
        this.Login("LoginA", "passwordA", ((string[])(null)));
    }

    [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
    [Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("Login")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("VariantName", "LoginB")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Login", "LoginB")]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("Parameter:Password", "passwordB")]
    public virtual void Login_LoginB()
    {
        this.Login("LoginB", "passwordB", ((string[])(null)));
    }

答案 1 :(得分:0)

您可以像这样访问堆栈跟踪。尝试和测试。您不仅限于一个属性名称。

# Set up total:
total = 0

# Keep asking for numbers until we answer 'NO':
while True:
    # Ask user if they want to continue
    cont = input("Would you like to enter another number: ").upper()

    # If yes, get a number and add it to the total
    if cont == "Y" or cont == "YES":
        num = float(input("Please enter number: \n"))
        total += num
    # If no, print the total and break out of the while loop
    elif cont == "N" or cont == "NO":
        print("The total of the numbers is", total)
        break
    # Otherwise, try again.
    else:
        print("Invalid response. Please enter Y or N")