使用Selenium Webdriver + Specflow + C#+ PageObject + PageFactory禁用浏览器JavaScript [英] Disabling browser javascript with Selenium webdriver + specflow + c# + Pageobject + pagefactory

查看:124
本文介绍了使用Selenium Webdriver + Specflow + C#+ PageObject + PageFactory禁用浏览器JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to disable firefox javascript and run my automated test.

我正在使用Specflow + c#和硒.

I am using Specflow + c# and selenium.

我也在使用PageObject模式和Page工厂.

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

我的代码如下:

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的代码:

Code for NavigateToURL:

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

将打开浏览器,并且使用禁用了javascript的形式导航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

TearDown的代码:

Code to 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.

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.

推荐答案

问题是在您的navigation方法中,它引用了另一个驱动程序实例.

The problem is that within your navigate method, it refers to a different driver instance.

请注意该导航方法中的_driver.

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

这篇关于使用Selenium Webdriver + Specflow + C#+ PageObject + PageFactory禁用浏览器JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