Dialog Service
Learn how to use BELLATRIX dialog service.
Example
public class DialogServiceTests extends WebTest {
@Test
public void acceptDialogAlert() {
app().navigate().to("http://demos.bellatrix.solutions/welcome/");
var couponButton = app().create().byId(Button.class, "couponBtn");
couponButton.click();
app().dialogs().handle();
}
@Test
public void happyBirthdayCouponDisplayed_When_ClickOnCouponButton() {
app().navigate().to("http://demos.bellatrix.solutions/welcome/");
var couponButton = app().create().byId(Button.class, "couponBtn");
couponButton.click();
app().dialogs().handle(a -> Assert.assertEquals(a.getText(), "Try the coupon- happybirthday"));
}
@Test
@Ignore
public void dismissDialogAlert() {
app().navigate().to("http://demos.bellatrix.solutions/welcome/");
var couponButton = app().create().byId(Button.class, "couponBtn");
couponButton.click();
app().dialogs().handle(DialogButton.CANCEL);
}
}
Explanations
BELLATRIX gives you some methods for handling dialogs.
app().dialogs().handle();
You can click on the OK button and handle the alert.
app().dialogs().handle(a -> Assert.assertEquals(a.getText(), "Try the coupon- happybirthday"));
You can pass an anonymous lambda function and do something with the alert.
app().dialogs().handle(DialogButton.CANCEL);
You can tell the dialog service to click a different button.
Playwright
Playwright auto-dismisses dialogs but in case you want to handle them yourself or if Playwright is unable to dismiss them on its own, there are slightly different methods you need to use.
@Test
public void acceptDialogAlert()
{
app().navigate().to("https://developer.mozilla.org/en-US/play");
var clearButton = app().create().byXpath(Button.class, "//button[@id='clear']");
app().dialogs().runAdnWaitForDialog(clearButton::click, 1000);
app().dialogs().accept();
}
You pass the logic that will trigger a dialog to this runAndWaitForDialog method and you give timeout in milliseconds. In case you don’t specify the timeout, it will set the timeout for actionTimeoutWhenHandlingDialogs from the config file. If you don’t use this method, no listener would be added and thus you won’t be able to do the custom logic for the Dialog.
Available methods in this DialogService are:
Dialog dialog = app().dialogs().runAdnWaitForDialog(() -> clearButton.click(), 1000);
Adds an Supplier
app().dialogs().accept();
Accepts the dialog.
app().dialogs().accept("prompt text");
Types the prompt text in the dialog and then accepts it.
app().dialogs().dismiss();
Dismisses the dialog.
String message = app().dialogs().getMessage();
Gets the dialog’s message.