SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Closing All Tabs Using Robot Class In Selenium WebDriver

Earlier we learnt how to work with multiple window tabs In single browser window In selenium WebDriver In THIS POST. Now supposing there are multiple tabs open In browser window and you wants to close all of them In one shot then how you will do It? Simple way Is using driver.quit();
method of selenium WebDriver. Can we do It using any other way? Yes.

Earlier we have used java robot class to save Image from page In THIS POST. Robot class Is useful to send key-press and key-release events. We will use same thing here to perform keyboard ALT + SPACE + "c" (Shortcut key)  key-press events to close all tabs of browser. We can perform this key-press event sequence very easily using Robot class. I am suggesting you to use always WebDriver's driver.quit(); method to close all tabs of browser. Intention of this post Is to make you more aware about Robot class usage with selenium WebDriver so you can perform such tricky actions easily whenever required.

Bellow given example has used Robot class In @AfterTest method to close browser tabs using ALT + SPACE + 'c' key-press event sequence. Run It In your eclipse IDE to check how It works.

package Testing_Pack;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Tabs {

 WebDriver driver;
 Robot rb;
 
 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");
 }
 
 @Test
 public void openTab() {  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
  driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
  switchToTab();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='plus']"));
  driver.findElement(By.xpath("//input[@id='3']"));
  driver.findElement(By.xpath("//input[@id='equals']"));
  
  switchToTab();
  driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
  driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
  
  switchToTab();
  String str = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Sum result Is -> "+str);
 } 
 
 public void switchToTab() {  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");  
  driver.switchTo().defaultContent();  
 }
 
 @AfterTest
 public void closeTabs() throws AWTException {
  //Used Robot class to perform ALT + SPACE + 'c' keypress event.
  rb =new Robot();
  rb.keyPress(KeyEvent.VK_ALT);
  rb.keyPress(KeyEvent.VK_SPACE);
  rb.keyPress(KeyEvent.VK_C);
 }
}

This way we can use Robot class to perform keyboard shortcut key operations.

4 comments: