Automation Using Selenium Webdriver
Showing posts with label Compatibility Testing using BrowserStack & Selenium – TestNG. Show all posts
Showing posts with label Compatibility Testing using BrowserStack & Selenium – TestNG. Show all posts

Wednesday 9 November 2016

Compatibility Testing using BrowserStack & Selenium – TestNG

What is Compatibility Testing?
Compatibility testing is a non-functional test to ensure that an application’s compatibility within different environments such various web browsers with various versions, Operating Systems (Windows, Linux, Mac…). In order to pass this test, we also need to check Forward and Backward Compatibility. Here i’m using Browserstack to perform Compatiblity testing.
BrowserStack allows users to make manual and automation testing on different browsers and operation systems.To execute your test scripts using BrowserStack, you need to set parameters of Browsers and Platforms.
There are few steps to be followed to integrate Selenium with BrowserStack.

Step 1 : SignUp for BrowserStack account (Free Trail)
Step 2 : Get your UserName and Access Key (Click on Account at the top and click on ‘Automate‘)
Step 3 : Create your Test scripts using TestNG
Step 4 : Create a TestNG.xml file to run tests in parallel (set platforms and browsers with desired versions).
Step 5 : Execute TestNG.xml.
Step 6 : To view your results, Login and click on ‘Automate‘ link. You can view your project results.
Here I’m providing a sample code. I’m using TestNG to run tests in parallel.
package package1;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserStackTestCase {
private WebDriver driver;
public static final String USERNAME = “YourUserName”;
public static final String AUTOMATE_KEY = “YourAccessKey”;
public static final String URL = “http://” + USERNAME + “:” + AUTOMATE_KEY
+ “@hub.browserstack.com/wd/hub”;
@BeforeTest
@Parameters(value = { “browser”, “version”, “platform” })
public void setUp(String browser, String version, String platform)
throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(“platform”, platform);
capability.setCapability(“browserName”, browser);
capability.setCapability(“browserVersion”, version);
capability.setCapability(“project”, “MyProject”);
capability.setCapability(“build”, “2.01”);
driver = new RemoteWebDriver(new URL(URL), capability);
}
@Test(priority = 1)
public void testcase001() throws Exception {
driver.get(“http://www.google.com”);
System.out.println(“Page title : ” + driver.getTitle());
Assert.assertEquals(“Google”, driver.getTitle());
WebElement element = driver.findElement(By.name(“q”));
element.sendKeys(“Merry christmas”);
element.sendKeys(Keys.ENTER);
}
@Test(priority = 2)
public void testcase002() {
driver.get(“http://seleniumhq.org”);
System.out.println(“Page title : ” + driver.getTitle());
Assert.assertEquals(“Selenium – Web Browser Automation”,
driver.getTitle());
}
@AfterMethod
public void takeScreenShot(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File(“D:\\Screenshot”
+ result.getParameters().toString() + “.png”));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Now setup your configuration in xml file. Copy the below code.
<suite thread-count=”3″ name=”Suite” parallel=”tests”>
<test name=”FirstTest”>
<parameter name=”browser” value=”firefox” />
<parameter name=”version” value=”38″ />
<parameter name=”platform” value=”Windows” />
<classes>
<class name=”package1.BrowserStackTestCase” />
</classes>
</test>
<test name=”SecondTest”>
<parameter name=”browser” value=”safari” />
<parameter name=”version” value=”6.0″ />
<parameter name=”platform” value=”MAC” />
<classes>
<class name=”package1.BrowserStackTestCase” />
</classes>
</test>
<test name=”ThirdTest”>
<parameter name=”browser” value=”Internet Explorer” />
<parameter name=”version” value=”11″ />
<parameter name=”platform” value=”Windows” />
<classes>
<class name=”package1.BrowserStackTestCase” />
</classes>
</test>
</suite>
That’s it, your Testcases were executed. Just login and click on ‘Automate’ link, you will see your results.
I really thank BrowserStack team for making our job simpler.