1

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
  • Please be sure to accept an answer so that the question gets marked as answered. You don't have to do it right now... you can wait, just please don't forget to come back in a day or two. :) Commented Oct 20, 2015 at 15:16
  • Thanks for the information @JeffC. Will do that. Cheers! Commented Oct 20, 2015 at 15:29

2 Answers 2

0

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(); } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.