我正在从头开始创建一个新的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并将任何值传递给键值'Browser'。
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值返回null。有任何想法吗?
谢谢:)
答案 0 :(得分:0)
你在哪里调用FixtureSetup方法?为什么不在这个方法上设置断点并查看它是否被调用。如果调用该方法,请检查为什么它不会出现在任何情况下。
我发布的代码中没有[Test]属性的方法。
编辑:
[TestFixture]
class GoogleTestsChrome
{
[Test]
public void GoogleTest()
{
try
{
FixtureSetup();
_driver.Navigate().GoToUrl(url);
Assert.AreEqual("Google", _driver.Title);
}
finally
{
_driver.Quit();
}
}
}
答案 1 :(得分:0)
您的问题是您正在混合specflow和正常的单元测试。
使用specflow时,为您生成单元测试,因此您不需要在步骤中使用任何单元测试属性。
尝试从绑定类中删除所有单元测试基础结构,并使用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网址传递给TestSetup方法,否则您的Then
步骤将失败。您应该在Then
步骤中传入要查看的标题,例如
[Then("The page should have loaded successfully and the title should be '(.*)'")]
public void ValidatePageHasLoadedSuccessfully(string title)
答案 2 :(得分:0)
Fixture设置不适用于SpecFlow。如果您只是尝试创建浏览器,我建议使用this blog post中所述的上下文文件。否则,您需要将设置和拆卸定义为[BeforeScenario(“mytag”)]或[AfterScenario(“mytag”)]。有关类似内容,请参阅this question。