Specflow - 完成所有测试后关闭浏览器

时间:2017-12-22 19:01:20

标签: c# google-chrome selenium-webdriver bdd specflow

我想按照功能示例逐个执行两个测试。 它工作但我不知道如何在第二次测试完成时关闭浏览器。

我已尝试过AfterFeature钩子,但它没有用。我刚刚登录。我想暂时关闭浏览器

这是代码

public class LoginSteps
    {
        [BeforeScenario("LoginProperly")]
        public static void BeforeScenario()
        {
            Browser.Initialize();
        }

        [AfterScenario("LoginProperly")]
        public static void AfterScenario()
        {

            Browser.ClearCache();
        }

        [AfterFeature("LoginProperly")]
        public static void AfterFeature()
        {
          Browser.Quit();

        }


        [Given(@"I navigate to login page")]
        public void GivenINavigateToLoginPage()
        {
            Browser.Wait();
            Pages.Navigation.GoToSignIn();

        }

        [Given(@"I have completed the form with '(.*)' and '(.*)'")]
        public void GivenIHaveCompletedTheFormWithAnd(string email, string password)
        {
            Pages.Login.LogIn(email, password);
        }

        [When(@"I press Submit")]
        public void WhenIPressSubmit()
        {
            Pages.Login.ClickLogin();
        }

        [Then(@"I should see my account panel")]
        public void ThenIShouldSeeMyAccountPanel()
        {
            Pages.Account.IsAt();
        }
    }
}
  

功能文件

Feature: Login
    In order to use my account
    As an application user
    I want to be able to log in and log out

@LoginProperly
Scenario Outline: Login with correct Email adress and Password
    Given I navigate to login page
    And I have completed the form with '<email>' and '<password>'
    When I press Submit
    Then I should see my account panel

    Examples: 

    |     email         |  password  |
    | tescik@gmail.com  |   11111    |
    | tescik2@gmail.com |   22222    |

2 个答案:

答案 0 :(得分:5)

您是否尝试过[AfterTestRun]

    [AfterTestRun]
    public static void AfterWebFeature()
    {
        Browser.Quit();
        Brrowser.Dispose();
    }

我在IWebDriver上调用quit后总是调用dispose。

答案 1 :(得分:-3)

获取浏览器进程然后将其终止。将此代码添加到测试用例的退出方法中。

Process.GetProcessesByName("your browser name", "your pc name").First().Kill();