From d51dc8af419f98b11f26c4476b24c2c2ca713a61 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Sat, 29 Jun 2019 19:07:38 +0200
Subject: [PATCH] no-jira: hardened Selenium for Firefox, remove unused back to
 top

---
 README.md                                     |  5 ++++
 .../olat/selenium/page/NavigationPage.java    | 19 +--------------
 .../selenium/page/graphene/OOGraphene.java    | 23 ++++++++++++++++++-
 .../page/lecture/RollCallInterceptorPage.java |  1 +
 .../lecture/TeacherRollCallWizardPage.java    |  1 +
 .../selenium/page/portfolio/BinderPage.java   |  2 +-
 .../page/repository/AuthoringEnvPage.java     |  5 +---
 7 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index 3151a226056..c5c73972aad 100644
--- a/README.md
+++ b/README.md
@@ -365,6 +365,11 @@ mvn clean verify -DskipTests=true -Dwith-mysql -Ptomcat
 mvn clean verify -DskipTests=true -Dwith-postgresql -Dtest.env.db.postgresql.pass=serial -Ptomcat
 ```
 
+Or with Firefox
+
+```bash
+mvn clean verify -DskipTests=true -Dwith-postgresql -Dtest.env.webdriver.browser=firefox -Dtest.env.db.postgresql.pass=serial -Ptomcat
+```
 
 #### Execute a single selenium functional integration test in Eclipse
 
diff --git a/src/test/java/org/olat/selenium/page/NavigationPage.java b/src/test/java/org/olat/selenium/page/NavigationPage.java
index 4cff35d1601..5f679f782c7 100644
--- a/src/test/java/org/olat/selenium/page/NavigationPage.java
+++ b/src/test/java/org/olat/selenium/page/NavigationPage.java
@@ -79,7 +79,6 @@ public class NavigationPage {
 	
 	public AuthoringEnvPage openAuthoringEnvironment() {
 		navigate(authoringEnvTabBy);
-		backToTheTop();
 		OOGraphene.closeBlueMessageWindow(browser);
 		return new AuthoringEnvPage(browser);
 	}
@@ -160,23 +159,7 @@ public class NavigationPage {
 		browser.findElement(openMoreBy).click();
 		//wait the small transition
 		By openedMoreMenuby = By.cssSelector("#o_navbar_more ul.dropdown-menu.dropdown-menu-right");
-		OOGraphene.waitElement(openedMoreMenuby, 5, browser);
-	}
-	
-	public NavigationPage backToTheTop() {
-		List<WebElement> backList = browser.findElements(toolbarBackBy);
-		
-		int count = 0;
-		while(backList.size() > 0) {
-			backList.get(count).click();
-			OOGraphene.waitBusy(browser);
-			backList = browser.findElements(toolbarBackBy);
-			
-			Assert.assertTrue(count++ < 3);
-		}
-
-		OOGraphene.closeBlueMessageWindow(browser);
-		return this;
+		OOGraphene.waitElement(openedMoreMenuby, browser);
 	}
 	
 	public NavigationPage closeTab() {
diff --git a/src/test/java/org/olat/selenium/page/graphene/OOGraphene.java b/src/test/java/org/olat/selenium/page/graphene/OOGraphene.java
index 598a163d97a..6996b4bdb66 100644
--- a/src/test/java/org/olat/selenium/page/graphene/OOGraphene.java
+++ b/src/test/java/org/olat/selenium/page/graphene/OOGraphene.java
@@ -63,6 +63,7 @@ public class OOGraphene {
 	
 	private static final Duration polling = Duration.ofMillis(100);
 	private static final Duration poolingSlow = Duration.ofMillis(200);
+	private static final Duration poolingSlower = Duration.ofMillis(400);
 	private static final Duration timeout = Duration.ofSeconds(5);
 
 	private static final By closeBlueBoxButtonBy = By.cssSelector("div.o_alert_info div.o_sel_info_message a.o_alert_close.o_sel_info_close");
@@ -170,6 +171,21 @@ public class OOGraphene {
 			.until(ExpectedConditions.visibilityOfElementLocated(element));
 	}
 	
+	/**
+	 * Wait until the element is visible. But slowly poll if the
+	 * element exists (every 333ms instead of 100ms)
+	 * 
+	 * @param element The selector for the element
+	 * @param timeoutInSeconds The timeout in seconds
+	 * @param browser The web driver
+	 */
+	public static void waitElementSlowly(By element, int timeoutInSeconds, WebDriver browser) {
+		new WebDriverWait(browser, driverTimeout)
+			.withTimeout(Duration.ofSeconds(timeoutInSeconds))
+			.pollingEvery(poolingSlower)
+			.until(ExpectedConditions.visibilityOfElementLocated(element));
+	}
+	
 	/**
 	 * Wait until the element is visible.
 	 * 
@@ -600,9 +616,14 @@ public class OOGraphene {
 		}
 	}
 	
+	/**
+	 * Click the "<" of the bread crumbs and wait.
+	 * 
+	 * @param browser The browser
+	 */
 	public static final void clickBreadcrumbBack(WebDriver browser) {
 		By backBy = By.xpath("//ol[@class='breadcrumb']/li[@class='o_breadcrumb_back']/a[i[contains(@class,'o_icon_back')]]");
-		waitElement(backBy, browser);
+		waitElement(backBy, 10, browser);
 		try {
 			browser.findElement(backBy).click();
 		} catch (StaleElementReferenceException e) {
diff --git a/src/test/java/org/olat/selenium/page/lecture/RollCallInterceptorPage.java b/src/test/java/org/olat/selenium/page/lecture/RollCallInterceptorPage.java
index 00c4bc7ba6c..5834741d284 100644
--- a/src/test/java/org/olat/selenium/page/lecture/RollCallInterceptorPage.java
+++ b/src/test/java/org/olat/selenium/page/lecture/RollCallInterceptorPage.java
@@ -53,6 +53,7 @@ public class RollCallInterceptorPage {
 		By startWizardBy = By.cssSelector("div.o_sel_lecture_start_wizard a.o_sel_lecture_start_wizard");
 		browser.findElement(startWizardBy).click();
 		OOGraphene.waitBusy(browser);
+		OOGraphene.waitElement(By.className("o_rollcall_wizard"), browser);
 		return new TeacherRollCallWizardPage(browser);
 	}
 
diff --git a/src/test/java/org/olat/selenium/page/lecture/TeacherRollCallWizardPage.java b/src/test/java/org/olat/selenium/page/lecture/TeacherRollCallWizardPage.java
index 7d383a3f6a9..1d0dede9523 100644
--- a/src/test/java/org/olat/selenium/page/lecture/TeacherRollCallWizardPage.java
+++ b/src/test/java/org/olat/selenium/page/lecture/TeacherRollCallWizardPage.java
@@ -46,6 +46,7 @@ public class TeacherRollCallWizardPage {
 	
 	public TeacherRollCallWizardPage setAbsence(String lecture) {
 		By checkBy = By.xpath("//div[contains(@class,'o_rollcall_wizard')]//table//tr[1]/td[count(//div[contains(@class,'o_rollcall_wizard')]//table//tr/th[a[text()='" + lecture + "']]/preceding-sibling::th)+1]/div/label/input");
+		OOGraphene.waitElement(checkBy, browser);
 		WebElement checkEl = browser.findElement(checkBy);
 		OOGraphene.check(checkEl, Boolean.TRUE);
 		OOGraphene.waitBusy(browser);
diff --git a/src/test/java/org/olat/selenium/page/portfolio/BinderPage.java b/src/test/java/org/olat/selenium/page/portfolio/BinderPage.java
index 93a64003cae..fd910c205e6 100644
--- a/src/test/java/org/olat/selenium/page/portfolio/BinderPage.java
+++ b/src/test/java/org/olat/selenium/page/portfolio/BinderPage.java
@@ -174,7 +174,7 @@ public class BinderPage {
 		browser.findElement(tocBy).click();
 		OOGraphene.waitBusy(browser);
 		By binderPageListBy = By.cssSelector("div.o_portfolio_entries");
-		OOGraphene.waitElement(binderPageListBy, browser);
+		OOGraphene.waitElementSlowly(binderPageListBy, 10, browser);
 		return this;
 	}
 	
diff --git a/src/test/java/org/olat/selenium/page/repository/AuthoringEnvPage.java b/src/test/java/org/olat/selenium/page/repository/AuthoringEnvPage.java
index e209c14a38e..04c76caeba9 100644
--- a/src/test/java/org/olat/selenium/page/repository/AuthoringEnvPage.java
+++ b/src/test/java/org/olat/selenium/page/repository/AuthoringEnvPage.java
@@ -228,10 +228,7 @@ public class AuthoringEnvPage {
 	 * @return
 	 */
 	public CoursePageFragment clickToolbarRootCrumb() {
-		By toolbarBackBy = By.xpath("//div[contains(@class,'o_breadcrumb')]/ol[contains(@class,'breadcrumb')]/li/a[contains(@onclick,'crumb_0')]");
-		OOGraphene.waitingALittleBit();// firefox will click the button without effect
-		browser.findElement(toolbarBackBy).click();
-		OOGraphene.waitBusy(browser);
+		OOGraphene.clickBreadcrumbBack(browser);
 		return new CoursePageFragment(browser);
 	}
 	
-- 
GitLab