如何从头开始设置selenium解决方案
本文关键字:selenium 解决方案 设置 从头开始 | 更新日期: 2023-09-27 18:14:34
我正在从头开始创建一个新的Selenium解决方案,并且遇到了我试图解决的错误,如果有人可以帮助。
首先,我有一个通用的app.config使用MsTest框架…
<?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值返回为空。什么好主意吗?
谢谢你
在哪里调用FixtureSetup方法?为什么不把断点放在这个方法上,看看它是否被调用。如果方法被调用,检查为什么在任何情况下它都没有运行。
我没有看到你的方法与[Test]属性在你发布的代码。
编辑:[TestFixture]
class GoogleTestsChrome
{
[Test]
public void GoogleTest()
{
try
{
FixtureSetup();
_driver.Navigate().GoToUrl(url);
Assert.AreEqual("Google", _driver.Title);
}
finally
{
_driver.Quit();
}
}
}
你的问题是你混合了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 url到TestSetup方法,否则你的Then
步骤将会失败。您应该在Then
步骤中传入您想要检查的标题,如
[Then("The page should have loaded successfully and the title should be '(.*)'")]
public void ValidatePageHasLoadedSuccessfully(string title)
夹具设置不能与SpecFlow一起工作。如果你只是想创建一个浏览器,我建议使用这篇博文中描述的上下文文件。否则,您需要将设置和拆除定义为[BeforeScenario("mytag")]或[AfterScenario("mytag")]。