使用C#在多个浏览器中运行Selenium测试

5
我有一个方法,可以创建两个远程的网络驱动程序。一个使用chrome,另一个使用firefox:
Driver.cs
 public class Driver
{

    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        DesiredCapabilities[] browsers = {DesiredCapabilities.Firefox(),DesiredCapabilities.Chrome()};
       foreach (DesiredCapabilities browser in browsers)
        {
            if (browser == DesiredCapabilities.Chrome()) 
                {
                var browser = DesiredCapabilities.Chrome();
                System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe");
                ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" };
                browser.SetCapability(ChromeOptions.Capability, options);
                Console.Write("Testing in Browser: " + browser.BrowserName);

                Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);

            } else {
               Console.Write("Testing in Browser: "+ browser.BrowserName);
               Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);
           }   
        }
        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
    }

然后我有一个测试类:

[TestClass]
public class LoginTests
{
    [TestInitialize]
    public void Init()
    {
       Driver.Initialize();
    }

    [TestMethod]
    public void Failed_login()
    {
        LoginPage.GoTo();
        LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

        Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");
    }


    [TestMethod]
    public void Admin_User_Can_Login()
    {
        LoginPage.GoTo();
        LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

        Assert.IsTrue(HomePage.IsAt, "Failed to login.");
    }

    [TestCleanup]
    public void Cleanup()
    {
      Driver.Close();

    }
}

问题在于当调用Driver.Initialize时,它不会同时运行Chrome和Firefox。我希望的是当调用Init方法时,它会启动两个浏览器并在每个浏览器中运行测试方法。

3个回答

13

我目前使用的方法是NUnit。我曾经也遇到了同样的问题,但是在MSTest中找不到好的解决方法。

我的做法是:

正如您所见,我只是为每个浏览器创建了一个新的TestFixture。

[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(FirefoxDriver))]

public class LoginTests<TWebDriver> where TWebDriver : IWebDriver, new()
{


[SetUp]
public void Init()
{
   Driver.Initialize<TWebDriver>();
}

[Test]
public void Failed_login()
{
    LoginPage.GoTo();
    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

    Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");
}


[Test]
public void Admin_User_Can_Login()
{
    LoginPage.GoTo();
    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

    Assert.IsTrue(HomePage.IsAt, "Failed to login.");
}

[TearDown]
public void Cleanup()
{
  Driver.Close();

}
}
}

驱动程序类

 public class Driver<TWebDriver> where TWebDriver : IWebDriver, new()
 {

    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        if (typeof(TWebDriver) == typeof(ChromeDriver))
        {


         var browser = DesiredCapabilities.Chrome();
                System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe");
                ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" };
                browser.SetCapability(ChromeOptions.Capability, options);
                Console.Write("Testing in Browser: " + browser.BrowserName);



                Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);

            } else {
               Console.Write("Testing in Browser: "+ browser.BrowserName);
               Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);
           }   
        }
        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
    }
}

我已经尝试将其适配到您的代码周围。


太棒了!谢谢,我会试一试的。稍后我会回来发帖。谢谢Tidus。 - JerryBringer
没问题,如果我有帮到你,请随意点赞。如果你有任何问题,请告诉我,我会提供帮助。 - Jamie Rees
@JamieRees 它是否允许您进行并行执行,还是仅限顺序执行?只是为了澄清我的理解。 - Saifur
@JamieRees 谢谢Jamie,这解决了我的问题,我已经运行起来了。我把它设置为采纳的答案。我不能投赞成,因为没有足够的声望。 - JerryBringer

3
如果您想在adhoc基础上指定要运行测试的浏览器,而不是每次都使用TestFixtures来运行所有浏览器,请参考Richard Bradshaw的优秀教程here
其思想是使用一个应用程序配置文件(以及Factory模式)来存储诸如浏览器、版本、平台、Selenium Hub和端口信息(以及在Grid上实现Hub/Node中可能需要的任何其他数据),并在测试时拉取它,以创建WebDriver实例。然后,您可以在测试之间修改此文件,以必要时启动不同类型的WebDriver。
我们使用这种方法在NUnit中顺序运行测试,效果非常好。

这是一个很好的回复,正是我想要做的。-- 我只是想知道你是否有在顺序 NUnit 测试中使用过这个的例子? - pipplupp
当然,在这篇文章中,有一个包含一些样例的github仓库链接。这是其中之一:https://github.com/FriendlyTester/WebDriverFactoryExample/blob/master/WebDriverDriverFactory/RichardsTestSuite/Tests/LoginTests.cs - Jordan

0

使用 NUnit 更简单、更直接的解决方案:

namespace Test
{
    //add all browser you want to test here
    [TestFixture(typeof(FirefoxDriver))]
    [TestFixture(typeof(ChromeDriver))]
    public class SkillTest<TWebDriver> where TWebDriver : IWebDriver, new()
    {
        private IWebDriver _driver;
        private string driverPath;
        
        [SetUp]
        public void Init()
        {
            _driver = new TWebDriver();
            _driver.Navigate().GoToUrl("https://localhost:5001/");
        }

        [Test]
        public void your_test_case()
        { 
        //some test logic
        } 


        [TearDown]
        public void CleanUp()
        {
            _driver.Quit();
            _driver.Dispose();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接