Automation Using Selenium Webdriver

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.

Get Attribute Values Using Webdriver

There are cases where you want to get the attributes values and then perform any action.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GetAttributes {
public WebDriver driver;
private By bySearchButton = By.name(“btnK”);
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.get(“http://www.google.com/&#8221;);
}
@Test
public void getAttribute_ButtonName() {
WebElement googleSearchBtn = driver.findElement(bySearchButton);
System.out.println(“Name of the button is:- ” +googleSearchBtn.getAttribute(“name”));
}
@Test
public void getAttribute_Id() {
WebElement googleSearchBtn = driver.findElement(bySearchButton);
System.out.println(“Id of the button is:- “+ googleSearchBtn.getAttribute(“id”));
}
@Test
public void getAttribute_class() {
WebElement googleSearchBtn = driver.findElement(bySearchButton);
System.out.println(“Class of the button is:- “+ googleSearchBtn.getAttribute(“class”));
}
@Test
public void getAttribute_InvalidAttribute() {
WebElement googleSearchBtn = driver.findElement(bySearchButton);
//Will return null value as the ‘status’ attribute doesn’t exists
System.out.println(“Invalid Attribute status of the button is:- “+ googleSearchBtn.getAttribute(“status”));
}
@Test
public void getAttribute_ButtonLabel() {
WebElement googleSearchBtn = driver.findElement(bySearchButton);
System.out.println(“Label of the button is:- “+ googleSearchBtn.getAttribute(“aria-label”));
}
@AfterClass
public void tearDown() {
driver.quit();
}
}

How To Write Data in NotePad Using Selenium

Sometimes we have to print some data into Notepad according to our need.So this post helps you to write data into Notepad.
import java.io.BufferedWriter;
import java.io.FileWriter;
public class txt_write
{
public static void main(String[] args) throws Exception
{
FileWriter fr=new FileWriter(“e:\\data.txt”);
BufferedWriter br=new BufferedWriter(fr);
br.write(“This is sample”);
br.newLine();
br.write(“Testing tools”);
br.newLine();
br.close();
}
}