目前,使用正確的參數爲每個測試用例調用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"); } }