Skip to main content
Version: 10.2.9

Handle JavaScript alerts and popup boxes

An alert is a pop-up window that comes up on the screen. There are many user actions that can result in alerts. For example, a user clicks a button that displays a message, and a web page asks for some extra information.

tip

For a sample, refer to a test page for alerts.

Alerts differ from regular windows. They block any action on an underlying web page if they are present on the web page, and you get the following exception:

UnhandledAlertException: Modal dialog present
UnhandledAlertException: unexpected alert open

To reproduce the exception, you can use this code:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot name="ie" driver="chrome" close-on-completion="true">
<script><![CDATA[

open('https://rpa-tutorial.s3.amazonaws.com/trainings/alert.html')

//This step will result in an alert on screen
$(byText('Simple Alert')).click()

//Once alert is present try to click on any button on the page
$(byText('Confirm Pop up')).click()

]]></script>
</robot>
</robotics-flow>
<export include-original-data="false" />
</config>

JavaScript provides mainly three types of alerts:

  • Simple alert

    document.alert("This is a simple alert");
    //or
    alert("This is a simple alert");
  • Confirmation alert

    var popuResult = confirm("Confirm pop up with OK and Cancel button");
  • Prompt alert

    var person = prompt("Do you like RPA?", "Yes/No");

To handle alerts, the RPA driver provides the alert interface (switchTo().alert()) that presents the following methods:

  • accept() accepts the alert.
  • dismiss() dismisses the alert.
  • getText() gets the text of the alert.
  • sendKeys() writes some text to the alert.

Simplified API methods that do not require switching to an alert are as follows:

  • confirm() accepts (clicks Yes or Ok) in the existing confirmation dialog (JavaScript alert or confirm).
  • confirm(String expectedDialogText) is the same as previous. If not null, expectedDialogText checks that the confirmation dialog displays this message (case-sensitive).
  • dismiss() dismisses (clicks No or Cancel) in the existing confirmation dialog (JavaScript alert or confirm).
  • dismiss(String expectedDialogText) is the same as previous. If not null, expectedDialogText checks that the confirmation dialog displays this message (case-sensitive).
import com.workfusion.odf2.compiler.BotTask;
import com.workfusion.odf2.core.task.AdHocTask;
import com.workfusion.odf2.core.task.TaskInput;
import com.workfusion.odf2.core.task.output.TaskRunnerOutput;
import com.workfusion.odf2.core.webharvest.rpa.RpaDriver;
import com.workfusion.odf2.core.webharvest.rpa.RpaFactory;
import com.workfusion.odf2.core.webharvest.rpa.RpaRunner;
import com.workfusion.rpa.driver.Driver;
import com.workfusion.rpa.helpers.RPA;
import com.workfusion.rpa.helpers.UiElement;
import com.workfusion.rpa.helpers.UiElementCollection;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static com.workfusion.rpa.helpers.RPA.*;
import static com.workfusion.rpa.helpers.UiSelectors.byImage;
import static com.workfusion.rpa.helpers.UiSelectors.byText;


@BotTask(requireRpa = true)
public class AutomateWebElements implements AdHocTask {

private final RpaRunner rpaRunner;
private final Logger logger;

@Inject
public AutomateWebElements(RpaFactory rpaFactory, Logger logger){
this.rpaRunner = rpaFactory
.builder(RpaDriver.UNIVERSAL)
.closeOnCompletion(true)
.build();
this.logger = logger;
}

@Override
public TaskRunnerOutput run(TaskInput taskInput) {

rpaRunner.execute(driver->{
this.handleJavaScriptAlertsAndPopupBoxes(driver);
});

return taskInput.asResult()
.withColumn("example_bot_task_output", "completed_successfully");
}

private void handleJavaScriptAlertsAndPopupBoxes(Driver driver){
RPA.openChrome("https://rpa-tutorial.s3.amazonaws.com/trainings/alert.html");
this.popAlert(driver," Popup Alert Example ");

// Confirming a simple alert
$(byText("Simple Alert")).click();
RPA.sleep(2000);
RPA.confirm("A simple Alert");

// Getting text and dismissing a Confirm popup
$(By.xpath("//td[2]/button")).click();
String alert_text = RPA.switchTo().alert().getText();
RPA.sleep(2000);
RPA.dismiss();


// Typing into a Prompt popup and confirming
RPA.switchTo().defaultContent();
$("td:last-child > button").click();
RPA.sleep(2000);
RPA.switchTo().alert().sendKeys("Sure");
RPA.sleep(2000);
RPA.confirm();
}

private void popAlert(Driver driver,String popupMessage){
driver.switchDriver("chrome");
//Switch to default content inorder to work with alert if already on a IFrame
RPA.switchTo().defaultContent();
String script = popupMessage;
driver.executeScript("alert(arguments[0])",script);
RPA.sleep(3000);
RPA.switchTo().alert().accept();
}

}