Main advantage of selenium grid is we can run webdriver software test cases in parallel using selenium grid 2 environment to reduce software test execution time as described in THIS POST. Also It helps us to perform compatibility testing as it is supporting multiple browsers too. Earlier we have configured selenium grid 2 hub in THIS POST and selenium grid nodes in THIS POST in just single machine. Now lets try to use this environment to execute webdriver software automation test cases in parallel.
Prerequisite : Selenium grid hub and node should be running as described in previous steps.
I have created simple software automation test to execute it parallel in multiple browsers using selenium grid. It will launch three browsers (Mozilla firefox, google chrome and IE) parallel and then execute same software automation test inside each browser.
Note : Please remove internet explorer related code stuff from bellow given script if face any related error as it is not very stable with selenium grid.
Create bellow give test in your eclipse project under Grid package.
SeGridTest.java
package Grid;
import static org.testng.Assert.fail;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class SeGridTest {
WebDriver driver = null;
private StringBuffer verificationErrors = new StringBuffer();
// Pass plateform, browser and url parameters to launch browser and URL in given plateform.
@Parameters({ "platform", "browser", "url" })
@BeforeTest(alwaysRun = true)
public void setup(String platform, String browser, String url) throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
// Set Platforms based on parameter received from testng.xml.
caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
// Set browser capability based on parameter received from testng.xml.
if (browser.equalsIgnoreCase("Internet Explorer"))
caps = DesiredCapabilities.internetExplorer();
if (browser.equalsIgnoreCase("Firefox"))
caps = DesiredCapabilities.firefox();
if (browser.equalsIgnoreCase("chrome"))
caps = DesiredCapabilities.chrome();
// Open browser on grid node.
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Open URL of Application
driver.get(url);
}
// Simple test method to execute.
@Test(description = "Wait for button enabled")
public void waitForButtonEnabled() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
driver.findElement(By.xpath("//input[@id='submitButton']")).click();
}
// Simple test method to execute.
@Test(description = "Wait for button enabled", dependsOnMethods = { "waitForButtonEnabled" })
public void testCalc() throws InterruptedException {
driver.navigate().to("http://only-testing-blog.blogspot.com/2014/04/calc.html");
driver.findElement(By.xpath("//input[@id='2']")).click();
driver.findElement(By.xpath("//input[@id='plus']")).click();
driver.findElement(By.xpath("//input[@id='5']")).click();
driver.findElement(By.xpath("//input[@id='equals']")).click();
String result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
// Get Browser name and version.
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();
// Get OS name.
String os = System.getProperty("os.name").toLowerCase();
// Print test result with browser and OS version detail.
System.out.println("OS = " + os + ", Browser = " + browserName + " "+ browserVersion + " Test Result = " + result);
}
@AfterTest
public void afterTest() {
// Close the browser
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
If you remember, We have used parallel in testng.xml file to execute software automation tests in parallel in selenium webdriver as described in THIS POST. We will use same thing here but using selenium grid.
You can see, We have used parameters (plateform, browser and url) with setup method. All these parameters will be served from testnx.xml file as bellow. We have configured three test in bellow given xml file with different browser parameter to run test in different browsers. Create bellow given testng.xml file under your project and run it.
testng.xml
<!--Set thread-count = 3 to execute test parallel in 3 max browsers at at time. You can increase it-->
<suite name="Parallel Tests" verbose="1" thread-count="3" parallel="tests">
<tests>
<!--Set test parameters to execute test in IE browser on windows plateform.-->
<test name="Windows+IE Test" >
<parameters>
<parameter name="platform" value="Windows" />
<parameter name="browser" value="Internet Explorer" />
<parameter name="url" value="http://only-testing-blog.blogspot.com/2014/01/textbox.html" />
</parameters>
<classes>
<class name="Grid.SeGridTest" />
</classes>
</test>
<!--Set test parameters to execute test in Firefox browser on windows plateform.-->
<test name="Windows+Firefox Test" >
<parameters>
<parameter name="platform" value="Windows" />
<parameter name="browser" value="Firefox" />
<parameter name="url" value="http://only-testing-blog.blogspot.com/2014/01/textbox.html" />
</parameters>
<classes>
<class name="Grid.SeGridTest" />
</classes>
</test>
<!--Set test parameters to execute test in chrome browser on windows plateform.-->
<test name="Windows+chrome Test" >
<parameters>
<parameter name="platform" value="Windows" />
<parameter name="browser" value="chrome" />
<parameter name="url" value="http://only-testing-blog.blogspot.com/2014/01/textbox.html" />
</parameters>
<classes>
<class name="Grid.SeGridTest" />
</classes>
</test>
</tests>
</suite>
Now, execute above testng.xml file and observe test execution process. It will launch three browsers parallel and then execute tests in all three browsers. At the end of test execution, It will print result in console as bellow.
OS = windows 7, Browser = chrome 43.0.2357.132 Test Result = 7
OS = windows 7, Browser = internet explorer 8 Test Result = 7
OS = windows 7, Browser = firefox 38.0.5 Test Result = 7
Testng result will looks like bellow.
This is the way to execute selenium webdriver software automation test in parallel multiple browsers using selenium grid 2 to perform compatibility testing.
Hi, Aravind
ReplyDeleteThank you very much, I am here with lots of QA automation test knowledge from a known-nothing. you are my only true teacher.
Here I face a problem: I couldn't run the script. So I change localhost to the address of the hub's IP which I get follow a normal way to get PC IP, but it still not works.
then I found there is an address on my hub command prompt which is a difference one, so I try using it. The scripts run properly, and I get the testng result same as yours here.
and there are information such as "input[@id='5']]]" showed in the node command prompt, the end is "Done: [delete seesion:2cb1cc4f-...".
I don't know whether or not it means that running webdriver in grid is successfully.
thanks.
Mak
what should we use for parallel testing?
ReplyDeleteif Selenium Grid then why not TestNG and vice versa.
what are the pros and cons? what is the basic difference between both of them?
In TestNG,
Delete- Parallel execution of tests is possible between different browsers of single platform machine.
- Application can be tested only with current available browser versions.
- At the same time, you can not run your tests on multiple platforms (windows, mac, linux..etc) on different machines.
In selenium grid,
- You can run your tests in parallel on different platforms, different versions of same browser & different browsers.
Hope now you understand the basic difference between them.
Can we run test cases in grid without using testng.
ReplyDelete