2011-02-17 68 views
29

我正在寻找建议/最好的方法来使Selenium测试在多个浏览器中一个接一个地执行。我正在测试的网站并不大,所以我不需要并行解决方案。从C#开始在多个浏览器中运行硒测试NUnit

我有[SetUp],[TearDown][Test]通常的测试设置方法。 SetUp之一,当然,实例化一个新的ISelenium对象与我想测试的任何浏览器。

所以我想要做的是以编程方式说:这个测试将按顺序在Chrome,IE和Firefox上运行。我怎么做?

编辑:

这可能会有所帮助。我们使用CruiseControl.NET在成功构建之后启动NUnit测试。有没有办法将参数传递给NUnit可执行文件,然后在测试中使用该参数?这样我们可以让NUnit使用不同的浏览器参数运行几次。

回答

50

NUnit的2.5+现在支持通用测试夹具,这使得在多个浏览器测试非常简单。 http://www.nunit.org/index.php?p=testFixture&r=2.5

运行以下示例将执行两次GoogleTest,一次在Firefox中,一次在IE中执行。

using NUnit.Framework; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.IE; 
using System.Threading; 

namespace SeleniumTests 
{ 
    [TestFixture(typeof(FirefoxDriver))] 
    [TestFixture(typeof(InternetExplorerDriver))] 
    public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new() 
    { 
     private IWebDriver driver; 

     [SetUp] 
     public void CreateDriver() { 
      this.driver = new TWebDriver(); 
     } 

     [Test] 
     public void GoogleTest() { 
      driver.Navigate().GoToUrl("http://www.google.com/"); 
      IWebElement query = driver.FindElement(By.Name("q")); 
      query.SendKeys("Bread" + Keys.Enter); 

      Thread.Sleep(2000); 

      Assert.AreEqual("bread - Google Search", driver.Title); 
      driver.Quit(); 
     } 
    } 
} 
+0

这是最好的答案 – 2013-01-10 17:10:30

+0

@alanning我无法让我的系统识别IE驱动程序所在的位置,我需要将它放在哪里才能运行? – DEnumber50 2015-04-21 19:08:56

0

还有必须是一个更好的方法,但是您可以使用T4 templates为每个浏览器生成重复的测试类 - 实质上是为每个浏览器自动复制并粘贴测试。

+0

呀,必须有一个更好的办法。 – Edgar 2011-02-17 12:44:07

1

好的,一种解决方案是使用不同浏览器设置ISelenium对象的包装测试。然后他们将这个对象传递给所有使用它的其他测试,而不是像以前那样自己设置一个新对象。

缺点是,我最终为每个浏览器做了一个大的测试。不是最好的解决方案。仍在寻找......

编辑:

在这花了一些时间。我提出的解决方案是在解决方案中有一个文本文件,指定用于测试的浏览器。在实例化Selenium对象时,NUnit会提取设置。

我使用CruiseControl.NET来运行自动构建和测试。而不是只运行一次测试,我配置它运行两次。但是在每次测试之前,我运行命令行命令来更改配置文本文件中的浏览器。

<exec> 
    <executable>cmd</executable> 
    <buildArgs>/C echo firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe > F:\...\selenium_browser.txt</buildArgs> 
</exec> 
<exec> 
    <executable>F:\...\NUnit 2.5.7\bin\net-2.0\nunit-console.exe</executable> 
    <baseDirectory>F:\...\bin\Debug</baseDirectory> 
    <buildArgs>F:\...\...nunit /xml:"F:\CCXmlLog\Project\nunit-results.xml" /noshadow</buildArgs> 
    <successExitCodes>0</successExitCodes> 
    <buildTimeoutSeconds>1200</buildTimeoutSeconds> 
</exec> 

<exec> 
    <executable>cmd</executable> 
    <buildArgs>/C echo googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe > F:\...\selenium_browser.txt</buildArgs> 
</exec> 
<exec> 
    <executable>F:\...\NUnit 2.5.7\bin\net-2.0\nunit-console.exe</executable> 
    <baseDirectory>F:\...\bin\Debug</baseDirectory> 
    <buildArgs>F:\...\...nunit /xml:"F:\CCXmlLog\Project\nunit-results.xml" /noshadow</buildArgs> 
    <successExitCodes>0</successExitCodes> 
    <buildTimeoutSeconds>1200</buildTimeoutSeconds> 
</exec> 
+0

事实证明,您也可以使用``元素指定环境变量,这样可以避免文本文件。 – Edgar 2011-06-02 16:11:06

5

