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 @@ -64,7 +64,8 @@ public void testTestBenchDriverActsAsProxy() {
mockDriver.quit();

Mockito.when(((JavascriptExecutor) mockDriver)
.executeScript(Mockito.anyString())).thenReturn(true);
.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenReturn(true);
TargetLocator mockTargetLocator = Mockito.mock(TargetLocator.class);
Mockito.when(mockDriver.switchTo()).thenReturn(mockTargetLocator);

Expand Down Expand Up @@ -103,8 +104,8 @@ public void testDisableWaitForVaadin() {

FirefoxDriver mockFF = Mockito.mock(FirefoxDriver.class);
Mockito.when(mockFF.getCapabilities()).thenReturn(mockCapabilities);
Mockito.when(
mockFF.executeScript(contains("clients[client].isActive()")))
Mockito.when(mockFF.executeAsyncScript(
contains("clients[client].isActive()"), Mockito.any()))
.thenReturn(true);
WebElement mockElement = Mockito.mock(WebElement.class);
Mockito.when(mockFF.findElement(isA(By.class))).thenReturn(mockElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.ScriptTimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand Down Expand Up @@ -235,8 +237,8 @@ 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")))
Mockito.when(driver.executeAsyncScript(
Mockito.contains("window.Vaadin.Flow"), Mockito.any()))
.thenReturn(Boolean.TRUE);
if (expectGetCapabilities) {
Capabilities mockedCapabilities = Mockito.mock(Capabilities.class);
Expand Down Expand Up @@ -303,10 +305,68 @@ public void testTotalTimeSpentServicingRequests() {

}

@Test
public void waitForVaadin_idle_asksBrowserOnce() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
Mockito.when(driver.executeAsyncScript(Mockito.contains("flow.ready("),
Mockito.any())).thenReturn(Boolean.TRUE);

newCommandExecutor(driver).waitForVaadin();

Mockito.verify(driver).executeAsyncScript(
Mockito.contains("flow.ready("),
// Flow's own timeout must end the call before the default
// WebDriver script timeout of 30s does
Mockito.longThat(timeout -> timeout > 0 && timeout < 30000));
}

@Test
public void waitForVaadin_notIdleOrInterrupted_asksAgain() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
Mockito.when(
driver.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenReturn(Boolean.FALSE)
.thenThrow(new ScriptTimeoutException("script timeout"),
new JavascriptException(
"document unloaded while waiting for result"),
new JavascriptException("Document was unloaded"))
.thenReturn(Boolean.TRUE);

newCommandExecutor(driver).waitForVaadin();

Mockito.verify(driver, Mockito.times(5))
.executeAsyncScript(Mockito.anyString(), Mockito.any());
}

@Test
public void waitForVaadin_scriptError_isNotRetried() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
JavascriptException error = new JavascriptException(
"TypeError: clients[client].isActive is not a function");
Mockito.when(
driver.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenThrow(error);
TestBenchCommandExecutor tbce = newCommandExecutor(driver);

JavascriptException thrown = Assertions
.assertThrows(JavascriptException.class, tbce::waitForVaadin);

Assertions.assertSame(error, thrown);
Mockito.verify(driver).executeAsyncScript(Mockito.anyString(),
Mockito.any());
}

private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) {
TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null,
null);
tbce.setDriver(TestBench.createDriver(driver, tbce));
return tbce;
}

private FirefoxDriver mockJSExecutor(boolean forcesSync) {
FirefoxDriver jse = Mockito.mock(FirefoxDriver.class);
Mockito.when(jse
.executeScript(Mockito.contains("window.Vaadin.Flow.client")))
Mockito.when(jse.executeAsyncScript(
Mockito.contains("window.Vaadin.Flow"), Mockito.any()))
.thenReturn(Boolean.TRUE);
Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()")))
.thenReturn(Arrays.asList(1000L, 2000L, 3000L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void testTestBenchDriverActsAsProxy() {
mockDriver.quit();

Mockito.when(((JavascriptExecutor) mockDriver)
.executeScript(Mockito.anyString())).thenReturn(true);
.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenReturn(true);
TargetLocator mockTargetLocator = Mockito.mock(TargetLocator.class);
Mockito.when(mockDriver.switchTo()).thenReturn(mockTargetLocator);

Expand Down Expand Up @@ -103,8 +104,8 @@ public void testDisableWaitForVaadin() {

FirefoxDriver mockFF = Mockito.mock(FirefoxDriver.class);
Mockito.when(mockFF.getCapabilities()).thenReturn(mockCapabilities);
Mockito.when(
mockFF.executeScript(contains("clients[client].isActive()")))
Mockito.when(mockFF.executeAsyncScript(
contains("clients[client].isActive()"), Mockito.any()))
.thenReturn(true);
WebElement mockElement = Mockito.mock(WebElement.class);
Mockito.when(mockFF.findElement(isA(By.class))).thenReturn(mockElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.junit.Test;
import org.mockito.Mockito;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.ScriptTimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -32,6 +34,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class TestBenchCommandExecutorTest {
Expand Down Expand Up @@ -216,8 +220,8 @@ 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")))
Mockito.when(driver.executeAsyncScript(
Mockito.contains("window.Vaadin.Flow"), Mockito.any()))
.thenReturn(Boolean.TRUE);
if (expectGetCapabilities) {
Capabilities mockedCapabilities = Mockito.mock(Capabilities.class);
Expand Down Expand Up @@ -284,10 +288,68 @@ public void testTotalTimeSpentServicingRequests() {

}

@Test
public void waitForVaadin_idle_asksBrowserOnce() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
Mockito.when(driver.executeAsyncScript(Mockito.contains("flow.ready("),
Mockito.any())).thenReturn(Boolean.TRUE);

newCommandExecutor(driver).waitForVaadin();

Mockito.verify(driver).executeAsyncScript(
Mockito.contains("flow.ready("),
// Flow's own timeout must end the call before the default
// WebDriver script timeout of 30s does
Mockito.longThat(timeout -> timeout > 0 && timeout < 30000));
}

@Test
public void waitForVaadin_notIdleOrInterrupted_asksAgain() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
Mockito.when(
driver.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenReturn(Boolean.FALSE)
.thenThrow(new ScriptTimeoutException("script timeout"),
new JavascriptException(
"document unloaded while waiting for result"),
new JavascriptException("Document was unloaded"))
.thenReturn(Boolean.TRUE);

newCommandExecutor(driver).waitForVaadin();

Mockito.verify(driver, Mockito.times(5))
.executeAsyncScript(Mockito.anyString(), Mockito.any());
}

@Test
public void waitForVaadin_scriptError_isNotRetried() {
FirefoxDriver driver = Mockito.mock(FirefoxDriver.class);
JavascriptException error = new JavascriptException(
"TypeError: clients[client].isActive is not a function");
Mockito.when(
driver.executeAsyncScript(Mockito.anyString(), Mockito.any()))
.thenThrow(error);
TestBenchCommandExecutor tbce = newCommandExecutor(driver);

JavascriptException thrown = assertThrows(JavascriptException.class,
tbce::waitForVaadin);

assertSame(error, thrown);
Mockito.verify(driver).executeAsyncScript(Mockito.anyString(),
Mockito.any());
}

private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) {
TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null,
null);
tbce.setDriver(TestBench.createDriver(driver, tbce));
return tbce;
}

private FirefoxDriver mockJSExecutor(boolean forcesSync) {
FirefoxDriver jse = Mockito.mock(FirefoxDriver.class);
Mockito.when(jse
.executeScript(Mockito.contains("window.Vaadin.Flow.client")))
Mockito.when(jse.executeAsyncScript(
Mockito.contains("window.Vaadin.Flow"), Mockito.any()))
.thenReturn(Boolean.TRUE);
Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()")))
.thenReturn(Arrays.asList(1000L, 2000L, 3000L));
Expand Down
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 @@ -46,9 +43,10 @@ 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 Down Expand Up @@ -82,9 +80,10 @@ public void waitForVaadin_devModeNotReady_waitsUntilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.devServerIsNotLoaded = false;");
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.devServerIsNotLoaded = false;"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertDevServerIsNotLoaded();
}
Expand Down Expand Up @@ -123,28 +122,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 @@ -46,9 +43,10 @@ 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 Down Expand Up @@ -83,9 +81,10 @@ public void waitForVaadin_devModeNotReady_waitsUntilReady() {
openTestURL();
assertDevServerIsNotLoaded();
getCommandExecutor().executeScript(
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};");
setWaitForVaadinLoopHook(500,
"window.Vaadin.Flow.devServerIsNotLoaded = false;");
"window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"
+ "setTimeout(function() {"
+ " window.Vaadin.Flow.devServerIsNotLoaded = false;"
+ "}, 500);");
getCommandExecutor().waitForVaadin();
assertDevServerIsNotLoaded();
}
Expand Down Expand Up @@ -125,28 +124,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