2013-04-22 38 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方法。

有点冗长但很容易管理。

相关问题