Conditional execution of automation tests on different browsers

978 Views Asked by At

I want to execute a test on three browsers and I want a conditional execution of this test.

Example - If browser name is chrome, go to Google.com and search for selenium testing. If browser name is Firefox, search for MongoDB. This is just a sample code solution but I need to implement this concept in my project.

I'm using TestFixture attribute of Nunit to execute the tests on multiple browsers and I want to continue using NUnit.

Here is the code sample that I'm working with.

Note: You may not see the tests in the Test Explorer because of the search parameters in the Test Fixture attribute.

 using NUnit.Framework;
 using OpenQA.Selenium;
 using OpenQA.Selenium.Firefox;
 using OpenQA.Selenium.IE;
 using OpenQA.Selenium.Chrome;
 using NUnit;


namespace MultipleBrowserTesting
{
   [TestFixture(typeof(FirefoxDriver), "MongoDB")]
   [TestFixture(typeof(ChromeDriver), "Selenium Testing")]
   [TestFixture(typeof(InternetExplorerDriver), "ElasticSearch")]

    public class BlogTest<TWebDriver> where TWebDriver : IWebDriver, new()
    {
        private IWebDriver _driver;

    [Test]
    public void Can_Visit_Google(string searchString)
    {
        _driver = new TWebDriver();

        // Navigate
        _driver.Manage().Window.Maximize();

        _driver.Navigate().GoToUrl("http://www.google.ie/");
        _driver.FindElement(By.Id("lst-ib")).SendKeys("searchString");
        _driver.FindElement(By.Name("btnK")).Click();

        FixtureTearDown();
    }

    [TestFixtureTearDown]
    public void FixtureTearDown()
    {
        // if (_driver != null) 
        _driver.Close();
    }
}
}

I think I'm passing the parameters in a wrong way. I have referred to this link.

TestFixtureAttribute

2

There are 2 best solutions below

1
Vish On BEST ANSWER

I have managed to find an answer to my own question. Here is the sample code for that. If you've a better solution, please post it. Thanks!

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using NUnit;
using OpenQA.Selenium.Remote;


namespace MultipleBrowserTesting
{
  [TestFixture(typeof(FirefoxDriver))]
  [TestFixture(typeof(ChromeDriver))]
  [TestFixture(typeof(InternetExplorerDriver))]

   public class BlogTest<TWebDriver> where TWebDriver : IWebDriver, new()
   {
    private IWebDriver _driver;

    [Test]
    public void Can_Visit_Google()
    {
        _driver = new TWebDriver();
        ICapabilities capabilities = ((RemoteWebDriver)_driver).Capabilities;
        string browser = capabilities.BrowserName;
        _driver.Manage().Window.Maximize();
        _driver.Navigate().GoToUrl("http://www.google.ie/");

        if (browser == "internet explorer")
        {
            _driver.FindElement(By.Id("lst-ib")).SendKeys("MongoDB");
        }
        else if (browser == "chrome")
        {
            _driver.FindElement(By.Id("lst-ib")).SendKeys("ElasticSearch");
        }
        else 
        {
            _driver.FindElement(By.Id("lst-ib")).SendKeys("Selenium");
        }
        _driver.FindElement(By.Name("btnG")).Click(); 
        FixtureTearDown();
    }

    [TestFixtureTearDown]
    public void FixtureTearDown()
    {
        _driver.Close();
    }
}
}
0
Vish On

Some techies may argue over the usage of if else if so here is another solution using switch.

public void Can_Visit_Google()
    {
        _driver = new TWebDriver();
        ICapabilities capabilities = ((RemoteWebDriver)_driver).Capabilities;
        string browser = capabilities.BrowserName;
        _driver.Manage().Window.Maximize();
        _driver.Navigate().GoToUrl("http://www.google.ie/");

        switch (browser)
        { 
            case ("internet explorer"):
                _driver.FindElement(By.Id("lst-ib")).SendKeys("ElasticSearch");
                break;

            case ("chrome"):
                _driver.FindElement(By.Id("lst-ib")).SendKeys("MongoDB");
                break;

            case ("firefox"): 
                _driver.FindElement(By.Id("lst-ib")).SendKeys("Selenium");
                break;
        }
                _driver.FindElement(By.Name("btnG")).Click();
                FixtureTearDown();
    }