Selenium C# Tutorial: Handling Alert Windows
Automation Testing • Selenium C# • Selenium Tutorial •
48119 Views | 7 Min Read
Alert windows are pop-up windows that are extensively used across different types of websites or web applications. Alert window alerts the user and expects an action from the user post that alert.
Table of Content
Types Of Alerts In Selenium C#
The different types of alerts in Selenium C# are:
- Simple Alert – This type of alert in Selenium C# is used for displaying warning or certain information on the screen.
- Confirmation Alert – This type of alert in Selenium C# seeks permission from the user to continue performing the intended operation. The preferred options could be ‘Yes’ (Continue) or ‘No’ (Cancel).
- Prompt Alert – This alert in Selenium C# expects the user to enter some information into the text box.
One major difference between alert window and a regular window is that alert window is blocking in nature i.e. the user needs to perform some operation on the alert window before proceeding further on the website (or web application).
Operations On Alerts In Selenium C#
The operation being performed on an alert window depends on the type of alert being used. The common operations on alert windows are:
- Dismiss – It is used to dismiss (i.e. cancel) the alert.
- Accept – It is used to accept the alert in Selenium C#.
- Text – It is used to read the message from the alert window
- SendKeys – It is used to enter some textual information in the text box provided in the alert window
Shown below is an example of alert window in Selenium C#
For performing valid operations on the alert in Selenium C#, a SwitchTo() operation has to be performed to switch to the alert window. Once the current window is the alert window, appropriate operation i.e. accept, dismiss, etc. can be performed on the alert.
In the further section of the Selenium C# tutorial, we look at Selenium C# examples that use the NUnit test framework for demonstrating usage of alert windows including authentication alerts. In case you haven’t set up visual studio, you can read our article to get a clearer understanding about it.
Handling Different Alert Types Using Selenium C#
This part of the alerts in Selenium C# tutorial caters to handling alert types such as simple, confirmation, and prompt alerts. As the changes in implementation for handling these alerts are not significant hence, we have combined the three in a single source file.
[SetUp], [Test], [Order], and
[TearDown] are used in the implementation. In one of the earlier blogs, we covered the NUnit framework in great detail.
There are four test cases that show the handling of alert operations such as accept, dismiss, and SendKeys.
Test Case 1 – test_alert()
An explicit wait of 10 seconds is added to ensure that the alert button (I am a JS Alert) is loaded on the page.
Once the button is loaded, a Click() operation is performed to load the alert window.
Once the alert window is loaded, the
SwitchTo() command is used to switch the context to the alert window. The alert is accepted using the accept() method.
The end result which appears in the web element with id=result is compared with expected resultant string.
Test Case 2 – test_confirm()
The alert window pops up on clicking the button ‘Click for JS Confirm’. Like the previous test case, an explicit wait is performed so that the button is loaded.
Once the alert window
‘I am a JS Confirm’ is loaded, accept() method is used to accept the alert operation.
Test Case 3 – test_dimiss()
The alert window used in this test case is the same as the one is test case 2 (test_confirm). The only difference is that instead of the accept() method for alert in Selenium C#, the dismiss method is used to dismiss the alert window.
Once we switch to the alert window, the alert is dismissed using the dismiss method. The resultant text is ‘You clicked: Cancel’.
Test Case 4 – test_sendalert_text()
The button with
CssSelector = button[onclick='jsPrompt()'] is located using the Inspect tool in Chrome. Once the button is located, a click operation is performed to trigger the alert.
Once the alert window is opened, the SendKeys() method for alert in Selenium C# is used to populate the alert text box with the text
‘This is a test alert message’. Once the text is entered, accept() method is used to accept the alert message.
Here is the output snapshot from Visual Studio 2019. As shown below, all the four tests have passed.
Handling Authentication Alerts In Selenium C#
Authentication alerts (or pop-ups) are used in websites when the user needs to have valid credentials to access the features of the site. The credentials would normally comprise of a valid user-name and password.
Here is an example of an authentication where you need to enter valid credentials to access the website.
The preferred way of handling authentication alerts in Selenium C# is by passing user-credentials in the URL. After appending user-name and password to the URL, the final URL will be http://[user-name]:[password]@website.com
The site used for demonstration is https://the-internet.herokuapp.com/basic_auth and its credentials are username – admin & password – admin. Hence, the URL on which we perform Selenium test automation is: http://admin:firstname.lastname@example.org/basic_auth
As we have passed valid credentials in the URL, login is successful.
The output message on the screen is compared with the expected message.
Here is the execution snapshot from Visual Studio and Chrome browser
Also read: Using Explicit and Fluent Wait in Selenium
Alert windows are commonly used in websites for warning or information users present on the site. Alerts are normally triggered on the click of a button. Accept, dismiss, and SendKeys are the widely used methods for handling alert in Selenium C#. Selenium test automation for websites that pop-up authentication alerts is also performed using the same fundamentals as normal alert windows. If you like you can also visit our Previous Selenium C# tutorial on setting up visual studio, implicit waits, explicit & fluent wait.
Update: We’ve now completed the Selenium C# tutorial series, so in order to help you easily navigate through the tutorials, we’ve compiled the complete list of tutorials, which you can find in the section below.
Selenium C# Tutorials With Examples
- Selenium C# Tutorial: Setting Up Selenium In Visual Studio
- Running First Selenium C# Script With NUnit
- Selenium C# Tutorial: Using Implicit Wait in Selenium
- Selenium C# Tutorial: Using Explicit and Fluent Wait in Selenium
- Selenium C# Tutorial: Handling Alert Windows
- Selenium C# Tutorial: Handling Multiple Browser Windows
- Selenium C# Tutorial: Handling Frames & iFrames With Examples
- Selenium C#: Page Object Model Tutorial With Examples
Happy Testing! Do leave your feedback & suggestions in the comments section so that we can co-learn and contribute to jointly solving Selenium test automation problems!
Written by Himanshu Sheth
With 7+ years of experience in test automation, BDD, TDD, Himanshu Sheth work as an automation engineer with LambdaTest. He is also an avid blogger.
2773 Views | 13 Min Min Read
9180 Views | 6 Min Min Read
19918 Views | 8 Min Min Read
26445 Views | 12 Min Min Read