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

时间:2015-02-12 14:52:01

标签: c# selenium selenium-webdriver

我有一个创建2个远程Web驱动程序的方法。一个用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));
    }

然后我有一个Test类:

[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.Intialize被调用时,它不会同时运行chrome和firefox。我想要发生的是,当调用Init方法时,它启动两个浏览器并在每个浏览器中运行测试方法。

3 个答案:

答案 0 :(得分:8)

我目前的做法是使用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));
    }
}

我试图让它适合您的代码。

答案 1 :(得分:2)

如果您希望能够指定一个浏览器以便在每次使用TestFixtures时以adhoc为基础运行测试而不是所有这些测试,那么Richard Bradshaw有一个很好的教程here

我们的想法是使用一个应用程序配置文件(和工厂模式),其中包含浏览器,版本,平台,硒中心和端口信息(以及您的集线器/节点实现中可能需要的任何其他数据)等值在Grid上)并在测试时将其拉入以创建WebDriver实例。然后,您可以在测试之间修改此文件,以在必要时启动其他类型的WebDriver。

我们使用它来使用NUnit顺序运行测试,并证明它非常有效。

答案 2 :(得分: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();
        }
    }
}