Many people has asked me on how to run a test on different browsers using Junit without actually changing the value of browser after one test execution. For which I had always kept answering that it may not be possible with Junit. But recently while trying to find a solution for it I came across with the Parameterization option that is available with latest changes of Junit. This was not possible in earlier versions of Junit but now Junit has come up with lot of added options in the latest version of Junit 4.8. Following is a way by which you can execute your automation tests in multiple browsers without actually waiting to finish your tests and then passing the second browser value. See the example below:
package qtt.selenium;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.server.SeleniumServer;
import java.util.Arrays;
import java.util.List;
@RunWith(Parameterized.class)
public class TestGoogleBase extends SeleneseTestBase {
SeleniumServer ss;
private String browser;
public TestGoogleBase(String browser){
this.browser=browser;
}
@Parameters
public static List data() {
return Arrays.asList(new Object[][]{{"*chrome"},{"*iehta"}});
}
@Before
public void setUp() throws Exception {
ss= new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, browser, "http://www.google.co.in/");
selenium.start();
}
@Test
public void testUntitled() throws Exception {
selenium.open("/");
selenium.type("id=lst-ib", "testing");
selenium.click("//input[@value='Google Search']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
}
@After
public void tearDown() throws Exception {
selenium.stop();
ss.stop();
}
}
In the above class I am using the @RunWith(Parametrized.class) option that will tell Junit that this class is run as a parametrized class. A parameter value providing method is added to the class by denoting @Parameter annotation which returns a List< Object[]> array. This array consists of the browser names on which the test needs to be executed, like “*chrome”(Firefox) and “*googlechrome” in this case.
The constructor of the said class is modified to accept a “browser” string that is set when the said parameter function return the browser value.
This “browser” value is then used by the “setup” and inturn by the “Test” method while execution.
package qtt.selenium;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.server.SeleniumServer;
import java.util.Arrays;
import java.util.List;
@RunWith(Parameterized.class)
public class TestGoogleBase extends SeleneseTestBase {
SeleniumServer ss;
private String browser;
public TestGoogleBase(String browser){
this.browser=browser;
}
@Parameters
public static List data() {
return Arrays.asList(new Object[][]{{"*chrome"},{"*iehta"}});
}
@Before
public void setUp() throws Exception {
ss= new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, browser, "http://www.google.co.in/");
selenium.start();
}
@Test
public void testUntitled() throws Exception {
selenium.open("/");
selenium.type("id=lst-ib", "testing");
selenium.click("//input[@value='Google Search']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
}
@After
public void tearDown() throws Exception {
selenium.stop();
ss.stop();
}
}
In the above class I am using the @RunWith(Parametrized.class) option that will tell Junit that this class is run as a parametrized class. A parameter value providing method is added to the class by denoting @Parameter annotation which returns a List< Object[]> array. This array consists of the browser names on which the test needs to be executed, like “*chrome”(Firefox) and “*googlechrome” in this case.
The constructor of the said class is modified to accept a “browser” string that is set when the said parameter function return the browser value.
This “browser” value is then used by the “setup” and inturn by the “Test” method while execution.
No comments:
Post a Comment