【问题标题】:Disabling browser javascript with Selenium webdriver + specflow + c# + Pageobject + pagefactory使用 Selenium webdriver + specflow + c# + Pageobject + pagefactory 禁用浏览器 javascript
【发布时间】:2015-01-11 02:36:51
【问题描述】:

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

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

我也在使用 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

要拆解的代码:

[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。

【问题讨论】:

  • 所以问题出在GenericFunctions.NavigateToURL(Url);。里面的代码是什么?
  • 如果您想回答您的问题,您应该在GenericFunctions.NavigateToURL(Url); 中提供代码,以便对其中发生的事情进行一些推理
  • @SamHolder :我已将代码添加到 NavigateToURL
  • @Arran:我已将代码添加到 NavigateToURL
  • @ArpanBuch 请发布Hof.Web.Tests.UIAutomation.FeatureFiles.Feature.ScenarioTearDown 的内容,因为这似乎是问题所在

标签: c# selenium selenium-webdriver specflow pageobjects


【解决方案1】:

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

注意导航方法中的_driver

你需要传入你声明的实例:

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

【讨论】:

  • +1 或将静态_driver 实例设置为新实例
  • @Arran 现在正在导航 url,但下一步它会抛出错误。下一个场景步骤不会发生。看到我的问题,我已经更新了完整的代码.....谢谢..
  • 已经更新了代码并添加了所有 4 个步骤,给出了时间和它们的代码..请看看!!
  • 2 个浏览器实例正在打开。我认为一个具有 firefox 配置文件和另一个新的 firefoxdriver。但不确定,为什么。
  • 您已经完成了一个步骤,即关闭一个浏览器并重新打开另一个浏览器。所以这是意料之中的,不是......?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多