目前,使用正确的参数为每个测试用例调用openGoogle()。问题是setBrowser似乎没有正常工作。它首次设置并成功完成测试。但是,openGoogle()第二次被调用时,它将继续使用第一个浏览器,而不是使用指定的新浏览器。Selenium,FluentAutomation&NUnit - 如何切换每个TestCase的浏览器?
using NFramework = NUnit.Framework; ...
[NFramework.TestFixture] public class SampleTest : FluentAutomation.FluentTest { string path; private Action<TinyIoCContainer> currentRegistration; public TestContext TestContext { get; set; } [NFramework.SetUp] public void Init() { FluentAutomation.Settings.ScreenshotOnFailedExpect = true; FluentAutomation.Settings.ScreenshotOnFailedAction = true; FluentAutomation.Settings.DefaultWaitTimeout = TimeSpan.FromSeconds(1); FluentAutomation.Settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(30); FluentAutomation.Settings.MinimizeAllWindowsOnTestStart = false; FluentAutomation.Settings.ScreenshotPath = path = "C:\\ScreenShots"; } [NFramework.Test] [NFramework.TestCase(SeleniumWebDriver.Browser.Firefox)] [NFramework.TestCase(SeleniumWebDriver.Browser.InternetExplorer)] public void openGoogle(SeleniumWebDriver.Browser browser) { setBrowser(browser); I.Open("http://www.google.com/"); I.WaitUntil(() => I.Expect.Exists("body")); I.Enter("Unit Testing").In("input[name=q]"); I.TakeScreenshot(browser + "EnterText"); I.Click("button[name=btnG]"); I.WaitUntil(() => I.Expect.Exists(".mw")); I.TakeScreenshot(browser + "ClickSearch"); } public SampleTest() { currentRegistration = FluentAutomation.Settings.Registration; } private void setBrowser(SeleniumWebDriver.Browser browser) { switch (browser) { case SeleniumWebDriver.Browser.InternetExplorer: case SeleniumWebDriver.Browser.Firefox: FluentAutomation.SeleniumWebDriver.Bootstrap(browser); break; } } }
注:这样做,这样下面还是正确的工作 - 打开一个单独的浏览器为每个测试。
public class SampleTest:FluentAutomation.FluentTest { string path; private action currentRegistration; public TestContext TestContext {get;组; }
private void ie() { FluentAutomation.SeleniumWebDriver.Bootstrap(FluentAutomation.SeleniumWebDriver.Browser.InternetExplorer); } private void ff() { >FluentAutomation.SeleniumWebDriver.Bootstrap(FluentAutomation.SeleniumWebDriver.Browser.Firefox); } public SampleTest() { //ff FluentAutomation.SeleniumWebDriver.Bootstrap(); currentRegistration = FluentAutomation.Settings.Registration; } [TestInitialize] public void Initialize() { FluentAutomation.Settings.ScreenshotOnFailedExpect = true; FluentAutomation.Settings.ScreenshotOnFailedAction = true; FluentAutomation.Settings.DefaultWaitTimeout = TimeSpan.FromSeconds(1); FluentAutomation.Settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(30); FluentAutomation.Settings.MinimizeAllWindowsOnTestStart = false; path = TestContext.TestResultsDirectory; FluentAutomation.Settings.ScreenshotPath = path; } [TestMethod] public void OpenGoogleIE() { ie(); openGoogle("IE"); } [TestMethod] public void OpenGoogleFF() { ff(); openGoogle("FF"); } private void openGoogle(string browser) { I.Open("http://www.google.com/"); I.WaitUntil(() => I.Expect.Exists("body")); I.Enter("Unit Testing").In("input[name=q]"); I.TakeScreenshot(browser + "EnterText"); I.Click("button[name=btnG]"); I.WaitUntil(() => I.Expect.Exists(".mw")); I.TakeScreenshot(browser + "ClickSearch"); } }