Reduced duplicate code to handle alerts in Selenium tests

This commit is contained in:
Steffen Schaefer committed 2017-04-05 10:01:24 +02:00
1 parent 420ccad09c
commit b2cd67238a
5 files changed
+56 -76

No files matched your search

@@ -10,7 +10,9 @@ import java.util.function.BooleanSupplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@@ -530,4 +532,54 @@ public class PageObject {
public enum TabPanelType {
TAB_PANEL, TAB_LAYOUT_PANEL, VERTICAL_TAB_LAYOUT_PANEL
}
/**
* Waits for an alert box to appear and accepts the alert. If no alert shows up, an Exception is thrown.
*/
protected void waitForAlertAndAccept() throws InterruptedException {
waitForAlertAndAccept(DEFAULT_WAIT_TIMEOUT_SECONDS);
}
/**
* Waits for an alert box to appear and accepts the alert. If no alert shows up, an Exception is thrown.
*/
protected void waitForAlertAndAccept(int timeoutInSeconds) throws InterruptedException {
int i = 0;
while (i < timeoutInSeconds) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.accept();
return;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
}
}
throw new NoAlertPresentException();
}
/**
* Waits for an alert box to appear and dismisses the alert. If no alert shows up, an Exception is thrown.
*/
protected void waitForAlertAndDismiss() throws InterruptedException {
waitForAlertAndDismiss(DEFAULT_WAIT_TIMEOUT_SECONDS);
}
/**
* Waits for an alert box to appear and dismisses the alert. If no alert shows up, an Exception is thrown.
*/
protected void waitForAlertAndDismiss(int timeoutInSeconds) throws InterruptedException {
int i = 0;
while (i < timeoutInSeconds) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.accept();
return;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
}
}
throw new NoAlertPresentException();
}
}
@@ -2,8 +2,6 @@ package com.sap.sailing.selenium.pages.adminconsole.advanced;
import java.util.function.BooleanSupplier;
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
@@ -57,24 +55,6 @@ public class MasterDataImportPO extends PageArea {
}
new Select(leaderBoardGroupListBox).selectByValue(eventName);
importBtn.click();
waitForAlertAndDispose(driver);
}
void waitForAlertAndDispose(WebDriver driver) throws InterruptedException {
int i = 0;
while (i < DEFAULT_WAIT_TIMEOUT_SECONDS) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.dismiss();
break;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
continue;
}
}
if (i >= DEFAULT_WAIT_TIMEOUT_SECONDS) {
throw new NoAlertPresentException();
}
waitForAlertAndDismiss();
}
}
@@ -1,7 +1,5 @@
package com.sap.sailing.selenium.pages.adminconsole.igtimi;
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@@ -43,20 +41,6 @@ public class IgtimiAccountsManagementPanelPO extends PageArea {
addIgtimiAccountDialog.setPassword(password);
addIgtimiAccountDialog.pressOk();
int i = 0;
while (i < DEFAULT_WAIT_TIMEOUT_SECONDS) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.accept();
break;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
continue;
}
}
if (i >= DEFAULT_WAIT_TIMEOUT_SECONDS) {
throw new NoAlertPresentException();
}
waitForAlertAndAccept(10);
}
}
@@ -1,7 +1,5 @@
package com.sap.sailing.selenium.pages.adminconsole.wind;
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@@ -27,21 +25,7 @@ public class WindPanelPO extends PageArea {
public void importWindFromIgtimi() throws InterruptedException {
importWindFromIgtimi.click();
int i = 0;
while (i < DEFAULT_WAIT_TIMEOUT_SECONDS) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.accept();
break;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
continue;
}
}
if (i >= DEFAULT_WAIT_TIMEOUT_SECONDS) {
throw new NoAlertPresentException();
}
waitForAlertAndAccept(10);
FluentWait<String> wait = createFluentWait("The following number of fixes were imported for the listed races:", /* waiting 10 minutes at most */ 10 * 60, DEFAULT_POLLING_INTERVAL);
wait.until(new Function<String, Object>() {
@@ -2,8 +2,6 @@ package com.sap.sailing.selenium.pages.raceboard;
import java.util.List;
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@@ -32,25 +30,7 @@ public class MapSettingsPO extends PageArea {
public void makeDefault() throws InterruptedException {
saveButton.click();
waitForAlertAndDispose(driver);
}
void waitForAlertAndDispose(WebDriver driver) throws InterruptedException {
int i = 0;
while (i < DEFAULT_WAIT_TIMEOUT_SECONDS) {
i++;
try {
Alert alert = driver.switchTo().alert();
alert.dismiss();
break;
} catch (NoAlertPresentException e) {
Thread.sleep(1000);
continue;
}
}
if (i >= DEFAULT_WAIT_TIMEOUT_SECONDS) {
throw new NoAlertPresentException();
}
waitForAlertAndDismiss();
}
public boolean isWindChartSelected() {