Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -213,9 +214,7 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed,
"cursor-bottom-edge-off.png");
Mockito.when(driver.getScreenshotAs(OutputType.BYTES))
.thenReturn(screenshotBytes);
Mockito.when(
driver.executeScript(Mockito.contains("window.Vaadin.Flow")))
.thenReturn(Boolean.TRUE);
mockWaitForVaadin(driver);
if (expectGetCapabilities) {
Capabilities mockedCapabilities = Mockito.mock(Capabilities.class);
Mockito.when(mockedCapabilities.getBrowserName())
Expand Down Expand Up @@ -283,11 +282,28 @@ public void testTotalTimeSpentServicingRequests() {

private FirefoxDriver mockJSExecutor(boolean forcesSync) {
FirefoxDriver jse = Mockito.mock(FirefoxDriver.class);
mockWaitForVaadin(jse);
Mockito.when(jse
.executeScript(Mockito.contains("window.Vaadin.Flow.client")))
.thenReturn(Boolean.TRUE);
Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()")))
.thenReturn(Arrays.asList(1000L, 2000L, 3000L));
return jse;
}

private void mockWaitForVaadin(RemoteWebDriver driver) {
WebDriver.Options options = Mockito.mock(WebDriver.Options.class);
WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class);
Mockito.when(driver.manage()).thenReturn(options);
Mockito.when(options.timeouts()).thenReturn(timeouts);
Mockito.when(timeouts.getScriptTimeout())
.thenReturn(Duration.ofSeconds(30));
Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class)))
.thenReturn(timeouts);
// Phase 1: sync check returns true immediately
Mockito.when(driver.executeScript(Mockito.contains("whenReady")))
.thenReturn(Boolean.TRUE);
Mockito.when(driver.executeAsyncScript(Mockito.anyString()))
.thenReturn(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;

import org.junit.Before;
Expand Down Expand Up @@ -216,9 +217,7 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed,
"cursor-bottom-edge-off.png");
Mockito.when(driver.getScreenshotAs(OutputType.BYTES))
.thenReturn(screenshotBytes);
Mockito.when(
driver.executeScript(Mockito.contains("window.Vaadin.Flow")))
.thenReturn(Boolean.TRUE);
mockWaitForVaadin(driver);
if (expectGetCapabilities) {
Capabilities mockedCapabilities = Mockito.mock(Capabilities.class);
Mockito.when(mockedCapabilities.getBrowserName())
Expand Down Expand Up @@ -286,11 +285,28 @@ public void testTotalTimeSpentServicingRequests() {

private FirefoxDriver mockJSExecutor(boolean forcesSync) {
FirefoxDriver jse = Mockito.mock(FirefoxDriver.class);
mockWaitForVaadin(jse);
Mockito.when(jse
.executeScript(Mockito.contains("window.Vaadin.Flow.client")))
.thenReturn(Boolean.TRUE);
Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()")))
.thenReturn(Arrays.asList(1000L, 2000L, 3000L));
return jse;
}

private void mockWaitForVaadin(RemoteWebDriver driver) {
WebDriver.Options options = Mockito.mock(WebDriver.Options.class);
WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class);
Mockito.when(driver.manage()).thenReturn(options);
Mockito.when(options.timeouts()).thenReturn(timeouts);
Mockito.when(timeouts.getScriptTimeout())
.thenReturn(Duration.ofSeconds(30));
Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class)))
.thenReturn(timeouts);
// Phase 1: sync check returns true immediately
Mockito.when(driver.executeScript(Mockito.contains("whenReady")))
.thenReturn(Boolean.TRUE);
Mockito.when(driver.executeAsyncScript(Mockito.anyString()))
.thenReturn(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
*/
package com.vaadin.tests;

import java.lang.reflect.Field;

import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.JavascriptExecutor;

import com.vaadin.flow.component.Component;
import com.vaadin.testUI.PageObjectView;
Expand Down Expand Up @@ -44,11 +41,11 @@ public void waitForVaadin_activeConnector_waits() {
@BrowserTest
public void waitForVaadin_activeConnector_waitsUtilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};");
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertClientIsActive();
}
Expand All @@ -61,41 +58,24 @@ public void waitForVaadin_noConnectors_returnsImmediately() {
assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin());
}

@BrowserTest
public void waitForVaadin_noFlow_returnsImmediately() {
openTestURL();

getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;");
assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin());
}

