filmov
tv
Selenium with C# 41 - How to handle javascript alert, prompt and confirm dialog boxes

Показать описание
How to handle javascript alert, prompt and confirm dialog boxes.
SwitchTo commands
Simple Alert
Prompt Alert
Confirmation Alert
SwitchTo Method :
SwitchTo method provides access to alerts, frames and windows by returning ITargetLocator Interface reference.
ITargetLocator provides access to alerts, frames and windows by its methods Alert(), Frame() and Window()
IAlert interface :
SwitchTo Method returns ITargwtLocator interface reference.
Alert method is a member of ITargwtLocator interface which returns IAlert reference.
IAlert Interface : Defines the interface through which the user can manipulate JavaScript alerts.
Text property : Gets the text of the alert.
Accept() method : Accepts the alert.
Dismiss() method : Dismisses the alert.
SendKeys() method : Sends keys to the prompt.
SetAuthenticationCredentials() method : Sets the user name and password in an alert prompting for credentials.
Handling javascript simple alert in selenium :
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: driver.SwitchTo().Alert().Text
driver.SwitchTo().Alert().Accept()
Handling javascript prompt in selenium :
This Prompt Alert asks some input from the user and selenium WebDriver can enter the text using sendkeys.
Usage: driver.SwitchTo().Alert().Text;
driver.SwitchTo().Alert().Accept();
driver.SwitchTo().Alert().Dismiss();
driver.SwitchTo().Alert().SendKeys(“abc”);
Handling javascript confirm in selenium :
This confirmation alert asks permission to do some type of operation.
Usage : driver.SwitchTo().Alert().Text
driver.SwitchTo().Alert().Accept()
driver.SwitchTo().Alert().Dismiss();
Possible Interview Questions on javascript alert, confirm and prompt :
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 :
[TestMethod]
public void SwitchToAlret()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("alert")).Click();
Thread.Sleep(1000);
string s = driver.SwitchTo().Alert().Text;
Console.WriteLine(s);
driver.SwitchTo().Alert().Accept();
Thread.Sleep(1000);
driver.Quit();
}
[TestMethod]
public void SwitchToConfirm()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("confirm")).Click();
Thread.Sleep(1000);
driver.SwitchTo().Alert().Dismiss();
Thread.Sleep(1000);
driver.Quit();
}
[TestMethod]
public void SwitchToPromt()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("prompt")).Click();
Thread.Sleep(1000);
driver.SwitchTo().Alert().SendKeys("demo");
Thread.Sleep(1000);
driver.SwitchTo().Alert().Accept();
driver.Quit();
}
SwitchTo commands
Simple Alert
Prompt Alert
Confirmation Alert
SwitchTo Method :
SwitchTo method provides access to alerts, frames and windows by returning ITargetLocator Interface reference.
ITargetLocator provides access to alerts, frames and windows by its methods Alert(), Frame() and Window()
IAlert interface :
SwitchTo Method returns ITargwtLocator interface reference.
Alert method is a member of ITargwtLocator interface which returns IAlert reference.
IAlert Interface : Defines the interface through which the user can manipulate JavaScript alerts.
Text property : Gets the text of the alert.
Accept() method : Accepts the alert.
Dismiss() method : Dismisses the alert.
SendKeys() method : Sends keys to the prompt.
SetAuthenticationCredentials() method : Sets the user name and password in an alert prompting for credentials.
Handling javascript simple alert in selenium :
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: driver.SwitchTo().Alert().Text
driver.SwitchTo().Alert().Accept()
Handling javascript prompt in selenium :
This Prompt Alert asks some input from the user and selenium WebDriver can enter the text using sendkeys.
Usage: driver.SwitchTo().Alert().Text;
driver.SwitchTo().Alert().Accept();
driver.SwitchTo().Alert().Dismiss();
driver.SwitchTo().Alert().SendKeys(“abc”);
Handling javascript confirm in selenium :
This confirmation alert asks permission to do some type of operation.
Usage : driver.SwitchTo().Alert().Text
driver.SwitchTo().Alert().Accept()
driver.SwitchTo().Alert().Dismiss();
Possible Interview Questions on javascript alert, confirm and prompt :
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 :
[TestMethod]
public void SwitchToAlret()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("alert")).Click();
Thread.Sleep(1000);
string s = driver.SwitchTo().Alert().Text;
Console.WriteLine(s);
driver.SwitchTo().Alert().Accept();
Thread.Sleep(1000);
driver.Quit();
}
[TestMethod]
public void SwitchToConfirm()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("confirm")).Click();
Thread.Sleep(1000);
driver.SwitchTo().Alert().Dismiss();
Thread.Sleep(1000);
driver.Quit();
}
[TestMethod]
public void SwitchToPromt()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.Id("prompt")).Click();
Thread.Sleep(1000);
driver.SwitchTo().Alert().SendKeys("demo");
Thread.Sleep(1000);
driver.SwitchTo().Alert().Accept();
driver.Quit();
}
Комментарии