我需要帮助将多个浏览器测试装置组合到我的框架中。 我的目标是通过定义testFixture的类型来逐个在多个浏览器上运行测试:ChromeDriver,InternetExplorerDriver等。
我已经按照Pluralsight上的一个教程来构建我的框架。现在看起来像这样:
TestClass:LoginTest
[TestFixture]
public class LoginTest : PortalTest
{
[Test]
public void LoginUser()
{
Assert.IsTrue(HomePage.IsAt, "Failed to login. ");
}
}
接下来, PortalTest基类:
public class PortalTest
{
[SetUp]
public void Init()
{
Driver.Initialize();
LoginPage.Goto();
LoginPage.LoginAs("user").WithPassword("pass").Login();
}
[TearDown]
public void CleanUp()
{
Driver.Close();
}
}
使用GoTo()的LoginPage:
public class LoginPage
{
public static void Goto()
{
//var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
//wait.Until(d => d.SwitchTo().ActiveElement().GetAttribute("id") == "UserName");
Driver.Instance.Navigate().GoToUrl(Driver.BaseAddress + "Account/LogOn?ReturnUrl=%2FHome");
if (Driver.Instance.Title != "Login")
{
throw new Exception("Not on Login page");
}
}
我的Driver类初始化 FirefoxDriver:
public class Driver : TestBase
{
public static IWebDriver Instance { get; set; }
public static void Initialize()
{
Instance = new FirefoxDriver();
// wait 5 sec
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
}
正如您所见,Driver类扩展了TestBase。这个定义了多个浏览器案例并返回相应的驱动程序。
我没有多少尝试,但没有运气。 相关帖子我的基础是: https://stackoverflow.com/a/7854838/2920121
http://makit.net/testing-aspdotnet-mvc-application-with-selenium-and-nunit
答案 0 :(得分:1)
您需要拥有WebDriver工厂类来创建所有驱动程序实例,以便轻松处理驱动程序。
您实例化所有驱动程序的WebDriver Factory
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.PhantomJS;
namespace Test.Tests
{
/// <summary>
/// A static factory object for creating WebDriver instances
/// </summary>
public class WebDriverFactory
{
public IWebDriver Driver;
protected WebDriverFactory(BrowserType type)
{
Driver = WebDriver(type);
}
[TestFixtureTearDown]
public void TestFixtureTearnDown()
{
Driver.Quit();
}
/// <summary>
/// Types of browser available for proxy examples.
/// </summary>
public enum BrowserType
{
IE,
Chrome,
Firefox,
PhantomJS
}
public static IWebDriver WebDriver(BrowserType type)
{
IWebDriver driver = null;
switch (type)
{
case BrowserType.IE:
driver = IeDriver();
break;
case BrowserType.Firefox:
driver = FirefoxDriver();
break;
case BrowserType.Chrome:
driver = ChromeDriver();
break;
default:
driver = PhanthomJsDriver();
break;
}
return driver;
}
/// <summary>
/// Creates Internet Explorer Driver instance.
/// </summary>
/// <returns>A new instance of IEDriverServer</returns>
private static IWebDriver IeDriver()
{
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnsureCleanSession = true;
IWebDriver driver = new InternetExplorerDriver(options);
return driver;
}
/// <summary>
/// Creates Firefox Driver instance.
/// </summary>
/// <returns>A new instance of Firefox Driver</returns>
private static IWebDriver FirefoxDriver()
{
FirefoxProfile profile = new FirefoxProfile();
IWebDriver driver = new FirefoxDriver(profile);
return driver;
}
/// <summary>
/// Creates Chrome Driver instance.
/// </summary>
/// <returns>A new instance of Chrome Driver</returns>
private static IWebDriver ChromeDriver()
{
ChromeOptions chromeOptions = new ChromeOptions();
IWebDriver driver = new ChromeDriver(chromeOptions);
return driver;
}
/// <summary>
/// Creates PhantomJs Driver instance..
/// </summary>
/// <returns>A new instance of PhantomJs</returns>
private static IWebDriver PhanthomJsDriver()
{
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
if (proxy != null)
IWebDriver driver = new PhantomJSDriver(service);
return driver;
}
}
}
<强>用法强>
using System;
using NUnit.Framework;
namespace Test.TestUI
{
[TestFixture(BaseTestFixture.BrowserType.Chrome)]
[TestFixture(BaseTestFixture.BrowserType.Firefox)]
[TestFixture(BaseTestFixture.BrowserType.InternetExplorer)]
public class DemoTest : WebDriverFactory
{
public DemoTest(BaseTestFixture.BrowserType browser)
: base(browser)
{
}
[TestFixtureSetUp]
public void SetUpEnvironment()
{
}
}
}
我有点跟随this来满足我的测试需求。