使用 Selenium webdriver + specflow + c# + Pageobject + pagefac
本文关键字:Pageobject pagefac specflow webdriver 使用 Selenium | 更新日期: 2023-09-27 18:30:22
我想禁用 Firefox javascript 并运行我的自动测试。
我正在使用Specflow + c#和Selenium。
我也在使用PageObject模式和Page工厂。
我的功能文件是这样的:
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);
}
导航到URL的代码:
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
要拆除的代码:
[Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute()]
public virtual void ScenarioTearDown()
{
testRunner.OnScenarioEnd();
}
还有更进一步
OnScenarioEnd() invokes void OnScenarioEnd();
我得到的错误是:系统无效操作异常:未指定
会话 IDTestCleanup 方法 Hof.Web.Tests.UIAutomation.FeatureFiles.Feature.ScenarioTearDown 抛出异常。System.InvalidOperationException: System.InvalidOperationException: 未指定会话 ID。
问题是在导航方法中,它引用了不同的驱动程序实例。
请注意该导航方法中的_driver
。
您需要传入已声明的实例:
public static void NavigateToURL(string url, IWebDriver driver)
{
driver.Navigate().GoToUrl(url);
}