Skip to content

Commit

Permalink
Make this less repetitive
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-beck committed Oct 23, 2024
1 parent a96cfc1 commit b029e5e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Describable;
import org.jenkinsci.test.acceptance.po.GlobalSecurityConfig;
import org.openqa.selenium.UnhandledAlertException;

/**
* @author Kohsuke Kawaguchi
Expand All @@ -21,32 +20,17 @@ public MatrixAuthorizationStrategy(GlobalSecurityConfig context, String path) {
* Adds a new user to this matrix.
*/
public MatrixRow addUser(String name) {
try {
// 3.2.3 and later
this.table
.resolve()
.findElement(
by.xpath(
"../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']"))
.click();
getPage().find(by.css("dialog input")).sendKeys(name);
getPage().find(by.css("dialog .jenkins-button--primary")).click();
} catch (UnhandledAlertException ex) {
// 3.2.2 and earlier
runThenHandleAlert(
() -> {
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();
});
}
runThenHandleInputDialog(
() -> {
this.table
.resolve()
.findElement(
by.xpath(
"../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']"))
.click();
},
name,
"OK");
return getUser(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Job;
import org.jenkinsci.test.acceptance.po.PageAreaImpl;
import org.openqa.selenium.UnhandledAlertException;

/**
* @author Kohsuke Kawaguchi
Expand All @@ -22,32 +21,17 @@ public ProjectMatrixProperty(Job job) {
* Adds a new user/group to this matrix.
*/
public MatrixRow addUser(String name) {
try {
// 3.2.3 and later
this.table
.resolve()
.findElement(
by.xpath(
"../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']"))
.click();
getPage().find(by.css("dialog input")).sendKeys(name);
getPage().find(by.css("dialog .jenkins-button--primary")).click();
} catch (UnhandledAlertException ex) {
// 3.2.2 and earlier
runThenHandleAlert(
() -> {
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();
});
}
runThenHandleInputDialog(
() -> {
this.table
.resolve()
.findElement(
by.xpath(
"../div/span/span/button[text()='Add user\u2026'] | ../div/button[text()='Add user\u2026']"))
.click();
},
name,
"OK");
return getUser(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Alert> action) {
runThenHandleAlert(null, action);
}
Expand Down

0 comments on commit b029e5e

Please # to comment.