完成Specflow中的第一个场景后,浏览器关闭

时间:2017-11-11 20:24:58

标签: c# selenium selenium-webdriver specflow

在我的Specflow项目中,我在我的功能文件中定义了两个场景。但是当我执行我的程序时,浏览器会在第一个场景后关闭。然后它没有通过测试,因为没有与远程服务器的连接。

我该如何解决这个问题?

Feature: Check functionality of dashboard

@SmokeTest
@Browser:Chrome
Scenario: Check dashboard page is loaded
    Given I have navigated to application
    And I typed emailid and password
    | Email           | Password |
    | qa02@example.com | test1234 |
    When I clicked login button
    Then I should see dashboard

Scenario: Check trading partners page is loaded
    Given I am on Trading Partner page
    When I click on trading partners link
    Then I should see trading partners page

以下是我的Package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Autofac" version="3.1.1" targetFramework="net461" />
  <package id="Autofac.Configuration" version="3.1.0" targetFramework="net461" />
  <package id="Baseclass.Contrib.SpecFlow.Selenium.NUnit" version="1.3.1" targetFramework="net461" />
  <package id="ExcelDataReader" version="2.1.2.3" targetFramework="net461" />
  <package id="ImpromptuInterface" version="5.6.2" targetFramework="net461" />
  <package id="MSTest.TestAdapter" version="1.1.18" targetFramework="net461" />
  <package id="MSTest.TestFramework" version="1.1.18" targetFramework="net461" />
  <package id="NUnit" version="2.6.0.12054" targetFramework="net461" />
  <package id="NUnitTestAdapter.WithFramework" version="2.0.0" targetFramework="net461" />
  <package id="Selenium.Chrome.WebDriver" version="2.30" targetFramework="net461" />
  <package id="Selenium.Support" version="3.4.0" targetFramework="net461" />
  <package id="Selenium.WebDriver" version="3.4.0" targetFramework="net461" />
  <package id="SharpZipLib" version="0.86.0" targetFramework="net461" />
  <package id="SpecFlow" version="1.9.0" targetFramework="net461" />
  <package id="SpecFlow.Assist.Dynamic" version="1.0.2" targetFramework="net461" />
</packages>

请参阅相关的步骤文件。

namespace DX2Functional.Steps
{
    [Binding]
    class DashboardSteps
    {


        [Given(@"I have navigated to application")]
        public void GivenIHaveNavigatedToLoginUrl()
        {
            Browser.Current.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);
            CommonMethods.driver = Browser.Current;
            PropertiesCollection.currentPage = new Login();
        }

        [Given(@"I typed emailid and password")]
        public void GivenITypedEmailidAndPassword(Table table)
        {
            dynamic tableDetails = table.CreateDynamicInstance();
            string email = tableDetails.Email;
            Console.Write("Email is " + email);
            string passowrd = tableDetails.Password;
            Console.Write("password is " + passowrd);

            PropertiesCollection.currentPage.As<Login>().enterUerEmailAndPassword(email, passowrd);
        }

        [When(@"I clicked login button")]
        public void WhenIClickedLoginButton()
        {
            PropertiesCollection.currentPage = PropertiesCollection.currentPage.As<Login>().clickLoginButton();
        }

        [Then(@"I should see dashboard")]
        public void ThenIShouldSeeDashboard()
        {
            PropertiesCollection.currentPage.As<Dashboard>().IsloadedIn();
        }


        [Given(@"I am on Trading Partner page")]
        public void GivenIamOnTradingPartnerPage()
        {
            PropertiesCollection.currentPage.As<Dashboard>().IsloadedIn();
        }

        [When(@"I click on trading partners link")]
        public void WhenIClickOnTradingPartnersLink()
        {
            PropertiesCollection.currentPage.As<Dashboard>().clickTradingPrtnersLink();
        }

        [Then(@"I should see trading partners page")]
        public void ThenIShouldSeeTradingPartnersPage()
        {
            PropertiesCollection.currentPage.As<TradingPartners>().IsTPListLoaded();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是在功能文件的第一部分中使用Background而不是Scenario来登录用户。

例如:

Feature: Check functionality of dashboard

@SmokeTest
@Browser:Chrome
Background: 
    Given I have navigated to application
    And I typed emailid and password
    | Email           | Password |
    | qa02@example.com | test1234 |
    When I clicked login button

Scenario: Check dashboard page is loaded    
    Then I should see dashboard

Scenario: Check trading partners page is loaded
    Given I am on Trading Partner page
    When I click on trading partners link
    Then I should see trading partners page