SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

How To Handle Javascript Alerts/PopUps In Selenium WebDriver

 How To Handle Javascript Alerts/PopUps In Selenium WebDriver   


In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.


To handle alerts popupswe need to do switch to the alert window and call Selenium WebDriver Alert API methods.
There are two types of alerts.
  1. Windows Based
  2. Web Based/Browser Based
Here in this post, I confine to Java Script Alerts (A.K.A. Browser/Web Based Alerts).
For Windows Based, Please check the below link.
To handle Browser based Alerts (Web based alert popups), we use Alert Interface. The Alert Interface provides some methods to handle the popups.
While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command :
driver.switchTo().alert();
Once we switch the control from browser to the alert window. We can use the AlertInterface methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.,
Let’s see the Alert Interface Methods.
We need to Import a package org.openqa.selenium.Alert to handle the alerts in Selenium.
To get a handle to the open alert:
Alert alert = driver.switchTo().alert();
To Click on OK button:
alert.accept(); 
To click on Cancel button.
alert.dismiss() 
To get the text which is present on the Alert.
alert.getText(); 
To enter the text into the alert box
alert.sendkeys(String stringToSend); 
To Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials) 
Example 1: Alert with OK button

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithOKButton {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("//*[@id='OKTab']/button")).click(); // click
// on
// the
// button

Thread.sleep(5000);

driver.switchTo().alert().accept(); // this will close alert box by
// clicking OK button

}


}

Example 2: Alert with OK and Cancel buttons

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithOKAndCancelButtons {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[2]/a")).click();// button1

driver.findElement(By.xpath("//*[@id='CancelTab']/button")).click(); // button2

Thread.sleep(5000);

// close alert window by pressing OK button - first time
driver.switchTo().alert().accept();

driver.findElement(By.xpath("//*[@id='CancelTab']/button")).click(); // button2
Thread.sleep(5000);

// close alert window by pressing Cacel button - second time time
driver.switchTo().alert().dismiss();

}


}

Example 3: Alert with input box

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithInputbox {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[3]/a")).click();// button1

driver.findElement(By.xpath("//*[@id='Textbox']/button")).click(); // button2

Thread.sleep(5000);

driver.switchTo().alert().sendKeys("welcome");

Thread.sleep(5000);

driver.switchTo().alert().accept();

}


}
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates