禁用浏览器的JavaScript与硒的webdriver + specflow + C#+ Page

2019-10-21 07:03发布

我想禁用Firefox的JavaScript和运行我的自动测试。

我使用Specflow + C#和硒。

我也使用PageObject模式和页面工厂。

我的特点文件变为这样:

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

浏览器打开,也该URL导航禁用了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

代码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。

Answer 1:

问题是,你的导航方法中,它指的是一个不同的驱动程序实例。

注意_driver在导航方法。

你需要在你声明的情况下通过:

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


文章来源: Disabling browser javascript with Selenium webdriver + specflow + c# + Pageobject + pagefactory