Alerts in Selenium Kotlin

Alerts in webpage used to get the attention of the user to perform some operation on the alert or on the webpage, sometimes alerts expects input from the user.

All the alerts are formed using javascript inside HTML page, there are three kind of alerts in HTML.

Alerts takes the focus away from the current window, and forces the browser to read the message.

Alert prevents the user from accessing other parts of the web page until the box is closed.

Alert Types :

  • Alert :
  • Confirmation dialog :
  • Prompt :


Properties of Alerts with respect to Selenium Kotlin :

  • We cannot identify alerts using inspect tools
  • We cannot write xpaths for alerts
  • It is not a Window
  • We cannot handle alerts using javaScript Executor.
We can consider as not alert if any of the above property mismatches.

Code of Alert handling in selenium Kotlin


When alert is present on webpage we cannot proceed further without handling the popup, and if we try to perform any operation it throws org.openqa.selenium.UnhandledAlertException.

We can handle alerts using switchTo().alert() method present in selenium Kotlin, with help of this alert() we can handle the pop up.

Syntax to handle popup


                        		val ale = driver.switchTo().alert();
                  

Technical Tip : Selenium kotlin up-cast the object to Alert Interface in above line of code With this api we can perform below operations on the pop up.
1. Accept the popUp by clicking OK button


                        		ale.accept();
                  


2. Dismiss the popUp by clicking 'X' icon.


                        		ale.dismiss();
                  


3. Get popUp text by getText method


                        		ale.getText();
                  


4. Send text to popUp by sendKeys (Applicable for Prompt only)


                        		ale.sendKeys("test Text");
                  


Note : Selenium kotlin never clicks Cancel button on any kind of alert

When does alerts occurs on a page : It can occur on a page on any time but most of the it happens on below timing of the webpage

  1. On Webpage Load
  2. On Webpage close
  3. On click of a element
  4. On Right click (when right click disabled)
  5. On wrong entry of a field
  6. On information save

Technical Tip : Now day developer are using overlays/hidden division popups rather than javascript popups

Alert

The alert() method displays an alert box with a message and an OK button, alert box is often used to inform a user about a particular operation like details saved in Database, right click disabled, Loaded successfully such kind of messages.

Alert is formed using alert("message") in javascript, alert considers same irrespective of user operation whether he clicks OK or 'X' icon.

Alert's sole purpose is to inform user, nothing more.
alert-on-chrome-selenium-webdriver

  • Open Url : https://chercher.tech/practice/practice-pop-ups-selenium-webdriver
  • Click on Alert button, application throws an Alert box
  • Switch to the alert using driver.switchTo().alert(), we save this object in Alert type variable 'ale'
  • We can accept the alert by using accept() non-static method from the alert api, this closes the popup.
  • 
    fun main(args: Array<String>) {
        System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
        val driver = FirefoxDriver()
        driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver")
        driver.findElement(By.name("alert")).click()
        val ale = driver.switchTo().alert()
        // clicks 'OK' button
        ale.accept()
    }
    										


  • We can dismiss the popup using dismiss() method from alert api, this click 'X' icon on popup. (if you have performed above step then you cannot perform this step as pop up is closed in step 4 itself)
  • 
    val ale = driver.switchTo().alert()
    // clicks 'x' icon
    ale.dismiss();
    								


  • We can get the text from the pop up using getText(), if you have performed step 4 or 5, this step will not work
  • 
    val ale = driver.switchTo().alert()
    // clicks 'x' icon
    ale.getText();
    								

    get-text-alert-selenium-webdriver

Confirmation Box

Confirmation box is second kind of alert, it displays a dialog with OK and Cancel button

Confirmation box informs developer about user choice whether user pressed OK or Cancel. The confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to developer.

Confirmation box confirm-chrome-selenium-webdriver

We can handle Confirmation box in Kotlin selenium like alert box, there is no coding difference.

Prompt

Prompt is used to get value from the user in text format. Prompt provides textbar for user input, Prompt is rarely used alert type.

Prompt : prompt-selenium-webdriver

Prompt also follows same coding as alert and prompt except sendkeys method, we can send text to prompt textbar using sendkeys() method in alerts api.

Note : You cannot use Keys.ENTER or such kind of keys with sendkeys() method of alerts


driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
driver.findElement(By.name("prompt")).click();
val ale = driver.switchTo().alert()
ale.sendKeys("chercher.tech");
										

How to check whether alert is present or Not ?

We can use WebdriverWait to check whether alert is there or not, alertIsPresent() method wait for the alert to be present till the given timeout, once it reaches timeout and if alert is not present then it throws TimeOutException, if alert is present before the timeout it proceeds with remaining code.

I have used only 2 seconds of time to check for the alert.


fun isAlertPresent(): Boolean {
        var presenceOfAlert = false
        val wait = WebDriverWait(driver, 2 /*timeout in seconds*/)
        try {
            wait.until(ExpectedConditions.alertIsPresent())
            presenceOfAlert = true
        } catch (e: TimeoutException) {
            presenceOfAlert = false
        }
        return presenceOfAlert
    }
										


Explanation :

  • We are setting presenceOfAlert to false, this is for by default value.
  • We are creating object for WebdriverWait
  • We are using try{} block to see whether code throws exception or not
  • We are checking whether alert is there or not using alertIsPresent() method. If alert is present execution goes to next line where we have set presenceOfAlert to true, but incase if alert is not there it will not execute "presenceOfAlert = true;" statement but goes to catch block.
  • We have used catch{} block to receive the exception, this catch block receives only TimeoutException
  • If try block code throws "TimeoutException" then "presenceOfAlert = false;" will be executed
  • Method returns the value based on the try catch block / based on presence of alert.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group