November 10

0  comments

1. Java alerts/popups handling using Selenium WD (getText(), accept() methods)
2. Another example of JS Alert
3. Example of JS Confirm
4. Example of JS Prompt

Java alerts/popups handling using Selenium WD (getText(), accept() methods)

Launch

Scroll at the page bottom. You will see ‘Display’ section having ‘Confirmation Alert’ button

Click ‘Confirmation Alert’ button to popup a message

We cannot right click on this popup alert. The reason being, this popup is a java alert popup and it is not web related. Since it not web related, we cannot inspect this popup by right click. To identify this popup, we will have to switch the driver from webpage to popup using ‘Alert’ class. However note that you should use this ‘Alert’ class only if it java alert, not always. 

‘Alert’ is an interface & you can read the api documentation here Notice below that this interface has methods such as accept(), dismiss(), getText() etc

Before we perform any action on java alert popups, we have to first switch to the alert popup using the syntax: switchTo().alert().accept()

Let us inspect ‘Confirmation Alert’, it is an ‘input’ type button

In line#15, we are clicking the ‘Confirmation Alert’ button 

Let us run the script, notice below that the popup comes up

We will now switch to this alert popup (line#16) and print the popup text using getText() method

Run the script, notice below that the text gets printed on the console

Using accept() method, selenium clicks the ‘Ok’ button on the popup (line#17) 



Run the script, you will see the alert popup getting closed

Another example of JS Alert

Similarly, let us launch

Click “Click for JS Alert”, the below popup would come up

Let us use the same logic to extract the text and accept the popup

Run the script, the text message is printed in the console and the popup gets closed

Example of JS Confirm

When we click ‘Click for JS Confirm’, we get a popup that has ‘Cancel’ button as well

We can now use the dismiss() method to cancel and close the popup

Run the script, the text gets printed in console and the popup closes

Example of JS Prompt

Next, let us look at “JSPrompt”, when we click this, we get a prompt having a text field wherein we can enter some text

The below code does the same

Run the script, notice below that the text gets typed in the text field

Also the text on the popup gets printed in console

To close the popup, we can simply use the accept() method

Run the script, the text that we entered in the text field gets shown in the ‘Result’ section

We can put a simple validation as shown below

Run the script, notice that the SOP message gets printed in the console

So this is how we can work with JS alerts and popups.

Thank you.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}