diff --git a/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java b/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java index ab453e945..31fdd1d8c 100644 --- a/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java +++ b/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java @@ -53,11 +53,10 @@ private static Logger getLogger() { private boolean autoScrollIntoView = true; // @formatter:off String WAIT_FOR_VAADIN_SCRIPT = - "var vaadin = (window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin;" - + "if (vaadin == null) {" + "if (!window.Vaadin || !window.Vaadin.Flow) {" + " return true;" + "}" - + "var clients = vaadin.clients;" + + "var clients = window.Vaadin.Flow.clients;" + "if (clients) {" + " for (var client in clients) {" + " if (clients[client].isActive()) {" @@ -197,22 +196,33 @@ public long totalTimeSpentServicingRequests() { } @SuppressWarnings("unchecked") - private List getTimingValues(boolean forceSync) { - // @formatter:off - String getProfilingData = "var pd = [0,0,0,0];\n" - + "var clients = (window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.clients : window.vaadin.clients;\n" - + "for (client in clients) {\n" - + " var p = clients[client].getProfilingData();\n" - + " pd[0] += p[0];\n" + " pd[1] += p[1];\n" - + " pd[2] += p[2];\n" + " pd[3] += p[3];\n" + "}\n" - + "return pd;\n"; - // @formatter:on - if (forceSync) { - // Force sync to get the latest server-side timing data. The - // server-side timing data is always one request behind. - executeScript("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.forceSync() : window.vaadin.forceSync()"); + private List getTimingValues(boolean poll) { + if (poll) { + // Get the latest server-side timing data. + // The server-side timing data is always one request behind. + executeScript("" // + + "if (!window.Vaadin || !window.Vaadin.Flow || !window.Vaadin.Flow.clients) {" + + " throw 'Performance data is only available when using Vaadin Flow';" + + "}" // + + "for (client in window.Vaadin.Flow.clients) {\n" // + + " window.Vaadin.Flow.clients[client].poll();\n" + "}"); } - return (List) executeScript(getProfilingData); + + return (List) executeScript("" // + + "if (!window.Vaadin || !window.Vaadin.Flow || !window.Vaadin.Flow.clients) {" + + " throw 'Performance data is only available when using Vaadin Flow';" + + "}" // + + "var pd = [0,0,0,0];\n" // + + "for (client in window.Vaadin.Flow.clients) {\n" + + " if (!window.Vaadin.Flow.clients[client].getProfilingData) {" + + " throw 'Performance data is not available in production mode';" + + " }" // + + " var p = window.Vaadin.Flow.clients[client].getProfilingData();\n" + + " pd[0] += p[0];\n" // + + " pd[1] += p[1];\n"// + + " pd[2] += p[2];\n" // + + " pd[3] += p[3];\n" // + + "}\n" + "return pd;\n"); } @Override diff --git a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java index 39eb8c699..8fe6fe4da 100644 --- a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java +++ b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java @@ -237,7 +237,7 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed, "cursor-bottom-edge-off.png"); expect(driver.getScreenshotAs(OutputType.BYTES)) .andReturn(screenshotBytes).times(nrScreenshotsGrabbed); - expect(driver.executeScript(contains("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin"))) + expect(driver.executeScript(contains("window.Vaadin.Flow"))) .andReturn(Boolean.TRUE).anyTimes(); if (expectGetCapabilities) { expect(driver.getCapabilities()) @@ -309,14 +309,10 @@ public void testTotalTimeSpentServicingRequests() { } private FirefoxDriver mockJSExecutor(boolean forcesSync) { - FirefoxDriver jse = createMock(FirefoxDriver.class); - if (forcesSync) { - expect(jse.executeScript("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow.forceSync() : window.vaadin.forceSync()")) - .andReturn(null); - } + FirefoxDriver jse = createNiceMock(FirefoxDriver.class); expect(jse.executeScript(contains("getProfilingData()"))) .andReturn(Arrays.asList(1000L, 2000L, 3000L)); - expect(jse.executeScript(contains("(window.Vaadin && window.Vaadin.Flow) ? window.Vaadin.Flow : window.vaadin"))) + expect(jse.executeScript(contains("window.Vaadin.Flow.client"))) .andReturn(Boolean.TRUE).anyTimes(); return jse; } diff --git a/vaadin-testbench-integration-tests/pom.xml b/vaadin-testbench-integration-tests/pom.xml index 95bfd9b63..d37c66e97 100644 --- a/vaadin-testbench-integration-tests/pom.xml +++ b/vaadin-testbench-integration-tests/pom.xml @@ -16,7 +16,7 @@ http://maven.apache.org 9.3.12.v20160915 - 1.0.0.beta1 + 1.0.0.beta2 v8.1.2 diff --git a/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/PerformanceView.java b/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/PerformanceView.java new file mode 100644 index 000000000..a19ec8fb6 --- /dev/null +++ b/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/PerformanceView.java @@ -0,0 +1,22 @@ +package com.vaadin.testUI; + +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.NativeButton; +import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.router.Route; + +@Route("PerformanceView") +public class PerformanceView extends Div { + + public PerformanceView() { + NativeButton button = new NativeButton("1s delay", e -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e1) { + } + add(new Span("Done sleeping")); + }); + add(button); + } + +} diff --git a/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/PerfomanceIT.java b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/PerfomanceIT.java new file mode 100644 index 000000000..dda4a70b8 --- /dev/null +++ b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/PerfomanceIT.java @@ -0,0 +1,51 @@ +package com.vaadin.tests; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +import com.vaadin.flow.component.Component; +import com.vaadin.testUI.PerformanceView; +import com.vaadin.tests.elements.NativeButtonElement; + +public class PerfomanceIT extends AbstractTB6Test { + + @Override + protected Class getTestView() { + return PerformanceView.class; + } + + @Test + @Ignore("Profiling info not available in production mode until beta3") + public void serverTime() { + openTestURL(); + $(NativeButtonElement.class).first().click(); + + Assert.assertEquals(1000.0, testBench().timeSpentServicingLastRequest(), + 100.0); + $(NativeButtonElement.class).first().click(); + Assert.assertEquals(2000.0, + testBench().totalTimeSpentServicingRequests(), 200.0); + } + + @Test + @Ignore("Profiling info not available in production mode until beta3") + public void renderingTime() { + openTestURL(); + long initialRendering = testBench().timeSpentRenderingLastRequest(); + // Assuming initial rendering is done in 5-195ms + Assert.assertEquals(150, initialRendering, 145); + Assert.assertEquals(initialRendering, + testBench().totalTimeSpentRendering()); + $(NativeButtonElement.class).first().click(); + $(NativeButtonElement.class).first().click(); + $(NativeButtonElement.class).first().click(); + + // Assuming rendering three poll responses is done in 50ms + Assert.assertTrue( + testBench().totalTimeSpentRendering() > initialRendering); + Assert.assertEquals(initialRendering, + testBench().totalTimeSpentRendering(), 50); + } + +}