From 122bbab94b60edc14b1894dd3aa736cb759e71e9 Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Wed, 23 Oct 2024 12:56:08 +0200 Subject: [PATCH] Adapt to dialog changes in matrix-auth 3.2.3 (#1804) Co-authored-by: Daniel Beck --- .../MatrixAuthorizationStrategy.java | 8 +++--- .../matrix_auth/ProjectMatrixProperty.java | 8 +++--- .../po/CapybaraPortingLayerImpl.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/MatrixAuthorizationStrategy.java b/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/MatrixAuthorizationStrategy.java index c76a16205..a5a044e17 100644 --- a/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/MatrixAuthorizationStrategy.java +++ b/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/MatrixAuthorizationStrategy.java @@ -20,17 +20,15 @@ public MatrixAuthorizationStrategy(GlobalSecurityConfig context, String path) { * Adds a new user to this matrix. */ public MatrixRow addUser(String name) { - runThenHandleAlert( + runThenHandleInputDialog( () -> this.table .resolve() .findElement( by.xpath( "../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']")) .click(), - a -> { - a.sendKeys(name); - a.accept(); - }); + name, + "OK"); return getUser(name); } diff --git a/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/ProjectMatrixProperty.java b/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/ProjectMatrixProperty.java index 781f5626f..1ffe45df9 100644 --- a/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/ProjectMatrixProperty.java +++ b/src/main/java/org/jenkinsci/test/acceptance/plugins/matrix_auth/ProjectMatrixProperty.java @@ -21,17 +21,15 @@ public ProjectMatrixProperty(Job job) { * Adds a new user/group to this matrix. */ public MatrixRow addUser(String name) { - runThenHandleAlert( + runThenHandleInputDialog( () -> this.table .resolve() .findElement( by.xpath( "../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']")) .click(), - a -> { - a.sendKeys(name); - a.accept(); - }); + name, + "OK"); return getUser(name); } diff --git a/src/main/java/org/jenkinsci/test/acceptance/po/CapybaraPortingLayerImpl.java b/src/main/java/org/jenkinsci/test/acceptance/po/CapybaraPortingLayerImpl.java index 44f5802a5..2de1717ac 100644 --- a/src/main/java/org/jenkinsci/test/acceptance/po/CapybaraPortingLayerImpl.java +++ b/src/main/java/org/jenkinsci/test/acceptance/po/CapybaraPortingLayerImpl.java @@ -424,6 +424,31 @@ public void runThenHandleDialog(Runnable runnable) { } } + /** + * Executes the runnable, then attempts to write {@code input} in a dialog's input field and click the button with the specified label {@code buttonLabel}. + * If an alert appears, instead the runnable is re-run, the input written to the alert, then the alert is submitted. + * + * @param runnable the runnable to run that causes a dialog to appear + * @param input the text to input into the dialog or alert + * @param buttonLabel the button of the dialog to click + */ + public void runThenHandleInputDialog(Runnable runnable, String input, String buttonLabel) { + try { + runnable.run(); + waitFor(by.button(buttonLabel)); + find(by.css("dialog input")).sendKeys(input); + clickButton(buttonLabel); + } catch (UnhandledAlertException uae) { + runThenHandleAlert( + runnable, + a -> { + a.sendKeys(input); + a.accept(); + }, + 2); + } + } + public void handleAlert(Consumer action) { runThenHandleAlert(null, action); }