Handle confirm and Prompt Pop-up in Selenium C#

In this tutorial we will learn how to handle JavaScript Confirmation and Prompt Pop-up using Selenium Web Driver in C#.

Automate Confirmation Pop-up in Selenium C#

In order to use the Confirmation Pop-up the steps are same, first we need to Switch to the confirmation Pop-up with the help of “SwitchTo().Alert()”. If we want to click on the Ok button at the confirmation Pop-up we will use the “Accept()” method and if we want to click on the Cancel button at the confirmation Pop-up we will use the “Dismiss()” method.

Let’s see an example of Confirmation Pop-up-

If we click on “Try it” button so it will give us confirmation Pop-up, if we click on Ok button this message will be display “You pressed ok” and if we click on Cancel button then this message will be display “You pressed cancel”. So we are going to handle this confirmation Pop-up with the help of web driver.

In TestPopups.cs we will create one more method TestConfirmPopup().

[TestMethod]
public void TestConfirmPopup()
{
NavigationHelper.NavigateToUrl("http://www.w3schools.com/js/tryit.asp?filename=tryjs confirm");
BrowserHelper.SwitchToFrame(By.Id("iframeResult"));
ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']"));
IAlert confirm= ObjectRepository.Driver.SwitchTo().Alert();
confirm.Accept();
ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']"));
confirm= ObjectRepository.Driver.SwitchTo().Alert();
confirm.Dismiss();
}

Explanation of code:

First we are Navigating to the required URL with the help of NavigationHelper, after that if we analyse this particular Try it button so as we can see here it is present inside the frame so we need to switch to the frame BrowserHelper.SwitchToFrame(By.Id(“iframeResult”));

