Selenium with Java 42 - How to Handle Javascript alert, confirm and prompt dialog boxes

preview_player
Показать описание
How to Handle Javascript alert, confirm and prompt dialog boxes in selenium webdriver using Java.

SwitchTo commands
Simple Alert
Prompt Alert
Confirmation Alert

SwitchTo Method in selenium webdriver :
SwitchTo method provides access to alerts, frames and windows by returning TargetLocator Interface reference.
TargetLocator provides access to alerts, frames and windows by its methods alert(), frame() and window()

Alert interface in selenium webdriver
switchTo Method returns TargetLocator interface reference.
alert method is a member of TargetLocator interface which returns Alert reference.
Alert Interface : Defines the interface through which the user can manipulate JavaScript alerts.

getText() : Gets the text of the alert.
accept() : Accepts the alert.
dismiss() : Dismisses the alert.
sendKeys() : Sends keys to the prompt.
setAuthenticationCredentials() : Sets the user name and password in an alert prompting for credentials.

How to Handle JavaScript alert?
Alert is a small message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation.
It may be also used for warning purpose.
Usage:

How to Handle JavaScript prompt?
This Prompt Alert asks some input from the user and selenium WebDriver can enter the text using sendkeys.
Usage:

How to Handle JavaScript Confirmation?
This confirmation alert asks permission to do some type of operation.
Usage:

Possible Interview Questions on browser javascript alert, confirmation and prompt dialog boxes :
What is switchTo method and in which interface it is present
What is Simple alert and how to handle it.
What is Prompt alert and how to handle it.
What is Confirmation alert and how to handle it.

Code :
@Test
public void SwitchToAlret1() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
}

@Test
public void SwitchToConfirm1() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
}

@Test
public void SwitchToPromt1() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
}
Рекомендации по теме
Комментарии
Автор

Hi All
Basic Authentication button is not working in the UITestPractice page. If it works we can work on Basic Authentication Alert. Could you please work on it

sureshbagi