Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add simple integration tests #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
/.project
/node_modules
/webpack.generated.js
/webpack.config.js
/node
/package.json
/package-lock.json
/frontend/index.html
/frontend/generated
/error-screenshots
111 changes: 111 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-testbench</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>license-checker</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.flowingcode.vaadin.test</groupId>
<artifactId>testbench-rpc</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -223,6 +251,89 @@
</build>

<profiles>
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<supportedPackagings>
<supportedPackaging>jar</supportedPackaging>
</supportedPackagings>
<stopKey>${project.artifactId}</stopKey>
<stopPort>8081</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<trimStackTrace>false</trimStackTrace>
<enableAssertions>true</enableAssertions>
<systemPropertyVariables>
<!-- Pass location of downloaded webdrivers to the tests -->
<webdriver.chrome.driver>
${webdriver.chrome.driver}
</webdriver.chrome.driver>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<!-- Since the view class is defined in test, after compilation copy
it to the runtime classpath to ensure it gets visited by vaadin-maven-plugin -->
<id>copy-test-to-classes</id>
<phase>process-test-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${project.build.testOutputDirectory}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>directory</id>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*-
* #%L
* Template Add-on
* %%
* Copyright (C) 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package com.flowingcode.addons.simpletimer.integration;

import com.vaadin.testbench.ScreenshotOnFailureRule;
import com.vaadin.testbench.TestBench;
import com.vaadin.testbench.parallel.ParallelTest;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.openqa.selenium.chrome.ChromeDriver;

/**
* Base class for ITs
*
* <p>The tests use Chrome driver (see pom.xml for integration-tests profile) to run integration
* tests on a headless Chrome. If a property {@code test.use .hub} is set to true, {@code
* AbstractViewTest} will assume that the TestBench test is running in a CI environment. In order to
* keep the this class light, it makes certain assumptions about the CI environment (such as
* available environment variables). It is not advisable to use this class as a base class for you
* own TestBench tests.
*
* <p>To learn more about TestBench, visit <a
* href="https://vaadin.com/docs/v10/testbench/testbench-overview.html">Vaadin TestBench</a>.
*/
public abstract class AbstractViewTest extends ParallelTest {
private static final int SERVER_PORT = 8080;

private final String route;

@Rule public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true);

public AbstractViewTest() {
this("");
}

protected AbstractViewTest(String route) {
this.route = route;
}

@BeforeClass
public static void setupClass() {
WebDriverManager.chromedriver().setup();
}

@Override
@Before
public void setup() throws Exception {
if (isUsingHub()) {
super.setup();
} else {
setDriver(TestBench.createDriver(new ChromeDriver()));
}
getDriver().get(getURL(route));
}

/**
* Returns deployment host name concatenated with route.
*
* @return URL to route
*/
private static String getURL(String route) {
return String.format("http://%s:%d/%s", getDeploymentHostname(), SERVER_PORT, route);
}

/** Property set to true when running on a test hub. */
private static final String USE_HUB_PROPERTY = "test.use.hub";

/**
* Returns whether we are using a test hub. This means that the starter is running tests in
* Vaadin's CI environment, and uses TestBench to connect to the testing hub.
*
* @return whether we are using a test hub
*/
private static boolean isUsingHub() {
return Boolean.TRUE.toString().equals(System.getProperty(USE_HUB_PROPERTY));
}

/**
* If running on CI, get the host name from environment variable HOSTNAME
*
* @return the host name
*/
private static String getDeploymentHostname() {
return isUsingHub() ? System.getenv("HOSTNAME") : "localhost";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.flowingcode.addons.simpletimer.integration;

public interface IntegrationCallables {

void setStartTime(Integer startTime);

void setEndTime(Integer endTime);

void start();

void pause();

void reset();

boolean isRunning();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.flowingcode.addons.simpletimer.integration;

import com.flowingcode.vaadin.addons.simpletimer.SimpleTimer;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route("/it")
public class IntegrationView extends Div implements IntegrationCallables {

private SimpleTimer timer = new SimpleTimer();

public IntegrationView() {
add(timer);
}

@Override
@ClientCallable
public void setStartTime(Integer startTime) {
timer.setStartTime(startTime);
}

@Override
@ClientCallable
public void setEndTime(Integer endTime) {
timer.setEndTime(endTime);
}

@Override
@ClientCallable
public void start() {
timer.start();
}

@Override
@ClientCallable
public void pause() {
timer.pause();
}

@Override
@ClientCallable
public void reset() {
timer.reset();
}

@Override
@ClientCallable
public boolean isRunning() {
return timer.isRunning();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.flowingcode.addons.simpletimer.integration;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport;
import org.junit.Test;

public class SimpleIT extends AbstractViewTest implements HasRpcSupport {

IntegrationCallables $server = createCallableProxy(IntegrationCallables.class);

public SimpleIT() {
super("it");
}

private Double currentTime() {
return $(SimpleTimerElement.class).first().currentTime();
}

@Test
public void countDown() {
assertThat(currentTime(), nullValue());
assertFalse($server.isRunning());

$server.setStartTime(1);
assertThat(currentTime(), equalTo(1.0));

$server.start();
assertTrue($server.isRunning());
double t0 = currentTime();
double t1 = currentTime();
assertThat(t0, lessThan(1.0));
assertThat(t1, lessThan(t0));
}

@Test
public void countUp() {
assertThat(currentTime(), nullValue());
assertFalse($server.isRunning());

$server.setEndTime(1);
assertThat(currentTime(), equalTo(0.0));

$server.start();
assertTrue($server.isRunning());
double t0 = currentTime();
double t1 = currentTime();
assertThat(t0, greaterThan(0.0));
assertThat(t1, greaterThan(t0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.flowingcode.addons.simpletimer.integration;

import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elementsbase.Element;
import java.util.Optional;

@Element("simple-timer")
public class SimpleTimerElement extends TestBenchElement {

Double currentTime() {
Number time = (Number) executeScript("return arguments[0].currentTime", this);
return Optional.ofNullable(time).map(Number::doubleValue).orElse(null);
}

}
Loading
Loading