After that we will click on Tty it button to generate the confirmation Pop-up ButtonHelper.ClickButton(By.XPath(“//button[text()=’Try it’]”));

Once the pop-up is generated here we need to switch to this Pop-up and as we know that it is going to return us the variable of type IAlert so IAlert confirm= ObjectRepository.Driver.SwitchTo().Alert();

Now for clicking on ok button we will call confirm.Accept() method and again we will generate the Pop-up and now for clicking on Cancel button we will call confirm.Dismiss() method.

Now pit a breakpoint on first ButtonHelper line, build the solution and run the script in debug mode.

Output: so we are going to do a stepover and it will click on Try it button as we can see here we have a confirmation Pop-up with ok and cancel button. If we do a stepover again it will switch to the confirmation Pop-up then it will click on Ok button as we can see here the message.

Again we will generate the Pop-up and this time we will click on the cancel button so we can see the message here. So in this manner we can handle the confirmation Pop-up with web driver.

Making the HelperClass of confirmation Pop-up:

Now we are going to add one more method inside our framework class here we have already a method which is ClickOkOnPopup() so we will add one more method which is ClickCancelOnPopup()

public static void ClickCancelOnPopup()
{
if(! IsPopupPresent())
       return;
  ObjectRepository.Driver.SwitchTo().Dismiss();
}

In our TestConfirmPopup() method we will comment out some piece of code.

[TestMethod] 
public void TestConfirmPopup() 
{ 
NavigationHelper.NavigateToUrl("http://www.w3schools.com/js/tryit.asp?filename=tryjs confirm"); BrowserHelper.SwitchToFrame(By.Id("iframeResult")); ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']")); 
var text= JavaScriptPopHelper.GetPopUpText();
JavaScriptPopHelper.ClickOkOnPopup();
//IAlert confirm= ObjectRepository.Driver.SwitchTo().Alert(); //confirm.Accept(); ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']")); 
JavaScriptPopHelper.ClickCancelOnPopup();
ObjectRepository.Driver.SwitchTo(). DefaultContent();
TextBoxHelper.ClearTextBox(By.Id("textareaCode"));
TextBoxHelper.TypeInTextBox(By.Id("textareaCode",text);
//confirm= ObjectRepository.Driver.SwitchTo().Alert(); //confirm.Dismiss(); 
}

So here again after generating the confirmation Pop-up we will get the text so var text= JavaScriptPopHelper.GetPopUpText(); after that we will click on ok button so JavaScriptPopHelper.ClickOkOnPopup(); now again we will generate the Pop-up and this time we are going to click on Cancel button so JavaScriptPopHelper.ClickCancelOnPopup(); after that we will switch to DefaultContent and again we will supply the Pop-up text inside the text are.

Output: if we will do the stepover it will click on Try it button now the confirmation Pop-up is here we will get the text. So this is the text from the confirmation Pop-up then we will accept it as we can see here the message again generate the confirmation Pop-up and this time we will click on Cancel button and then switch to DefaultContent, then Clear the textarea and supply the pop-up string to the textarea.

Automate Prompt Pop-up in Selenium C#

Now the next type of Pop-up is the Prompt Pop-up it is similar to the Confirmation Pop-up but it has an additional field where user can supply the input.

So the procedure of handling the Prompt Pop-up is same first we need to call the SwitchTo().Alert() for for switching to Prompt.

Then we will call the Accept() method if we need to click on the Ok button, we will call the Dismiss() method if we want to click on Cancel button and in order to supply an input the SendKeys() method will use.

Let’s see an example of Prompt Pop-up-

As we can see here it has Ok and Cancel button just like the Confirmation Pop-up but along with that we have an additional field where we can supply the input.

Here if we type “test” so it will display this message “Hello test ! How are you today ?” And if we will supply any value and click on Cancel the it will not take it.

In TestPopups.cs we will add one more method called TestPrompt().

[TestMethod]
public void TestPrompt()
{
NavigationHelper.NavigateToUrl("http://www.w3schools.com/js/tryit.asp?filename=tryjs prompt"); BrowserHelper.SwitchToFrame(By.Id("iframeResult")); ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']")); 
IAlert prompt= ObjectRepository.Driver.SwitchTo().Alert();
prompt.SendKeys("This is automation");
prompt.Accept();
ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']")); 
prompt=ObjectRepository.Driver.SwitchTo().Alert();
prompt.SendKeys("This is automation"); 
prompt.Dismiss(); 
}

Explanation of code:

First of all we need to navigate to the location so first we analyse this button, the button is present in the same frame so we will use the first three lines in our method as used in the previous method but here the URL will be (“http://www.w3schools.com/js/tryit.asp?filename=tryjs prompt”) this.

Now we need to switch to the Prompt so IAlert prompt= ObjectRepository.Driver.SwitchTo().Alert(); now we will call prompt.SendKeys(“This is automation”); and then we will click on Ok button prompt.Accept();

After that again we will generate the Prompt and this time we will click on the Cancel button so we will call prompt.Dismiss(); method.

Here if we supply any value suppose “asd” so it will display this message “Hello asd ! How are you today ?” And if we supply the value and click on Cancel so the message will remain same as the previous value.

Here we will put a breakpoint at IAlert prompt= ObjectRepository.Driver.SwitchTo().Alert(); line and build the solution and run the script in debug mode.

Output: we will do the stepover so we have clicked on Try it button and we have Prompt Pop-up here then we will switch to Prompt after that we will supply the string and click on Ok button, so as we can see here is the message have been displaying.

Again we will click on Try it button to generate the Prompt then switch to the Prompt, supply the key and click on Cancel so try this is the message which is coming from the previous action.

Making the HelperClass of Prompt Pop-up

So in our framework we are going to add one more method to handle the Prompt. There we have already two methods for clicking on Ok and Cancel button here we will add another method for supplying the input.

public static void SendKeys(string text) 
{ 
if(! IsPopupPresent()) 
     return; 
  ObjectRepository.Driver.SwitchTo().Alert().SendKeys(text); 
}

So in our TestPrompt() method we will comment out some lines of code and there we will write

[TestMethod] 
public void TestPrompt()  
{ NavigationHelper.NavigateToUrl("http://www.w3schools.com/js/tryit.asp?filename=tryjs prompt"); BrowserHelper.SwitchToFrame(By.Id("iframeResult")); ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']"));  
//IAlert prompt= ObjectRepository.Driver.SwitchTo().Alert(); //prompt.SendKeys("This is automation");  
//prompt.Accept(); //ButtonHelper.ClickButton(By.XPath("//button[text()='Try it']"));  
//prompt=ObjectRepository.Driver.SwitchTo().Alert(); //prompt.SendKeys("This is automation");  
//prompt.Dismiss();  
var text= JavaScriptPopHelper.GetPopUpText();
JavaScriptPopHelper.SendKeys(text);
JavaScriptPopHelper.ClickOkOnPopup();
ObjectRepository.Driver.SwitchTo().DefaultContent();
TextBoxHelper.ClearTextBox(By.Id("textareaCode")); TextBoxHelper.TypeInTextBox(By.Id("textareaCode",text);
}

We have write var text= JavaScriptPopHelper.GetPopUpText(); then we will supply the text to the Prompt JavaScriptPopHelper.SendKeys(text); then we will click on Ok so JavaScriptPopHelper.ClickOkOnPopup(); after this again we will switch back to the default content so ObjectRepository.Driver.SwitchTo().DefaultContent(); then we will clear the text area and supply the text from the Prompt to text area. Now put a breakpoint on ButtonHelper.ClickButton(By.XPath(“//button[text()=’Try it’]”)); this line build the solution and run the script in debug mode.

Output: so we will do the stepover to click on the Try it button for generating the Prompt for us then get the text from the Prompt then supply the text to the Prompt, click on Ok as we can see this is the whole message then switch to the default content, clear the text area and supply the string to the TextBox.

So in this manner we can handle JavaScript Pop-ups with the help of Selenium Web Driver in C#.