Tuesday 11 February 2020

Handling JavaScript Popup in Selenium

https://www.w3schools.com
There are three types of JavaScript pop-up-
  1. Alert 
  2. Confirm 
  3. Prompt
The question here come in to mind is- How do we identify the current pop-up is javascript pop-up?
Answer is -
  • Javascript pop-up doesn't have close (X) button on top right corner.
  • We couldn't do anything on pageunless we handle javascript pop-up.
  •  Even we couldn't minimize or maximize the javascript pop-up.

To handle javascript pop-up in selenum we have to follow below steps-

  1. Navigate to site/URL.
  2. Once the pop-up is opened the we need to Switch to alert using- driver.switchTo().alert();
  3. perform operations like- accept, Dismiss, sendKeys & getText.  

Handling Alert pop-up:


Alert pop-up has only ok button in selenium we have respective accept button. 

e.g.

 
 Program:


package popup;

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import selenium.Browser;

public class JavaScriptPopUp 
{
 
   @Test(enabled=true)
   public void f() throws InterruptedException 
   { 
 
    System.setProperty("webdriver.gecko.driver", "D:\\geckodriver\\geckodriver.exe");
 
    driver = new FirefoxDriver(); 
 
    driver.get("https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
        
    driver.switchTo().frame("iframeResult");
    
    driver.findElement(By.tagName("button")).click();
    
    
    Thread.sleep(5000);
    
    Alert alert = driver.switchTo().alert();
    System.out.println("Test: "+alert.getText());
    alert.accept();

     }
}



Handling Prompt pop-up:

  • A prompt box is often used if you want the user to input a value before entering a page.
  • When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
  • If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
e.g.
                     






Program:


package popup;

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import selenium.Browser;

public class JavaScriptPopUp 
{
   
   @Test(enabled=true)
   public void prompt() throws InterruptedException 
   {
    WebDriver driver = Browser.getBrowser("ff");
    Browser.goToPage("https://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt");
    driver.switchTo().frame("iframeResult");
    driver.findElement(By.tagName("button")).click();
       
    Thread.sleep(5000);
    
    Alert alert = driver.switchTo().alert();
    System.out.println("Test: "+alert.getText());
    
    alert.sendKeys("Avinash");
    
    Thread.sleep(2000);
    
    alert.accept();
    WebElement ele = driver.findElement(By.tagName("p"));
    System.out.println(ele.getText());
   }
   
}
 
 
 

Handling Confirm Pop-Up:


  • A confirm box is often used if you want the user to verify or accept something.
  • When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
  • If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

e.g.






Program:

package popup;

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import selenium.Browser;

public class JavaScriptPopUp 
{

@Test(enabled=true)
   public void confirmTest() throws InterruptedException 
   
   {
    WebDriver driver = Browser.getBrowser("ff");
    Browser.goToPage("https://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm");
    driver.switchTo().frame("iframeResult");driver.findElement(By.tagName("button")).click();
       
    Thread.sleep(5000);
    
    Alert alert = driver.switchTo().alert();
    System.out.println("Test: "+alert.getText());
    
    alert.dismiss();
    
    WebElement ele = driver.findElement(By.tagName("p"));
    System.out.println(ele.getText());
   
           }

}


References:

https://www.w3schools.com/js/js_popup.asp