这是一个反复出现的问题和解决几种方法:

  1. 工厂方法产生的ISelenium对象 - 你有一个静态getSelenium方法的辅助类。该方法读入一些外部配置,该配置具有将您想要的浏览器定义为字符串的属性。在你的getSelenium中,你可以相应地配置浏览器。这里有一个使用NUnit的配置文件的便利帖子http://blog.coryfoy.com/2005/08/nunit-app-config-files-its-all-about-the-nunit-file/

  2. 其他人通过IoC容器注入浏览器已经成功。我非常喜欢这个,因为TestNG在Java领域与Guice很好地合作,但我不确定将NUnit和Ninject,MEF等混合在一起是多么容易......

+0

我有助手的东西和可配置的浏览器字符串,但问题是我必须多次使用不同的浏览器运行相同的测试。 – Edgar 2011-02-21 08:22:17

+0

然后您应该交换每次运行的配置文件。整个配置文件将是相同的,但是您需要将浏览器名称作为每个想要测试的浏览器的字符串。 – pnewhook 2011-02-22 15:08:35

4

我使用iWeb中的驱动程序的列表,通过生产线来进行所有的浏览器测试,线路:

[ClassInitialize] 
     public static void ClassInitialize(TestContext context) { 
      drivers = new List<IWebDriver>(); 
      firefoxDriver = new FirefoxDriver(); 
      chromeDriver = new ChromeDriver(path); 
      ieDriver = new InternetExplorerDriver(path); 
      drivers.Add(firefoxDriver); 
      drivers.Add(chromeDriver); 
      drivers.Add(ieDriver); 
      baseURL = "http://localhost:4444/"; 
     } 

    [ClassCleanup] 
    public static void ClassCleanup() { 
     drivers.ForEach(x => x.Quit()); 
    } 

..and then am able to write tests like this: 

[TestMethod] 
     public void LinkClick() { 
      WaitForElementByLinkText("Link"); 
      drivers.ForEach(x => x.FindElement(By.LinkText("Link")).Click()); 
      AssertIsAllTrue(x => x.PageSource.Contains("test link")); 
     } 

..where我写我自己的方法WaitForElementByLinkText和AssertIsAllTrue来执行操作的每个驱动程序,并在出现任何故障,输出信息帮助我识别器浏览器可能失败:

public void WaitForElementByLinkText(string linkText) { 
      List<string> failedBrowsers = new List<string>(); 
      foreach (IWebDriver driver in drivers) { 
       try { 
        WebDriverWait wait = new WebDriverWait(clock, driver, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(250)); 
        wait.Until((d) => { return d.FindElement(By.LinkText(linkText)).Displayed; }); 
       } catch (TimeoutException) { 
        failedBrowsers.Add(driver.GetType().Name + " Link text: " + linkText); 
       } 
      } 
      Assert.IsTrue(failedBrowsers.Count == 0, "Failed browsers: " + string.Join(", ", failedBrowsers)); 
     } 

的IEDriver慢得令人痛苦,但这将有3 O f并行运行测试的主要浏览器

5

这基本上只是alanning的答案的扩展(11月21日在20:20)。我的情况很相似,只是我不想用无参数构造函数运行(因此使用驱动程序可执行文件的默认路径)。我有包含我想测试对司机单独的文件夹,这似乎很好地工作了:

[TestFixture(typeof(ChromeDriver))] 
[TestFixture(typeof(InternetExplorerDriver))] 
public class BrowserTests<TWebDriver> where TWebDriver : IWebDriver, new() 
{ 
    private IWebDriver _webDriver; 

    [SetUp] 
    public void SetUp() 
    { 
     string driversPath = Environment.CurrentDirectory + @"\..\..\..\WebDrivers\"; 

     _webDriver = Activator.CreateInstance(typeof (TWebDriver), new object[] { driversPath }) as IWebDriver; 
    } 

    [TearDown] 
    public void TearDown() 
    { 
     _webDriver.Dispose(); // Actively dispose it, doesn't seem to do so itself 
    } 

    [Test] 
    public void Tests() 
    { 
     //TestCode 
    } 
} 

}

1

我想知道同样的问题,最后我got solution

所以当你安装插件时,你就可以控制应该在哪个浏览器中进行测试。

功能例如:

@Browser:IE 
@Browser:Chrome 
Scenario Outline: Add Two Numbers 
    Given I navigated to/
    And I have entered <SummandOne> into summandOne calculator 
    And I have entered <SummandTwo> into summandTwo calculator 
    When I press add 
    Then the result should be <Result> on the screen 
    Scenarios: 
     | SummandOne | SummandTwo | Result | 
     | 50 | 70 | 120 | 
     | 1 | 10 | 11 | 

实施

[Given(@"I have entered '(.*)' into the commentbox")] 
public void GivenIHaveEnteredIntoTheCommentbox(string p0) 
{ 
      Browser.Current.FindElement(By.Id("comments")).SendKeys(p0); 
} 

More info

相关问题