Run Selenium tests in multiple browsers with C#

后端 未结 2 1982
半阙折子戏
半阙折子戏 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:18

    If you wanted to be able to specify a browser to run a test on an adhoc basis rather than all of them every time using TestFixtures, Richard Bradshaw has an excellent tutorial here.

    The idea is to use an app config file (and Factory Pattern) that houses values such as the browser, version, platform, selenium hub and port information (as well as any other data you might require in your Hub/Node implementation on Grid) and pull it in at the time of testing to create a WebDriver instance. You can then modify this file in between tests to spin up a WebDriver of a different type if necessary.

    We use this to run tests sequentially with NUnit and it has proven quite effective.

    0 讨论(0)
  • 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<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();
    
    }
    }
    }
    

    Driver Class

     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));
        }
    }
    

    I have tried to fit it around your code.

    0 讨论(0)
提交回复
热议问题