【问题标题】:Selenium C# multiple browser testing exe referencesSelenium C# 多浏览器测试 exe 参考
【发布时间】:2015-04-21 15:43:05
【问题描述】:

我有一个关于使用 C# 在 selenium NUnit 中为多个浏览器测试引用 exe 文件的问题。我添加了额外的代码来让我的测试在每个浏览器中运行,但是每次运行测试时,我都会收到错误:OpenQA.Selenium.DriverServiceNotFoundException。我的问题是,有没有在不专门布置路径的情况下添加参考?我认为如果不重构我所拥有的内容,我将无法为我拥有的当前代码添加路径。提前感谢您的帮助。

测试夹具

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public class CustomerLogin<TWebDriver> where TWebDriver : IWebDriver, new()
{
    private IWebDriver driver;
    private string url;

    [TestFixtureSetUp]
    public void FixtureSetUp()
    {
        url = System.Configuration.ConfigurationManager.AppSettings["homeUrl"];
        this.driver = new TWebDriver();
        driver.Navigate().GoToUrl(url);
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
    }

使用语句

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    我相信你可以用这样的方式设置路径;

      System.setProperty("webdriver.ie.driver", "C:\my\path\to\IEDriverServer.exe");
      System.setProperty("webdriver.firefox.driber", "C:\my\path\to\FFDriver.exe");  
    

    或者,我认为更好的解决方案是将驱动程序添加到您的项目中并修改构建,以便它们最终出现在您的放置文件夹中。上面的代码非常适合您的本地系统,但不可移植。大多数自动化解决方案旨在“开箱即用”,这需要您将依赖项打包到 bin 中。

    将驱动程序与您的构建打包在一起;将“驱动程序”文件夹添加到项目中(右键单击解决方案-> 添加-> 新文件夹),然后在下面添加可执行文件(右键单击文件夹,添加现有项目)。现在您可以在解决方案资源管理器中看到驱动程序 exe,右键单击并选择属性,在复制到输出目录下选择“如果较新则复制”。

    【讨论】:

    • 很好的建议,您将如何实施您的建议?这正是我想要做的
    • @DEnumber50 将评论放在答案中。
    • 我已放置文件夹并更改了属性。我需要在代码中添加什么来识别这些 exe 文件?我收到错误“System.Reflection.TargetInvocationException”。上面的相同错误也仍然存在。
    • @DEnumber50 您可能仍需要使用该 setproperty 位,但您可以为其提供相对路径或仅提供 exe 名称,以便它保持可移植性。不过,老实说,我不确定,因为我面前没有硒项目来处理事情。另外,这个TWebDriver(); 是怎么回事?也许如果你用过; driver = new InternetExplorerDriver(); 反而会起作用。
    • @DEnumber50 正常情况是在您的代码中使用IWebDriver 接口,并根据您的配置确定您在设置中实例化的版本。也许我上面引用的陈述实际上是你的问题?
    【解决方案2】:

    对代码进行一些更改后,我可以在三大浏览器上运行所有测试。我必须将 Chrome.exe 和 IE.exe 添加到 bin\Debug 文件夹(带有 WebDriver.dll 的文件夹)这是我对代码所做的更改。

    测试夹具

    [TestFixture(typeof(FirefoxDriver))]
    [TestFixture(typeof(InternetExplorerDriver))]
    [TestFixture(typeof(ChromeDriver))]
    public class CustomerLogin<TWebDriver> where TWebDriver : IWebDriver, new()
    {
        private IWebDriver driver;
        private string url;
        [SetUp]
        public void SetUp()
        {
            this.driver = new TWebDriver();
            url = System.Configuration.ConfigurationManager.AppSettings["homeUrl"];
            driver.Navigate().GoToUrl(url);
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
            //Finding the customer login link and clicking it
            driver.FindElement(By.Id("Homepage_r2_c14")).Click();
        }
        [TearDown]
        public void TearDown()
        {
            driver.Quit();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 2018-07-22
      • 2011-11-27
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多