GXtest UI Commands - Alerts

Official Content
This documentation is valid for:

JavaScript Alerts and Confirmation messages are not common in GeneXus applications, but in case you need to handle this type of dialogs, you can use the following commands.

Warning: GXtest recorder has a bit different model of handling alerts (Canceling or Accepting alerts are commands that needs to run before the alert is shown). To understand the full behavior of these commands, try to run the example

AlertAccept

Goal: To accept an Alert or Confirmation message.

Returns: n/a

Example of use: 

&driver.AlertAccept()

AlertDismiss

Goal: To dismiss an Alert or Confirmation message.

Returns: n/a

Example of use: 

&driver.AlertDismiss()

GetAlertText

Goal: To retrieve the Alert / Confirmation message.

Returns: the message of the Alert / Confirmation

Example of use: 

//Get AlertText and check the value using Assertions:
AssertStringEquals(&driver.GetAlertText(), "Are you sure?", "unexpected alert text")

Example

//Start webdriver
&driver.Start()
&driver.Maximize()

// Open a webpage that handles Alerts and Confirmation messages:
&driver.Go("https://the-internet.herokuapp.com/javascript_alerts")

//Open the alert, Accept it and then check that was accepted 
&driver.ClickByCSS("button")
AssertStringEquals("I am a JS Alert",&driver.GetAlertText(),  "Unexpected alert text")
&driver.AlertAccept()
AssertStringEquals("You successfuly clicked an alert",&driver.GetTextByID("result"),"alert was not accepted")

//Open the confirmation dialog, Accept it and then check that was accepted 
&driver.ClickByXPath("//button[@onclick='jsConfirm()']")
AssertStringEquals("I am a JS Confirm",&driver.GetAlertText(),  "Unexpected confirmation text")
&driver.AlertAccept()
AssertStringEquals("You clicked: Ok",&driver.GetTextByID("result"),"message was not confirmed")

//Open the confirmaion dialog and dismiss it,  then check that was dismissed
&driver.ClickByXPath("//button[@onclick='jsConfirm()']")
AssertStringEquals("I am a JS Confirm",&driver.GetAlertText(),  "Unexpected confirmation text")
&driver.AlertDismiss()
AssertStringEquals("You clicked: Cancel",&driver.GetTextByID("result"),"message was not canceled")

&driver.End()