Run Selenium tests in multiple browsers with C#

后端 未结 2 2006
半阙折子戏
半阙折子戏 2020-12-30 16:36

I have a method that creates 2 remote web drivers. one with chrome and another with firefox:

Driver.cs

 public class Driver
{

    public static IWe         


        
2条回答
  •  一个人的身影
    2020-12-30 17:21

    The way I am currently doing this is with NUnit. I had the same problem and could not find a good way to do it with MSTest.

    What I am doing would be:

    As you can see I just create a new TestFixture for each browser.

    [TestFixture(typeof(ChromeDriver))]
    [TestFixture(typeof(InternetExplorerDriver))]
    [TestFixture(typeof(FirefoxDriver))]
    
    public class LoginTests where TWebDriver : IWebDriver, new()
    {
    
    
    [SetUp]
    public void Init()
    {
       Driver.Initialize();
    }
    
    [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();
    
    }
    }
    }
    

    Driver Class

     public class Driver 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));
        }
    }
    

    I have tried to fit it around your code.

提交回复
热议问题