【问题标题】:How to setup a selenium solution from scratch如何从头开始设置硒解决方案
【发布时间】:2014-07-27 07:38:03
【问题描述】:

我正在从头开始创建一个新的 Selenium 解决方案,并且遇到了一个我正在尝试解决的错误,如果有人可以提供帮助的话。

首先我有一个使用 MsTest 框架的通用 app.config...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
  </configSections>
  <specFlow>
    <unitTestProvider name="MsTest" />
  </specFlow>
  <appSettings>
    <add key="Browser" value="Chrome" />
  </appSettings>
</configuration>

创建 app.config 文件的目的是让我可以操作 appSettings 并针对键值“浏览器”传递任何值。

using System;
using System.Configuration;  
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome; 
using OpenQA.Selenium.IE;     
using OpenQA.Selenium.Firefox;
using TechTalk.SpecFlow;

namespace Automation
{
    [Binding]
    [TestFixture]
    public class GoogleTests_Chrome
    {
        private IWebDriver _driver;

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            switch (ConfigurationManager.AppSettings["Browser"])
            {
                case "Chrome":
                    _driver = new ChromeDriver();
                    _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                    _driver.Manage().Cookies.DeleteAllCookies();
                    _driver.Manage().Window.Maximize();
                    break;
                case "Firefox":
                    _driver = new FirefoxDriver();
                    _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                    _driver.Manage().Cookies.DeleteAllCookies();
                    _driver.Manage().Window.Maximize();
                    break;
                case "IE":
                    _driver = new InternetExplorerDriver();
                    _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                    _driver.Manage().Cookies.DeleteAllCookies();
                    _driver.Manage().Window.Maximize();
                    break;
                default:
                    Console.WriteLine("Defaulting to Firefox");
                    _driver = new FirefoxDriver();
                    _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                    _driver.Manage().Cookies.DeleteAllCookies();
                    _driver.Manage().Window.Maximize();
                    break;
            }
        }

        [Given("I have navigated to (.*) in my web browser")]
        public void TestSetUp(string url)
        {
            _driver.Navigate().GoToUrl(url);
        }

        [Then("I want to verify that the page has loaded successfully")]
        public void GooglePageTitle()
        {
            Assert.AreEqual("Google", _driver.Title);
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            if (_driver != null) _driver.Quit();
        }
    }
}

在这个阶段,我创建了一个简单的 specFlow 功能文件,如下所示,以执行以下步骤。

Feature: AutomationFeature

@mytag
Scenario: Navigate to the Google homepage
    Given I have navigated to http://www.google.com in my web browser
    Then I want to verify that the page has loaded successfully

不幸的是,每当我运行测试时,我都会不断收到以下错误“{“对象引用未设置为对象的实例。”}'。我注意到 _driver 值返回为空。有任何想法吗?

谢谢你:)

【问题讨论】:

    标签: c# selenium automation config specflow


    【解决方案1】:

    你在哪里调用 FixtureSetup 方法?为什么不在这个方法上设置断点,看看它是否被调用。如果该方法被调用,检查为什么它在任何情况下都没有运行。

    我在您发布的代码中没有看到您使用 [Test] 属性的方法。

    编辑:

    [TestFixture]
    class GoogleTestsChrome
    {
        [Test]
        public void GoogleTest()
        {
             try
             {
                  FixtureSetup();
    
                  _driver.Navigate().GoToUrl(url);
                  Assert.AreEqual("Google", _driver.Title);
             }
             finally
             {
                  _driver.Quit();
             }
    
        }
    }
    

    【讨论】:

    • 嗨,我调用“FixtureSetup”的方式是使用 [TestFixtureSetUp],据我了解,这是一个用于在运行任何测试之前调用任何方法的属性。我试图在“FixtureSetup”方法上放置一个断点,但它似乎没有被执行。它在调试期间被完全忽略。我确实在代码中添加了 [Test] 属性,但到目前为止还没有解决问题
    【解决方案2】:

    您的问题是您正在混合使用规范流和普通单元测试。

    使用 specflow 时,它会为您生成单元测试,因此您无需在步骤中使用任何单元测试属性。

    尝试从绑定类中删除所有单元测试基础架构,并使用规范流定义的属性来执行相同的操作,如下所示:

    using System;
    using System.Configuration;  
    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome; 
    using OpenQA.Selenium.IE;     
    using OpenQA.Selenium.Firefox;
    using TechTalk.SpecFlow;
    
    namespace Automation
    {
        [Binding]    
        public class GoogleTests_Chrome
        {
            private IWebDriver _driver;
    
            [BeforeScenario]
            public void FixtureSetup()
            {
                switch (ConfigurationManager.AppSettings["Browser"])
                {
                    case "Chrome":
                        _driver = new ChromeDriver();
                        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                        _driver.Manage().Cookies.DeleteAllCookies();
                        _driver.Manage().Window.Maximize();
                        break;
                    case "Firefox":
                        _driver = new FirefoxDriver();
                        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                        _driver.Manage().Cookies.DeleteAllCookies();
                        _driver.Manage().Window.Maximize();
                        break;
                    case "IE":
                        _driver = new InternetExplorerDriver();
                        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                        _driver.Manage().Cookies.DeleteAllCookies();
                        _driver.Manage().Window.Maximize();
                        break;
                    default:
                        Console.WriteLine("Defaulting to Firefox");
                        _driver = new FirefoxDriver();
                        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
                        _driver.Manage().Cookies.DeleteAllCookies();
                        _driver.Manage().Window.Maximize();
                        break;
                }
            }
    
            [Given("I have navigated to (.*) in my web browser")]
            public void TestSetUp(string url)
            {
                _driver.Navigate().GoToUrl(url);
            }
    
            [Then("I want to verify that the page has loaded successfully")]
            public void GooglePageTitle()
            {
                Assert.AreEqual("Google", _driver.Title);
            }
    
            [AfterScenario]
            public void FixtureTearDown()
            {
                if (_driver != null) _driver.Quit();
            }
        }
    }
    

    你还有一个问题是你已经参数化了这个方法:

    [Given("I have navigated to (.*) in my web browser")]
    public void TestSetUp(string url)
    

    但不是这个:

    [Then("I want to verify that the page has loaded successfully")]
    public void GooglePageTitle()
    

    这意味着除非您将 google url 传递给 TestSetup 方法,否则您的 Then 步骤将失败。您应该在Then 步骤中传入要检查的标题,如下所示

    [Then("The page should have loaded successfully and the title should be '(.*)'")]
    public void ValidatePageHasLoadedSuccessfully(string title)
    

    【讨论】:

      【解决方案3】:

      夹具设置不适用于 SpecFlow。如果您只是想创建一个浏览器,我建议您使用this blog post 中描述的上下文文件。否则,您需要将设置和拆卸定义为 [BeforeScenario("mytag")] 或 [AfterScenario("mytag")]。请参阅this question 了解类似内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-31
        • 1970-01-01
        • 2011-04-11
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多