Earlier nosotros learnt how to operate alongside multiple window tabs In unmarried browser window In selenium WebDriver In THIS POST. Now supposing at that topographic point are multiple tabs opened upwardly In browser window too you lot wants to unopen all of them In 1 shot hence how you lot volition produce It? Simple agency Is using driver.quit();
method of selenium WebDriver. Can nosotros produce It using whatever other way? Yes.
Earlier nosotros convey used coffee robot shape to salve Image from page In THIS POST. Robot shape Is useful to ship key-press too key-release events. We volition role same affair hither to perform keyboard ALT + SPACE + "c" (Shortcut key) key-press events to unopen all tabs of browser. We tin bathroom perform this key-press lawsuit sequence really easily using Robot class. I am suggesting you lot to role ever WebDriver's driver.quit(); method to unopen all tabs of browser. Intention of this post service Is to brand you lot to a greater extent than aware nearly Robot shape usage alongside selenium WebDriver hence you lot tin bathroom perform such tricky actions easily whenever required.
Bellow given representative has used Robot shape In @AfterTest method to unopen browser tabs using ALT + SPACE + 'c' key-press lawsuit sequence. Run It In your eclipse IDE to cheque 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; populace shape Tabs { WebDriver driver; Robot rb; @BeforeTest populace void setup() throws Exception { driver = novel FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get(" "); } @Test populace void openTab() { driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); driver.get(" "); 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 upshot Is -> "+str); } populace void switchToTab() { driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t"); driver.switchTo().defaultContent(); } @AfterTest populace void closeTabs() throws AWTException { //Used Robot shape 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 agency nosotros tin bathroom role Robot shape to perform keyboard shortcut primal operations.
http://www.software-testing-tutorials-automation.com/