我有一种创建2个远程Web驱动程序的方法。一个带有chrome,另一个带有Firefox:
Driver.cs
public class Driver { public static IWebDriver Instance { get; set; } public static void Initialize() { DesiredCapabilities[] browsers = {DesiredCapabilities.Firefox(),DesiredCapabilities.Chrome()}; foreach (DesiredCapabilities browser in browsers) { if (browser == DesiredCapabilities.Chrome()) { var browser = DesiredCapabilities.Chrome(); System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe"); ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" }; browser.SetCapability(ChromeOptions.Capability, options); Console.Write("Testing in Browser: " + browser.BrowserName); Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser); } else { Console.Write("Testing in Browser: "+ browser.BrowserName); Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser); } } Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15)); }
然后我有一个Test类:
[TestClass] public class LoginTests { [TestInitialize] public void Init() { Driver.Initialize(); } [TestMethod] public void Failed_login() { LoginPage.GoTo(); LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login(); Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect"); } [TestMethod] public void Admin_User_Can_Login() { LoginPage.GoTo(); LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login(); Assert.IsTrue(HomePage.IsAt, "Failed to login."); } [TestCleanup] public void Cleanup() { Driver.Close(); } }
}
问题是当Driver.Intialize被调用时,它无法同时运行chrome和firefox。我要发生的是,调用Init方法时,它将启动两个浏览器并在每个浏览器中运行测试方法。
我目前使用NUnit的方式。我遇到了同样的问题,找不到使用MSTest的好方法。
我正在做的是:
如您所见,我只是为每个浏览器创建一个新的TestFixture。
[TestFixture(typeof(ChromeDriver))] [TestFixture(typeof(InternetExplorerDriver))] [TestFixture(typeof(FirefoxDriver))] public class LoginTests<TWebDriver> where TWebDriver : IWebDriver, new() { [SetUp] public void Init() { Driver.Initialize<TWebDriver>(); } [Test] public void Failed_login() { LoginPage.GoTo(); LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login(); Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect"); } [Test] public void Admin_User_Can_Login() { LoginPage.GoTo(); LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login(); Assert.IsTrue(HomePage.IsAt, "Failed to login."); } [TearDown] public void Cleanup() { Driver.Close(); } } }
驾驶舱
public class Driver<TWebDriver> where TWebDriver : IWebDriver, new() { public static IWebDriver Instance { get; set; } public static void Initialize() { if (typeof(TWebDriver) == typeof(ChromeDriver)) { var browser = DesiredCapabilities.Chrome(); System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe"); ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" }; browser.SetCapability(ChromeOptions.Capability, options); Console.Write("Testing in Browser: " + browser.BrowserName); Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser); } else { Console.Write("Testing in Browser: "+ browser.BrowserName); Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser); } } Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15)); } }
我已尝试将其适合您的代码。