【发布时间】: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