Pages

How To Handle Alert dialogs in Selenium WebDriver

Handling Alerts:
===========================================
click some element to get the alert
//switching cursor to alert box
Alert alt=driver.switchTo().alert();

//fetch the alert message
String msg=alt.getText();
//print alert message to the console
SOP(alt.getText());

//clicking on alert ok button
alt.accept();

//prompt handling
//switching cursor to the prompt dialog box
Alert prmpt=driver.switchTo().alert();

//fetching the text of the prompt
String msgp=prmpt.getText();

//entering the data in the prompt editbox
prmpt.sendKeys("value");

//clicking on prompt ok button
prmpt.accept();

//clicking on prompt cancel button
prmpt.dismiss();

authenticateUsing(Credentials credentials):Authenticate an HTTP Basic Auth dialog.
 driver.switchTo().alert().authenticateUsing(new UsernamePasswordCredentials("cheese", "secretGouda"));
 (or)
driver.get(http://username+":"+password+"url");

===========================================================
confirmation handling
===================================================
//switching to the confirmation
Alert cnf=driver.switchTo().alert();

//fetch the text of the confirmation
String cmsg=cnf.getText();

//click on confirmation ok button
cnf.accept();

//click on cancel button

cnf.dismiss();
=========================================== 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.