Skip to content

Commit

Permalink
test: fix flaky spring security tests (#20642) (#20715)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Collovati <[email protected]>
  • Loading branch information
vaadin-bot and mcollovati authored Dec 16, 2024
1 parent caaeadc commit 500e859
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ protected void login(String username, String password) {
form.getUsernameField().setValue(username);
form.getPasswordField().setValue(password);
form.submit();
waitUntilNot(
driver -> driver.getCurrentUrl().contains("my/login/page"));
waitUntilNot(driver -> $(LoginOverlayElement.class).exists());
}

Expand All @@ -102,12 +104,14 @@ protected void assertLoginViewShown() {
}

protected void assertRootPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ROOT_PAGE_HEADER_TEXT, headerText);
}

protected void assertAnotherPublicPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ANOTHER_PUBLIC_PAGE_HEADER_TEXT, headerText);
Expand All @@ -130,7 +134,7 @@ protected void assertAdminPageShown(String fullName) {
}

protected void assertPathShown(String path) {

waitForClientRouter();
waitUntil(driver -> {
String url = driver.getCurrentUrl();
if (!url.startsWith(getRootURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected void login(String username, String password) {
form.getUsernameField().setValue(username);
form.getPasswordField().setValue(password);
form.submit();
waitUntilNot(
driver -> driver.getCurrentUrl().contains("my/login/page"));
waitUntilNot(driver -> $(LoginOverlayElement.class).exists());
}

Expand All @@ -105,12 +107,14 @@ protected void assertLoginViewShown() {
}

protected void assertRootPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ROOT_PAGE_HEADER_TEXT, headerText);
}

protected void assertAnotherPublicPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ANOTHER_PUBLIC_PAGE_HEADER_TEXT, headerText);
Expand All @@ -133,7 +137,7 @@ protected void assertAdminPageShown(String fullName) {
}

protected void assertPathShown(String path) {

waitForClientRouter();
waitUntil(driver -> {
String url = driver.getCurrentUrl();
if (!url.startsWith(getRootURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -12,6 +13,7 @@
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;

import com.vaadin.flow.component.button.testbench.ButtonElement;
import com.vaadin.flow.component.upload.testbench.UploadElement;
Expand Down Expand Up @@ -220,7 +222,8 @@ public void upload_file_in_private_view() throws IOException {
.getResourceAsStream("image.png");
IOUtils.copyLarge(imageStream, new FileOutputStream(tmpFile));
tmpFile.deleteOnExit();
upload.upload(tmpFile);
upload.upload(tmpFile, 0);
waitForUploads(upload, 60);

TestBenchElement text = $("p").id("uploadText");
TestBenchElement img = $("img").id("uploadImage");
Expand Down Expand Up @@ -400,4 +403,25 @@ private List<MenuItem> getMenuItems() {
}).collect(Collectors.toList());
}

// Workaround for https://github.com/vaadin/flow-components/issues/3646
// The issue causes the upload test to be flaky
private void waitForUploads(UploadElement element, int maxSeconds) {
WebDriver.Timeouts timeouts = getDriver().manage().timeouts();
timeouts.scriptTimeout(Duration.ofSeconds(maxSeconds));

String script = """
var callback = arguments[arguments.length - 1];
var upload = arguments[0];
let intervalId;
intervalId = window.setInterval(function() {
var inProgress = upload.files.filter(function(file) { return file.uploading;}).length >0;
if (!inProgress) {
window.clearInterval(intervalId);
callback();
}
}, 500);
""";
getCommandExecutor().getDriver().executeAsyncScript(script, element);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void securityContextSetForUIAccess() throws Exception {
}

private void loginAdmin(HasElementQuery adminContext) {
waitForClientRouter();
LoginFormElement form = adminContext.$(LoginOverlayElement.class)
.first().getLoginForm();
form.getUsernameField().setValue("emma");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected void login(String username, String password) {
form.getUsernameField().setValue(username);
form.getPasswordField().setValue(password);
form.submit();
waitUntilNot(
driver -> driver.getCurrentUrl().contains("my/login/page"));
waitUntilNot(driver -> $(LoginOverlayElement.class).exists());
}

Expand All @@ -105,12 +107,14 @@ protected void assertLoginViewShown() {
}

protected void assertRootPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ROOT_PAGE_HEADER_TEXT, headerText);
}

protected void assertAnotherPublicPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ANOTHER_PUBLIC_PAGE_HEADER_TEXT, headerText);
Expand All @@ -133,7 +137,7 @@ protected void assertAdminPageShown(String fullName) {
}

protected void assertPathShown(String path) {

waitForClientRouter();
waitUntil(driver -> {
String url = driver.getCurrentUrl();
if (!url.startsWith(getRootURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -13,6 +14,7 @@
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.WebDriver;

import com.vaadin.flow.component.button.testbench.ButtonElement;
import com.vaadin.flow.component.upload.testbench.UploadElement;
Expand Down Expand Up @@ -222,7 +224,8 @@ public void upload_file_in_private_view() throws IOException {
.getResourceAsStream("image.png");
IOUtils.copyLarge(imageStream, new FileOutputStream(tmpFile));
tmpFile.deleteOnExit();
upload.upload(tmpFile);
upload.upload(tmpFile, 0);
waitForUploads(upload, 60);

TestBenchElement text = $("p").id("uploadText");
TestBenchElement img = $("img").id("uploadImage");
Expand Down Expand Up @@ -368,4 +371,25 @@ private List<MenuItem> getMenuItems() {
}).collect(Collectors.toList());
}

// Workaround for https://github.com/vaadin/flow-components/issues/3646
// The issue causes the upload test to be flaky
private void waitForUploads(UploadElement element, int maxSeconds) {
WebDriver.Timeouts timeouts = getDriver().manage().timeouts();
timeouts.scriptTimeout(Duration.ofSeconds(maxSeconds));

String script = """
var callback = arguments[arguments.length - 1];
var upload = arguments[0];
let intervalId;
intervalId = window.setInterval(function() {
var inProgress = upload.files.filter(function(file) { return file.uploading;}).length >0;
if (!inProgress) {
window.clearInterval(intervalId);
callback();
}
}, 500);
""";
getCommandExecutor().getDriver().executeAsyncScript(script, element);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void securityContextSetForUIAccess() throws Exception {
}

private void loginAdmin(HasElementQuery adminContext) {
waitForClientRouter();
LoginFormElement form = adminContext.$(LoginOverlayElement.class)
.first().getLoginForm();
form.getUsernameField().setValue("emma");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ protected void login(String username, String password) {
form.getUsernameField().setValue(username);
form.getPasswordField().setValue(password);
form.submit();
waitUntilNot(
driver -> driver.getCurrentUrl().contains("my/login/page"));
waitUntilNot(driver -> $(LoginOverlayElement.class).exists());
}

Expand All @@ -111,12 +113,14 @@ protected void assertLogoutViewShown() {
}

protected void assertRootPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ROOT_PAGE_HEADER_TEXT, headerText);
}

protected void assertAnotherPublicPageShown() {
waitForClientRouter();
waitUntil(drive -> $("h1").attribute("id", "header").exists());
String headerText = $("h1").id("header").getText();
Assert.assertEquals(ANOTHER_PUBLIC_PAGE_HEADER_TEXT, headerText);
Expand Down Expand Up @@ -147,7 +151,7 @@ protected void assertRolePrefixedAdminPageShown(String fullName) {
}

protected void assertPathShown(String path) {

waitForClientRouter();
waitUntil(driver -> {
String url = driver.getCurrentUrl();
if (!url.startsWith(getRootURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;

import com.vaadin.flow.component.button.testbench.ButtonElement;
import com.vaadin.flow.component.upload.testbench.UploadElement;
import com.vaadin.flow.spring.flowsecurity.views.AdminView;
import com.vaadin.flow.spring.flowsecurity.views.PublicView;
import com.vaadin.testbench.TestBenchElement;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

public class AppViewIT extends AbstractIT {

private static final String LOGIN_PATH = "my/login/page";
Expand Down Expand Up @@ -258,7 +260,8 @@ public void upload_file_in_private_view() throws IOException {
.getResourceAsStream("image.png");
IOUtils.copyLarge(imageStream, new FileOutputStream(tmpFile));
tmpFile.deleteOnExit();
upload.upload(tmpFile);
upload.upload(tmpFile, 0);
waitForUploads(upload, 60);

TestBenchElement text = $("p").id("uploadText");
TestBenchElement img = $("img").id("uploadImage");
Expand All @@ -272,6 +275,7 @@ public void upload_file_in_private_view() throws IOException {
@Test
public void navigate_in_thread_without_access() {
open("");
waitForClientRouter();
$(ButtonElement.class).id(PublicView.BACKGROUND_NAVIGATION_ID).click();

// This waits for longer than the delay in the UI so we do not need a
Expand Down Expand Up @@ -381,4 +385,25 @@ private List<MenuItem> getMenuItems() {
}).collect(Collectors.toList());
}

// Workaround for https://github.com/vaadin/flow-components/issues/3646
// The issue causes the upload test to be flaky
private void waitForUploads(UploadElement element, int maxSeconds) {
WebDriver.Timeouts timeouts = getDriver().manage().timeouts();
timeouts.scriptTimeout(Duration.ofSeconds(maxSeconds));

String script = """
var callback = arguments[arguments.length - 1];
var upload = arguments[0];
let intervalId;
intervalId = window.setInterval(function() {
var inProgress = upload.files.filter(function(file) { return file.uploading;}).length >0;
if (!inProgress) {
window.clearInterval(intervalId);
callback();
}
}, 500);
""";
getCommandExecutor().getDriver().executeAsyncScript(script, element);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void securityContextSetForUIAccess() throws Exception {
}

private void loginAdmin(HasElementQuery adminContext) {
waitForClientRouter();
LoginFormElement form = adminContext.$(LoginOverlayElement.class)
.first().getLoginForm();
form.getUsernameField().setValue("emma");
Expand Down

0 comments on commit 500e859

Please sign in to comment.