问题
I want to disable firefox javascript and run my automated test.
I am using Specflow + c# and selenium.
I am also using PageObject pattern and Page factory.
My feature file goes this way :
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
My code is as follows :
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);
}
Code for NavigateToURL:
public static void NavigateToURL(string url , IWebDriver driver)
{
driver.Navigate().GoToUrl(url);
}
The browser opens and also the URL is navigated with javascript disabled form, but the next steps are failing :
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
Code to TearDown :
[Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute()]
public virtual void ScenarioTearDown()
{
testRunner.OnScenarioEnd();
}
And further
OnScenarioEnd() invokes void OnScenarioEnd();
error i get is :
System.InvalidOperationException: No session ID specified
TestCleanup method Hof.Web.Tests.UIAutomation.FeatureFiles.Feature.ScenarioTearDown threw exception. System.InvalidOperationException: System.InvalidOperationException: No session ID specified.
回答1:
The problem is that within your navigate method, it refers to a different driver instance.
Notice the _driver
in that navigate method.
You will need to pass in the instance you've declared:
public static void NavigateToURL(string url, IWebDriver driver)
{
driver.Navigate().GoToUrl(url);
}
来源:https://stackoverflow.com/questions/26917530/disabling-browser-javascript-with-selenium-webdriver-specflow-c-sharp-page