How to handle Alert using selenium

Generally alert pop up are of three types:
1. Alert pop up with OK button
2. Alert pop up with OK and Cancel button
3. Alert pop up with OK , Cancel button and Text field to enter values.
To deal with alert pop up selenium comes with simple method Alert();
In the first case where we only have OK button below mentioned code snippet will be helpfull.
Alert alert = driver.switchTo().alert();
alert.accept();
Above code snippet will switch the control to alert and it will accept it.
In the second case where we have cancel button as well, code snippet will be:
Alert alert = driver.switchTo().alert();
alert.accept() — To accept the alert
alert.dismiss() — To dismiss the alert
In the last case where we are supposed to enter some value and click either OK or Cancel , code snippet will be:
Alert alert = driver.switchTo().alert();
alert.sendKeys(“value to enter”);
alert.accept(); or alert.dismiss();

Comments

Popular posts from this blog

API Concepts