使用Selenium webdriver + specflow + c#+ Pageobject + pagefactory禁用浏览器javascript

时间:2014-11-13 20:13:07

标签: c# selenium selenium-webdriver specflow pageobjects

我想禁用firefox javascript并运行我的自动化测试。

我正在使用Specflow + c#和selenium。

我也在使用PageObject模式和Page factory。

我的功能文件是这样的:

Scenario: Search for item after disabling the javascript
Given I am a ACUST 
When I disable the javascript
And I have entered a text 'dress' string to search for that matches a product
Then The relevant search results for the 'dress' will be returned

我的代码如下:

using System;
using System.Collections.Generic;
using System.Collections;
using TechTalk.SpecFlow;
using NUnit.Framework;
using OpenQA.Selenium;
using Hof.Web.Tests.UIAutomation.Pages;
using Hof.Web.Tests.UIAutomation.Helper;
using Hof.Components.Common;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

  public class SearchSteps : Steps  

    {  
        private static readonly string Url = ConfigHelper.GetValue<string>("tset.Web.BaseUrl.QA3") + "/product";  
        public IWebDriver driver;  

        public SearchSteps()  
        {  
            WebBrowser.driverType = "MF";  
            driver = WebBrowser.Current;  
        }  


[Given(@"I am a ACUST")]
        public void GivenIamAAnonymousCustumer()
        {
            var hp = new HeaderPage(driver);
            GenericFunctions.NavigateToURL(Url);//In Future it will be changed to Homepage URL
            Console.WriteLine("Account Name:= " + hp.Lnk_SignInOrRegister.Text);
            Assert.IsTrue(hp.Lnk_SignInOrRegister.Text.Trim().Equals("Sign in or Register"),"Err! User is not anonymous. Please Log out");
        }

[When(@"I disable the javascript")]
public void WhenIDisableTheJavascript()
{
    driver.Close();  //closes previous browser session which has javascript enabled
    FirefoxProfile p = new FirefoxProfile();  
    p.SetPreference("javascript.enabled", false);  
    driver = new FirefoxDriver(p);  
    var hp = new HeaderPage(driver);  
    GenericFunctions.NavigateToURL(Url);  
}

[When(@"I have entered a text '(.*)' string to search for that matches a product")]
        public void WhenIHaveEnteredATextStringToSearchForThatMatchesAProduct(string strPrdToSearch)
        {
            var hp = new HeaderPage(driver);
            hp.searchForProduct(strPrdToSearch);
        }

[Then(@"The relevant search results for the '(.*)' will be returned")]
        public void ThenTheRelevantSearchResultsForTheWillBeReturned(string prdSearchToVerify)
        {
            var hofProductpage = new ProductPage(WebBrowser.Current);
            hofProductpage.verifyProductSearch(prdSearchToVerify);
        }

NavigateToURL的代码:

public static void NavigateToURL(string url , IWebDriver driver)  
    {  
        driver.Navigate().GoToUrl(url);  
    }  

浏览器打开,并且使用禁用javascript的形式导航URL,但接下来的步骤失败:

And I have entered a text 'dress' string to search for that matches a product
Then The relevant search results for the 'dress' will be returned

TearDown的代码:

[Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute()]
        public virtual void ScenarioTearDown()
        {
            testRunner.OnScenarioEnd();
        }

进一步

OnScenarioEnd()  invokes  void OnScenarioEnd();

我得到的错误是: System.InvalidOperationException:未指定会话ID
TestCleanup方法Hof.Web.Tests.UIAutomation.FeatureFiles.Feature.ScenarioTearDown抛出异常。 System.InvalidOperationException:System.InvalidOperationException:未指定会话ID。

1 个答案:

答案 0 :(得分:2)

问题在于,在导航方法中,它引用了不同的驱动程序实例。

注意导航方法中的_driver

您需要传递您声明的实例:

public static void NavigateToURL(string url, IWebDriver driver)  
{  
    driver.Navigate().GoToUrl(url);  
}