@BrowserTest
public void waitForVaadin_devModeNotReady_waits() {
openTestURL();
getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
getCommandExecutor()
.executeScript("window.Vaadin.Flow.whenReady = false;");
assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin());
}

@BrowserTest
public void waitForVaadin_devModeNotReady_waitsUntilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.devServerIsNotLoaded = false;");
"window._savedWhenReady = window.Vaadin.Flow.whenReady;"
+ "window.Vaadin.Flow.whenReady = false;"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.whenReady = window._savedWhenReady;"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertDevServerIsNotLoaded();
}

private void assertDevServerIsNotLoaded() {
Object devServerIsNotLoaded = executeScript(
"return window.Vaadin.Flow.devServerIsNotLoaded;");
Assertions.assertTrue(
devServerIsNotLoaded == null
|| devServerIsNotLoaded == Boolean.FALSE,
"devServerIsNotLoaded should be null or false");
}

private void assertClientIsActive() {
Expand Down Expand Up @@ -123,28 +103,4 @@ private void assertExecutionBlocked(Runnable command) {
"Unexpected execution time, waiting time = " + timeout);
}

private void setWaitForVaadinLoopHook(long timeout,
String scriptToRunAfterTimeout) {
long systemCurrentTimeMillis = System.currentTimeMillis();
setWaitForVaadinLoopHook(() -> {
if (System.currentTimeMillis()
- systemCurrentTimeMillis > timeout) {
((JavascriptExecutor) getCommandExecutor().getDriver()
.getWrappedDriver())
.executeScript(scriptToRunAfterTimeout);
setWaitForVaadinLoopHook(null);
}
});
}

private void setWaitForVaadinLoopHook(Runnable action) {
try {
Field field = getCommandExecutor().getClass()
.getDeclaredField("waitForVaadinLoopHook");
field.setAccessible(true);
field.set(getCommandExecutor(), action);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
*/
package com.vaadin.tests;

import java.lang.reflect.Field;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;

import com.vaadin.flow.component.Component;
import com.vaadin.testUI.PageObjectView;
Expand Down Expand Up @@ -44,11 +41,11 @@ public void waitForVaadin_activeConnector_waits() {
@Test
public void waitForVaadin_activeConnector_waitsUtilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};");
"window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertClientIsActive();
}
Expand All @@ -61,41 +58,24 @@ public void waitForVaadin_noConnectors_returnsImmediately() {
assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin());
}

@Test
public void waitForVaadin_noFlow_returnsImmediately() {
openTestURL();

getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;");
assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin());
}

@Test
public void waitForVaadin_devModeNotReady_waits() {
openTestURL();

getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
getCommandExecutor()
.executeScript("window.Vaadin.Flow.whenReady = false;");
assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin());
}

@Test
public void waitForVaadin_devModeNotReady_waitsUntilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.devServerIsNotLoaded = false;");
"window._savedWhenReady = window.Vaadin.Flow.whenReady;"
+ "window.Vaadin.Flow.whenReady = false;"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.whenReady = window._savedWhenReady;"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertDevServerIsNotLoaded();
}

private void assertDevServerIsNotLoaded() {
Object devServerIsNotLoaded = executeScript(
"return window.Vaadin.Flow.devServerIsNotLoaded;");
Assert.assertTrue("devServerIsNotLoaded should be null or false",
devServerIsNotLoaded == null
|| devServerIsNotLoaded == Boolean.FALSE);
}

private void assertClientIsActive() {
Expand Down Expand Up @@ -125,28 +105,4 @@ private void assertExecutionBlocked(Runnable command) {
timeout >= BLOCKING_EXECUTION_TIMEOUT);
}

private void setWaitForVaadinLoopHook(long timeout,
String scriptToRunAfterTimeout) {
long systemCurrentTimeMillis = System.currentTimeMillis();
setWaitForVaadinLoopHook(() -> {
if (System.currentTimeMillis()
- systemCurrentTimeMillis > timeout) {
((JavascriptExecutor) getCommandExecutor().getDriver()
.getWrappedDriver())
.executeScript(scriptToRunAfterTimeout);
setWaitForVaadinLoopHook(null);
}
});
}

private void setWaitForVaadinLoopHook(Runnable action) {
try {
Field field = getCommandExecutor().getClass()
.getDeclaredField("waitForVaadinLoopHook");
field.setAccessible(true);
field.set(getCommandExecutor(), action);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Loading
Loading