Automation Testing

Selenium:
Selenium is a free (open source) automated testing suite for web applications across different browsers and platforms. It is quite similar to HP Quick Test Pro (QTP) only that Selenium focuses on automating web-based applications.

Selenium is not just a single tool but a suite of software's, each catering to different testing needs of an organization. It has four components.


  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • WebDriver
  • Selenium Grid




Selenium (http://docs.seleniumhq.org/projects/webdriver/) is an Interface and it is designed to overcome some limitations of selenium RC software testing tool. 

Selenium WebDriver is also known as Selenium 2 and used for web as well mobile applications testing

Selenium 2(WebDriver) controls browser directly from operating system level so it is interacting very fast and with more realistic way with browsers.


Example:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class myFirefoxclass {

 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://only-testing-blog.blogspot.in");
  String i = driver.getCurrentUrl();
  System.out.println(i);
  driver.close();
 }
}

 Example:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class chromebrowser {

 public static void main(String[] args) {
  System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32_2.3\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://www.google.com");
  if(driver.findElement(By.id("gbqfq")).isEnabled()){
   System.out.println("Google search text box Is enabled.");
   driver.findElement(By.id("gbqfq")).sendKeys("WebDriver Test successful.");
   driver.findElement(By.id("gbqfb")).click();
   System.out.println("Google search completed.");
  }
  else{
   System.out.println("Google search test box Is Not enabled.");
   
  }
  driver.close();
 }
}


The way of locating element in webdriver like Locating Element 
  • By ID
  • By Name, 
  • By Class Name,
  • By Tag Name,
  • By Xpath
  • By Link Text Or Partial Link and 
  • By CSS Selector

Creating New Instance Of Firefox Driver

WebDriver driver = new FirefoxDriver();

Command To Open URL In Browser

driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");

Clicking on any element or button of webpage

driver.findElement(By.id("submitButton")).click();

Store text of targeted element in variable

String dropdown = driver.findElement(By.tagName("select")).getText();

Typing text in text box or text area

driver.findElement(By.name("fname")).sendKeys("My First Name");

Applying Implicit wait in webdriver

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

Applying Explicit wait in webdriver with WebDriver canned conditions

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));

Get page title in selenium webdriver

driver.getTitle();

Get Current Page URL In Selenium WebDriver

driver.getCurrentUrl();

Get domain name using java script executor
JavascriptExecutor javascript = (JavascriptExecutor) driver;
String CurrentURLUsingJS=(String)javascript.executeScript("return document.domain");

Generate alert using webdriver's java script executor interface
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("alert('Test Case Execution Is started Now..');");

Selecting or Deselecting value from drop down in selenium webdriver

Select By Visible Text
Select mydrpdwn = new Select(driver.findElement(By.id("Carlist")));
mydrpdwn.selectByVisibleText("Audi");

It will select value from drop down list using visible text value = "Audi".

Select By Value
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.selectByValue("Italy");

It will select value by value = "Italy".

Select By Index
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.selectByIndex(0);

Deselect by Visible Text
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectByVisibleText("Russia");

It will deselect option by visible text = Russia from list box.

Deselect by Value
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectByValue("Mexico");

It will deselect option by value = Mexico from list box.

Deselect by Index
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectByIndex(5);
It will deselect option by Index = 5 from list box.

Deselect All
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectAll();

isMultiple()
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
boolean value = listbox.isMultiple();

Navigate to URL or Back or Forward in Selenium Webdriver
driver.navigate().to("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
driver.navigate().back();
driver.navigate().forward();

Capturing entire page screenshot in Selenium WebDriver
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));

Generating Mouse Hover Event In WebDriver
Actions actions = new Actions(driver);
WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div"));
actions.moveToElement(moveonmenu).build();
actions.perform();

Handling Multiple Windows In Selenium WebDriver

Get All Window Handles
Set<String> AllWindowHandles = driver.getWindowHandles();
Extract parent and child window handle from all window handles.
String window1 = (String) AllWindowHandles.toArray()[0];
String window2 = (String) AllWindowHandles.toArray()[1];

Use window handle to switch from one window to other window.
driver.switchTo().window(window2);

Check Whether Element is Enabled Or Disabled In Selenium Web driver
boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print(fname);

Selenium WebDriver Assertions With TestNG Framework
assertEquals
Assert.assertEquals(actual, expected);

assertNotEquals
Assert.assertNotEquals(actual, expected);

assertTrue
Assert.assertTrue(condition);

assertFalse
Assert.assertFalse(condition);

Submit() method to submit form
driver.findElement(By.xpath("//input[@name='Company']")).submit();

Handling Alert, Confirmation and Prompts Popups

To store alert text
String myalert = driver.switchTo().alert().getText();

To accept alert
driver.switchTo().alert().accept();

To dismiss confirmation
driver.switchTo().alert().dismiss();

To type text In text box of prompt popup
driver.switchTo().alert().sendKeys("This Is John");


findElement()

We need to use findElement method frequently in our webdriver test case because this is the only way to locate any element in webdriver.
findElement method is useful to locating targeted single element.
If targeted element is not found on the page then it will throw NoSuchElementException.


findElements()

We are using findElements method just occasionally.
findElements method will return list of all the matching elements from current page as per given element locator mechanism.
If not found any element on current page as per given element locator mechanism, it will return empty list.

1 comment: