1.

How To Handle Window-Based Alerts/Pop-Ups In Selenium?

Answer»

Sometimes while automating, we come across scenarios where we need to handle pop-ups generated by windows like a print pop up or a browser WINDOW while uploading a file.

We know selenium is an automation testing tool that supports only web application testing. Selenium does not support windows based applications and automating window alert is not possible. Handling these pop-ups in selenium is always a bit tricky. However, along with third-party intervention, selenium can handle windows alert.

Many third-party tools are available for handling window-based pop-ups along with the selenium.

Let’s handle a window-based pop-up using the Robot class.

Robot class is a java based utility to emulate the keyboard and mouse actions.

We will automate one scenario

  1. Launch the browser and open “gmail.com”
  2. Enter username and password and click on the Sign button(Note SCRIPT has dummy username and password)
  3. Click on the compose icon
  4. Click on the attachment icon and select the file to upload with the window based pop up.

WebDriver Code using Robot Class

  • Step 1: Create a new java class named as “DemoAlertWindow” under project “Learning_Selenium”
  • Step 2: Import package import java.awt.Robot prior to the script creation.
  • Step 3: Import package java.awt.event.KeyEvent allows the USER to use keyPress and keyRelease events of a keyboard.

Below is the test script that is equivalent to the above-mentioned scenario.

  import java.awt.Robot;     import java.awt.event.KeyEvent;     import org.junit.After;     import org.junit.Before;     import org.junit.Test;     import org.openqa.selenium.By;     import org.openqa.selenium.WebDriver;     import org.openqa.selenium.firefox.FirefoxDriver;     public class DemoAlertWindow {     WebDriver driver;     @Before     public void setUp()     {     driver=new FirefoxDriver();     driver.get("https://gmail.com");     driver.manage().window().maximize();     }     @Test     public void testAlertWindow() throws Exception{     // enter a valid email address     driver.findElement(By.id("Email")).sendKeys("TestSelenium1@gmail.com");     // enter a valid password     driver.findElement(By.id("Passwd")).sendKeys("TestSelenium");     // click on the sign in button     driver.findElement(By.id("signIn")).click();     Thread.sleep(30000);     // click on compose button     driver.findElement(By.xpath("//div[@class='z0']//div[contains(text(),'COMPOSE')]")).click();     // click on attach files icon     driver.findElement(By.xpath("//div[contains(@command,'Files')]//div[contains(@class,'aaA')]")).click();     // creating instance of Robot class (A java based utility)     Robot rb =new Robot();     // pressing keys with the help of keyPress and keyRelease events     rb.keyPress(KeyEvent.VK_D);     rb.keyRelease(KeyEvent.VK_D);     Thread.sleep(2000);     rb.keyPress(KeyEvent.VK_SHIFT);     rb.keyPress(KeyEvent.VK_SEMICOLON);     rb.keyRelease(KeyEvent.VK_SEMICOLON);     rb.keyRelease(KeyEvent.VK_SHIFT);     rb.keyPress(KeyEvent.VK_BACK_SLASH);     rb.keyRelease(KeyEvent.VK_BACK_SLASH);     Thread.sleep(2000);     rb.keyPress(KeyEvent.VK_P);     rb.keyRelease(KeyEvent.VK_P);     rb.keyPress(KeyEvent.VK_I);     rb.keyRelease(KeyEvent.VK_I);     rb.keyPress(KeyEvent.VK_C);     rb.keyRelease(KeyEvent.VK_C);     Thread.sleep(2000);     rb.keyPress(KeyEvent.VK_ENTER);     rb.keyRelease(KeyEvent.VK_ENTER);     Thread.sleep(2000);     }     @After     public void tearDown()     {     driver.quit();     }     }


Discussion

No Comment Found