Zach
Zach

Reputation: 5885

How to manage automated UI test code for different browsers?

We are trying to do some automated UI testing using Selenium. So far for one function, we write at least 3 tests for IE, FF, Chrome.

For example, we wrote 3 tests: LoginTestFF, LoginTestIE, LoginTestChrome for the login function. This way, we can see 3 items in the VS Test View, and can select one or all of them to run.

Thanks to Selenium, the test code for each browser are pretty much same. So we don't want to repeat that again.

Is it possible to see the tests in the VS Test View like:

LoginTestFF
LoginTestIE
LoginTestChrome

but using one set of test code?

I've found that the [Browser] attribute in xUnit should be OK.

How do develop such an attribute for Visual Studio Testing?

Upvotes: 0

Views: 2475

Answers (2)

Yi Zeng
Yi Zeng

Reputation: 32855

If you still want to see all three of them is VS test view, you may try this. (But what if you have ten different browsers?)

[TestMethod]
public void LoginTestFF() {
    ExecuteLoginTest();
}

[TestMethod]
public void LoginTestIE() {
    ExecuteLoginTest();
}

[TestMethod]
public void LoginTestChrome() {
    ExecuteLoginTest();
}

public void ExecuteLoginTest() {
    //real test steps
}

If you just want to reuse code for all browsers, you may define your desired browser in App.config, then use a switch case to select.

<add key="Browser" value="Chrome"/>
IWebDriver driver;
string browser = ConfigurationManager.AppSettings["Browser"];
switch (browser) {
    case "Firefox":
        driver = new FirefoxDriver();
        break;
    case "IE":
        driver = new InterntExplorerDriver();
        break;
    case "Chrome":
        driver = new ChromeDriver();
        break;
    default:
        break;
}
// if this test can only be run against FF and Chrome, add test category to it
[TestMethod, TestCategory("Firefox"), TestCategory("Chrome")]
public void LoginTestTest() {
    //real test steps
}

Upvotes: 2

Arran
Arran

Reputation: 25066

I am not sure of a way with VS.

Using NUnit, use parameterised tests, that is the idea of passing in the parameters into your test cases. So instead of having LoginFF, LoginIE, LoginChrome, you have something similar to this:

[TestCase(Browser.IE)]
[TestCase(Browser.Firefox)]
[TestCase(Browser.Chrome)]
public void TestLogin(Browser browser)
{
    // test stuff
}

So that repeats the tests for each test case source attribute you give it - this is better than copy/pasting your test methods for each browser type.

In my example, combined with Resharper, JustCode or any NUnit Test Runner, it will show the test three times in VS.

Upvotes: 1

Related Questions