From 86dcfc66969c28db591c5abe7d07224880650387 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Sat, 8 Feb 2020 20:05:25 +0100 Subject: [PATCH] Fixes acceptance tests Acceptance tests were not working as they wasn't adapted to the Angular framework after the migration. This commit fixes these test that now work again. --- pom.xml | 2 +- src/main/angular/package-lock.json | 2 +- src/main/angular/package.json | 2 +- .../people-form/people-form.component.html | 2 +- .../people-list/people-list.component.html | 12 ++-- .../esei/daa/util/AdditionalConditions.java | 14 ---- .../java/es/uvigo/esei/daa/util/JSWaiter.java | 67 +++++++++++++++++++ .../es/uvigo/esei/daa/web/PeopleWebTest.java | 6 +- .../es/uvigo/esei/daa/web/pages/MainPage.java | 8 +-- tomcat/server.hsqldb.xml | 14 +++- 10 files changed, 95 insertions(+), 34 deletions(-) delete mode 100644 src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java create mode 100644 src/test/java/es/uvigo/esei/daa/util/JSWaiter.java diff --git a/pom.xml b/pom.xml index 79cc81e..09fcb7e 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ 1.3.0 2.3.3 5.1.45 - v0.24.0 + v0.26.0 2.4.2 diff --git a/src/main/angular/package-lock.json b/src/main/angular/package-lock.json index b0ee6c6..1d3e8ae 100644 --- a/src/main/angular/package-lock.json +++ b/src/main/angular/package-lock.json @@ -1,6 +1,6 @@ { "name": "daa-example", - "version": "0.2.0-alpha.5", + "version": "0.2.0-alpha.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/main/angular/package.json b/src/main/angular/package.json index a9322bf..553c5fb 100644 --- a/src/main/angular/package.json +++ b/src/main/angular/package.json @@ -1,6 +1,6 @@ { "name": "daa-example", - "version": "0.2.0-alpha.5", + "version": "0.2.0-alpha.6", "scripts": { "ng": "./node_modules/.bin/ng", "start": "./node_modules/.bin/ng serve", diff --git a/src/main/angular/src/app/modules/people/components/people-form/people-form.component.html b/src/main/angular/src/app/modules/people/components/people-form/people-form.component.html index de89f56..fed524e 100644 --- a/src/main/angular/src/app/modules/people/components/people-form/people-form.component.html +++ b/src/main/angular/src/app/modules/people/components/people-form/people-form.component.html @@ -17,7 +17,7 @@ ~ along with this program. If not, see . --> -
+
diff --git a/src/main/angular/src/app/modules/people/components/people-list/people-list.component.html b/src/main/angular/src/app/modules/people/components/people-list/people-list.component.html index f2c06c0..2f37f4e 100644 --- a/src/main/angular/src/app/modules/people/components/people-list/people-list.component.html +++ b/src/main/angular/src/app/modules/people/components/people-list/people-list.component.html @@ -17,7 +17,7 @@ ~ along with this program. If not, see . --> - +
@@ -26,12 +26,12 @@ - - - + + + diff --git a/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java b/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java deleted file mode 100644 index 68bb922..0000000 --- a/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java +++ /dev/null @@ -1,14 +0,0 @@ -package es.uvigo.esei.daa.util; - -import org.openqa.selenium.JavascriptExecutor; -import org.openqa.selenium.support.ui.ExpectedCondition; - -/* - * Implementation based on https://stackoverflow.com/questions/33348600/selenium-wait-for-ajax-content-to-load-universal-approach - */ -public class AdditionalConditions { - public static ExpectedCondition jQueryAjaxCallsHaveCompleted() { - return driver -> - (Boolean) ((JavascriptExecutor) driver).executeScript("return (window.jQuery !== null) && (jQuery.active === 0)"); - } -} diff --git a/src/test/java/es/uvigo/esei/daa/util/JSWaiter.java b/src/test/java/es/uvigo/esei/daa/util/JSWaiter.java new file mode 100644 index 0000000..6193774 --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/util/JSWaiter.java @@ -0,0 +1,67 @@ +package es.uvigo.esei.daa.util; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Utility class to wait for several JavaScript events to complete. + * + * Code adapted from + * https://www.swtestacademy.com/selenium-wait-javascript-angular-ajax/ + */ +public class JSWaiter { + private final WebDriverWait jsWait; + private final JavascriptExecutor jsExec; + + private JSWaiter(WebDriver driver) { + this.jsWait = new WebDriverWait(driver, 10); + this.jsExec = (JavascriptExecutor) driver; + } + + public static JSWaiter wait(WebDriver driver) { + return new JSWaiter(driver); + } + + public void untilAngular5Ready() { + try { + final Object angular5Check = jsExec.executeScript("return getAllAngularRootElements()[0].attributes['ng-version']"); + if (angular5Check != null) { + final Boolean angularPageLoaded = (Boolean) jsExec + .executeScript("return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1"); + if (!angularPageLoaded) { + poll(20); + + waitForAngular5Load(); + + poll(20); + } + } + } catch (WebDriverException ignored) {} + } + + private void waitForAngular5Load() { + String angularReadyScript = "return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1"; + + try { + final ExpectedCondition angularLoad = driver -> Boolean + .valueOf(((JavascriptExecutor) driver).executeScript(angularReadyScript).toString()); + + final boolean angularReady = Boolean.valueOf(jsExec.executeScript(angularReadyScript).toString()); + + if (!angularReady) { + jsWait.until(angularLoad); + } + } catch (WebDriverException ignored) {} + } + + private void poll(long milis) { + try { + Thread.sleep(milis); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java b/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java index 81313c8..ff4371b 100644 --- a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java +++ b/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java @@ -25,7 +25,6 @@ import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.html5.LocalStorage; -import org.openqa.selenium.remote.DesiredCapabilities; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -71,7 +70,7 @@ public class PeopleWebTest { final FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.privatebrowsing.autostart", true); - final FirefoxOptions options = new FirefoxOptions(DesiredCapabilities.firefox()); + final FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); final FirefoxDriver firefoxDriver; @@ -84,7 +83,8 @@ public class PeopleWebTest { // Login as "admin:adminpass" final LocalStorage localStorage = firefoxDriver.getLocalStorage(); - localStorage.setItem("authorization-token", "YWRtaW46YWRtaW5wYXNz"); + // YWRtaW46YWRtaW5wYXNz + localStorage.setItem("user", "{\"login\":\"admin\",\"password\":\"adminpass\"}"); mainPage = new MainPage(driver, baseUrl); mainPage.navigateTo(); diff --git a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java b/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java index 97b6a1a..4170948 100644 --- a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java +++ b/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java @@ -1,6 +1,5 @@ package es.uvigo.esei.daa.web.pages; -import static es.uvigo.esei.daa.util.AdditionalConditions.jQueryAjaxCallsHaveCompleted; import static java.util.stream.Collectors.toList; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement; @@ -14,6 +13,7 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.WebDriverWait; import es.uvigo.esei.daa.entities.Person; +import es.uvigo.esei.daa.util.JSWaiter; public class MainPage { private static final String TABLE_ID = "people-list"; @@ -35,9 +35,9 @@ public class MainPage { } public void navigateTo() { - this.driver.get(this.baseUrl + "main.html"); + this.driver.get(this.baseUrl + "#/people"); - this.wait.until(presenceOfElementLocated(By.id("people-list"))); + this.wait.until(presenceOfElementLocated(By.id(TABLE_ID))); } public int countPeople() { @@ -87,7 +87,7 @@ public class MainPage { table.deletePerson(id); - wait.until(jQueryAjaxCallsHaveCompleted()); + JSWaiter.wait(driver).untilAngular5Ready(); } private final static class PeopleTable { diff --git a/tomcat/server.hsqldb.xml b/tomcat/server.hsqldb.xml index 64c6e65..80afccd 100644 --- a/tomcat/server.hsqldb.xml +++ b/tomcat/server.hsqldb.xml @@ -139,9 +139,17 @@ - + userTable="users" + userNameCol="login" + userCredCol="password" + userRoleTable="users" + roleNameCol="role" + digest="SHA-256" + > + + +
Nombre
{{person.name}}{{person.surname}}
{{person.name}}{{person.surname}} - - + +