Automation Using Selenium Webdriver

Wednesday, 9 November 2016

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/”);
}
@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();
}
}

Tuesday, 8 November 2016

Test Case Grouping using TestNG ‘Groups’ Annotations

TestNG allows us to group several tests together. You can group certain tests based on what behavior/aspect they are actually testing. You may have a scenario where few tests belong to a certain group(say Regression) and other ones belong to other group(say Sanity) and yet another one belong to other group(say Login). With this approach you may decide to execute only certain group of test and skip other ones(let’s say there was a regression on related code, so we prefer to only execute Sanity related tests).The group test is a new innovative feature in TestNG, it doesn’t exist in Junit framework
Method annotated with @BeforeGroups gets executed only once for a group before any of the test of that group executes. Method annotated with @AfterGroups gets executed only once for a group only after all of the tests of that group finished execution.
Let’s see this with a simple example:
Review a test group example.
  1. checkMail(),deleteMail() and composeMail() are belong to group ‘Sanity’.
  2. checkDrafts(), checkAccountDetails() and checkPromotions() are belong to group ‘Regression’.
  3. If i chose to execute only ‘Sanity’ then Step1 should be  executed.
package samples;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class TestNG_Groups {
@BeforeGroups(“Login”)
public void login_Account() {
System.out.println(“Account Login”);
}
@Test(groups = “Sanity”)
public void checkMail() {
System.out.println(“Checking Mail in the Inbox”);
}
@Test(groups = “Regression”)
public void checkDrafts() {
System.out.println(“Checking Drafts”);
}
@Test(groups = “Regression”)
public void checkPromotions() {
System.out.println(“Checking Promotions”);
}
@Test(groups = “Regression”)
public void checkAccountDetails() {
System.out.println(“Checking Account Details”);
}
@Test(groups = “Sanity”)
public void composeMail() {
System.out.println(“Send a Mail “);
}
@Test(groups = “Sanity”)
public void deleteMail() {
System.out.println(“Delete a Mail”);
}
@AfterGroups(“Login”)
public void logout_Account() {
System.out.println(“Account Logout”);
}
}
Now create testng.xml file,
<suite name=”Build 2.0.1″>
<test name=”groups Test”>
<classes>
<class name=”samples.TestNG_Groups” />
</classes>
<groups>
<run>
<include name=”Sanity” />
</run>
</groups>
</test>
</suite>


When you run this xml only the Sanity testcases should be executed.We can also use ‘exclude’ to exclude desired testcases.