Automation Using Selenium Webdriver

Tuesday 24 January 2017

Closing All Tabs Using Robot Class In Selenium WebDriver

Closing All Tabs Using Robot Class In Selenium WebDriver

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 postIs 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 methodto 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://use any website try ur self");
 }

 @Test
 public void openTab() {  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
  driver.get("http:try your own");
  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);
 }
}

2 comments:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.DataScience with Python Training in Bangalore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete