Bug 4339: Removed code duplication

This commit is contained in:
Steffen Schaefer committed 2018-07-24 08:39:50 +02:00
1 parent 5ab1bf1815
commit de6394b3c1
3 files changed
+36 -48

No files matched your search

@@ -20,7 +20,6 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
@@ -177,18 +176,6 @@ public class SeleniumRunner extends ParentRunner<SeleniumJUnit4ClassRunner> {
DesiredCapabilities capabilities = new DesiredCapabilities(capabilityDefinitions);
Constructor<WebDriver> constructor = clazz.getConstructor(Capabilities.class);
WebDriver driver = constructor.newInstance(capabilities);
try {
driver.manage().window().maximize();
} catch (Exception e) {
// Depending on the combination of OS and WebDriver implementation this may fail
// e.g. chrome with xvfb can't do this successfully.
try {
// Trying to set a proper screen size as fallback that should usable with all modern screens
driver.manage().window().setSize(new Dimension(1440, 900));
} catch (Exception exc) {
// In this case we just can't change the window
}
}
File screenshots = resolveScreenshotFolder();
@@ -1,7 +1,9 @@
package com.sap.sailing.selenium.core;
import java.util.Set;
import java.util.function.BiConsumer;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.TargetLocator;
@@ -25,6 +27,8 @@ public class WindowManager {
*/
public WindowManager(WebDriver driver) {
this.driver = driver;
setWindowMaximized();
}
/**
@@ -134,4 +138,33 @@ public class WindowManager {
return null;
}
public void withExtraWindow(BiConsumer<WebDriverWindow, WebDriverWindow> defaultAndExtraWindow) {
final WebDriverWindow defaultWindow = getCurrentWindow();
final WebDriverWindow extraWindow = openNewWindow();
extraWindow.switchToWindow();
setWindowMaximized();
defaultWindow.switchToWindow();
try {
defaultAndExtraWindow.accept(defaultWindow, extraWindow);
} finally {
extraWindow.close();
defaultWindow.switchToWindow();
}
}
private void setWindowMaximized() {
try {
driver.manage().window().maximize();
} catch (Exception e) {
// Depending on the combination of OS and WebDriver implementation this may fail
// e.g. chrome with xvfb can't do this successfully.
try {
// Trying to set a proper screen size as fallback that should usable with all modern screens
driver.manage().window().setSize(new Dimension(1440, 900));
} catch (Exception exc) {
// In this case we just can't change the window
}
}
}
}
@@ -6,15 +6,11 @@ import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.Dimension;
import com.sap.sailing.selenium.core.WebDriverWindow;
import com.sap.sailing.selenium.core.WindowManager;
import com.sap.sailing.selenium.pages.adminconsole.AdminConsolePage;
import com.sap.sailing.selenium.pages.adminconsole.leaderboard.LeaderboardConfigurationPanelPO;
import com.sap.sailing.selenium.pages.adminconsole.leaderboard.LeaderboardDetailsPanelPO;
@@ -75,7 +71,7 @@ public class TestLeaderboardConfiguration extends AbstractSeleniumTest {
@Test
public void testDynamicRaceLinking() {
withExtraWindow((leaderboardWindow, adminConsoleWindow) -> {
this.environment.getWindowManager().withExtraWindow((leaderboardWindow, adminConsoleWindow) -> {
// Open the leaderboard and check for "empty" leaderboard
LeaderboardPage leaderboard = LeaderboardPage.goToPage(getWebDriver(), getContextRoot(), LEADERBOARD, false);
LeaderboardTablePO table = leaderboard.getLeaderboardTable();
@@ -114,7 +110,7 @@ public class TestLeaderboardConfiguration extends AbstractSeleniumTest {
@Test
public void testDynamicRaceDeletion() {
withExtraWindow((adminConsoleWindow, leaderboardWindow) -> {
this.environment.getWindowManager().withExtraWindow((adminConsoleWindow, leaderboardWindow) -> {
// Go to the administration console and link all 5 races
AdminConsolePage adminConsole = AdminConsolePage.goToPage(getWebDriver(), getContextRoot());
LeaderboardConfigurationPanelPO leaderboardConfiguration = adminConsole.goToLeaderboardConfiguration();
@@ -148,7 +144,7 @@ public class TestLeaderboardConfiguration extends AbstractSeleniumTest {
@Ignore("This test belongs to bug 1892 and currently fails. It is currently enabled on branch bug1892.")
@Test
public void testDynamicRenamingOfRace() {
withExtraWindow((adminConsoleWindow, leaderboardWindow) -> {
this.environment.getWindowManager().withExtraWindow((adminConsoleWindow, leaderboardWindow) -> {
// Go to the administration console and link all 5 races
AdminConsolePage adminConsole = AdminConsolePage.goToPage(getWebDriver(), getContextRoot());
LeaderboardConfigurationPanelPO leaderboardConfiguration = adminConsole.goToLeaderboardConfiguration();
@@ -184,34 +180,6 @@ public class TestLeaderboardConfiguration extends AbstractSeleniumTest {
});
}
private void withExtraWindow(BiConsumer<WebDriverWindow, WebDriverWindow> defaultAndExtraWindow) {
final WindowManager manager = this.environment.getWindowManager();
final WebDriverWindow defaultWindow = manager.getCurrentWindow();
final WebDriverWindow extraWindow = manager.openNewWindow();
try {
extraWindow.switchToWindow();
// On WebDriver implementations that support it, the window is maximized
// This makes our Tests work much better on Windows
getWebDriver().manage().window().maximize();
} catch (Exception e) {
// maximizing isn't supported on all Systems / WebDriver implementations
try {
// Trying to set a proper screen size as fallback that should usable with all modern screens
getWebDriver().manage().window().setSize(new Dimension(1440, 900));
} catch (Exception exc) {
// In this case we just can't change the window
}
} finally {
defaultWindow.switchToWindow();
}
try {
defaultAndExtraWindow.accept(defaultWindow, extraWindow);
} finally {
extraWindow.close();
defaultWindow.switchToWindow();
}
}
private void configureLeaderboard() {
// Open the admin console for some configuration steps
AdminConsolePage adminConsole = AdminConsolePage.goToPage(getWebDriver(), getContextRoot());