2013-04-22 36 views
2

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

} } 

回答

3

Dev分支:在我的經驗,Dev分支發揮很好地與NUnit的的參數化測試用例的最新位。

只需在測試用例本身內部移動Bootstrap調用,並確保在最後手動調用I.Dispose()。這允許在此上下文中運行時正確創建瀏覽器。

下面是一個示例,如果您從開發分支上的GitHub獲取最新內容,您應該能夠複製/粘貼並運行。

[TestCase(FluentAutomation.SeleniumWebDriver.Browser.InternetExplorer)] 
    [TestCase(FluentAutomation.SeleniumWebDriver.Browser.Chrome)] 
    public void CartTest(FluentAutomation.SeleniumWebDriver.Browser browser) 
    { 
     FluentAutomation.SeleniumWebDriver.Bootstrap(browser); 
     I.Open("http://automation.apphb.com/forms"); 
     I.Select("Motorcycles").From(".liveExample tr select:eq(0)"); // Select by value/text 
     I.Select(2).From(".liveExample tr select:eq(1)"); // Select by index 
     I.Enter(6).In(".liveExample td.quantity input:eq(0)"); 
     I.Expect.Text("$197.70").In(".liveExample tr span:eq(1)"); 

     // add second product 
     I.Click(".liveExample button:eq(0)"); 
     I.Select(1).From(".liveExample tr select:eq(2)"); 
     I.Select(4).From(".liveExample tr select:eq(3)"); 
     I.Enter(8).In(".liveExample td.quantity input:eq(1)"); 
     I.Expect.Text("$788.64").In(".liveExample tr span:eq(3)"); 

     // validate totals 
     I.Expect.Text("$986.34").In("p.grandTotal span"); 

     // remove first product 
     I.Click(".liveExample a:eq(0)"); 

     // validate new total 
     I.WaitUntil(() => I.Expect.Text("$788.64").In("p.grandTotal span")); 
     I.Dispose(); 
    } 

它應該找到它的方式來NuGet在下一個版本,我希望在本週發生。

NuGet v2.0:每次測試只支持一次Bootstrap調用。在v1中,我們內置了對提供者支持的所有瀏覽器運行相同測試的支持,但發現用戶更願意將它分成多個測試。

我用v2管理它的方式是擁有一個'Base'TestClass,其中包含TestMethods。然後,我再擴展一次我想要定位的瀏覽器,並覆蓋構造函數以調用適當的Bootstrap方法。

有點冗長但很容易管理。

相關問題