Skip to content

Commit

Permalink
feat(playwright): split current module in two modules: e2e tests and …
Browse files Browse the repository at this point in the history
…component-tests

Fixes #10613
  • Loading branch information
murdos committed Aug 23, 2024
1 parent d97b68b commit b9d6135
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public PlaywrightApplicationService() {
factory = new PlaywrightModuleFactory();
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
return factory.buildModule(properties);
public JHipsterModule buildComponentTestsModule(JHipsterModuleProperties properties) {
return factory.buildComponentTestsModule(properties);
}

public JHipsterModule buildE2ETestsModule(JHipsterModuleProperties properties) {
return factory.buildE2ETestsModule(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.packagejson.VersionSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
Expand All @@ -12,28 +13,61 @@ public class PlaywrightModuleFactory {

private static final JHipsterSource SOURCE = from("client/common/playwright");

private static final String WEBAPP_COMPONENT_TESTS = "src/test/webapp/component/";
private static final String PLAYWRIGHT_TESTS = WEBAPP_COMPONENT_TESTS + "common/primary/app";
private static final JHipsterDestination WEBAPP_COMPONENT_TESTS = to("src/test/webapp/component/");
private static final JHipsterDestination WEBAPP_E2E_TESTS = to("src/test/webapp/e2e/");
private static final String PLAYWRIGHT_TESTS = "common/primary/app";

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
public JHipsterModule buildComponentTestsModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

//@formatter:off
return moduleBuilder(properties)
return commonPlaywrightModuleBuilder(properties, WEBAPP_COMPONENT_TESTS)
.packageJson()
.addDevDependency(packageName("@playwright/test"), VersionSource.COMMON)
.addDevDependency(packageName("start-server-and-test"), VersionSource.COMMON)
.addScript(scriptKey("test:component"), scriptCommand("start-server-and-test start http://localhost:9000 'playwright test --ui --config src/test/webapp/component/playwright.config.ts'"))
.addScript(scriptKey("test:component:headless"), scriptCommand("start-server-and-test start http://localhost:9000 'playwright test --config src/test/webapp/component/playwright.config.ts'"))
.and()
.context()
.put("reportSubDirectory", "component-tests")
.put("resultsSubDirectory", "component-tests")
.and()
.build();
//@formatter:on
}

public JHipsterModule buildE2ETestsModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

//@formatter:off
return commonPlaywrightModuleBuilder(properties, WEBAPP_E2E_TESTS)
.packageJson()
.addScript(scriptKey("e2e"), scriptCommand("playwright test --ui --config src/test/webapp/e2e/playwright.config.ts"))
.addScript(scriptKey("e2e:headless"), scriptCommand("playwright test --config src/test/webapp/e2e/playwright.config.ts"))
.and()
.context()
.put("reportSubDirectory", "e2e-tests")
.put("resultsSubDirectory", "e2e-tests")
.and()
.build();
//@formatter:on
}

private static JHipsterModuleBuilder commonPlaywrightModuleBuilder(
JHipsterModuleProperties properties,
JHipsterDestination destinationFolder
) {
//@formatter:off
return moduleBuilder(properties)
.packageJson()
.addDevDependency(packageName("@playwright/test"), VersionSource.COMMON)
.and()
.files()
.add(SOURCE.template("playwright.config.ts"), to(WEBAPP_COMPONENT_TESTS + "playwright.config.ts"))
.batch(SOURCE.append(PLAYWRIGHT_TESTS), to(PLAYWRIGHT_TESTS))
.add(SOURCE.template("playwright.config.ts"), destinationFolder.append("playwright.config.ts"))
.batch(SOURCE.append(PLAYWRIGHT_TESTS), destinationFolder.append(PLAYWRIGHT_TESTS))
.addFile("Home.spec.ts")
.addFile("Home-Page.ts")
.and()
.and()
.build();
//@formatter:off
.and();
//@formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@
class PlaywrightModuleConfiguration {

@Bean
JHipsterModuleResource playwrightModule(PlaywrightApplicationService playwright) {
JHipsterModuleResource playwrightComponentTestsModule(PlaywrightApplicationService playwright) {
return JHipsterModuleResource.builder()
.slug(PLAYWRIGHT)
.slug(PLAYWRIGHT_COMPONENT_TESTS)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().build())
.apiDoc("E2e", "Add Playwright")
.organization(JHipsterModuleOrganization.builder().feature(FRONT_BROWSER_TEST).addDependency(CLIENT_CORE).build())
.apiDoc("E2E", "Configure frontend component tests using Playwright")
.organization(JHipsterModuleOrganization.builder().feature(FRONTEND_COMPONENT_TESTS).addDependency(CLIENT_CORE).build())
.tags("client", "test", "playwright", "frontend")
.factory(playwright::buildComponentTestsModule);
}

@Bean
JHipsterModuleResource playwrightE2ETestsModule(PlaywrightApplicationService playwright) {
return JHipsterModuleResource.builder()
.slug(PLAYWRIGHT_E2E)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().build())
.apiDoc("E2E", "Configure E2E tests using Playwright")
.organization(JHipsterModuleOrganization.builder().feature(E2E_TESTS).addDependency(CLIENT_CORE).build())
.tags("client", "test", "playwright", "e2e")
.factory(playwright::buildModule);
.factory(playwright::buildE2ETestsModule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
NEO4J_MIGRATIONS("neo4j-migrations"),
OPTIONAL_TYPESCRIPT("optional-typescript"),
PAGINATION_DOMAIN("pagination-domain"),
PLAYWRIGHT("playwright"),
PLAYWRIGHT_COMPONENT_TESTS("playwright-component-tests"),
PLAYWRIGHT_E2E("playwright-e2e"),
POSTGRESQL("postgresql"),
PRETTIER("prettier"),
PROTOBUF("protobuf"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default defineConfig({
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: [['list'], ['html', { outputFolder: '../../../../{{ projectBuildDirectory }}/playwright-report/component-tests' }]],
reporter: [['list'], ['html', { outputFolder: '../../../../{{ projectBuildDirectory }}/playwright-report/{{ reportSubDirectory }}' }]],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
Expand Down Expand Up @@ -92,5 +92,5 @@ export default defineConfig({
],

/* Folder for test artifacts such as screenshots, videos, traces, etc. */
outputDir: '../../../../{{ projectBuildDirectory }}/playwright-results/component-tests',
outputDir: '../../../../{{ projectBuildDirectory }}/playwright-results/{{ resultsSubDirectory }}',
});
10 changes: 8 additions & 2 deletions src/test/features/client/browser-test/playwright.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Feature: Playwright module

Scenario: Should apply Playwright module
When I apply "playwright" module to default project with package json
Scenario: Should apply Playwright component tests module
When I apply "playwright-component-tests" module to default project with package json
| serverPort | 9000 |
Then I should have files in "src/test/webapp/component/common/primary/app"
| Home.spec.ts |

Scenario: Should apply Playwright E2E module
When I apply "playwright-e2e" module to default project with package json
| serverPort | 9000 |
Then I should have files in "src/test/webapp/e2e/common/primary/app"
| Home.spec.ts |
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class PlaywrightModuleFactoryTest {
private static final PlaywrightModuleFactory factory = new PlaywrightModuleFactory();

@Test
void shouldBuildModule() {
void shouldBuildComponentTestsModule() {
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build();

JHipsterModule module = factory.buildModule(properties);
JHipsterModule module = factory.buildComponentTestsModule(properties);

assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("package.json")
Expand All @@ -44,4 +44,24 @@ void shouldBuildModule() {
.and()
.hasPrefixedFiles("src/test/webapp/component/common/primary/app", "Home.spec.ts", "Home-Page.ts");
}

@Test
void shouldBuildE2ETestsModule() {
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()).build();

JHipsterModule module = factory.buildE2ETestsModule(properties);

assertThatModuleWithFiles(module, packageJsonFile())
.hasFile("package.json")
.containing(nodeDependency("@playwright/test"))
.containing(nodeScript("e2e", "playwright test --ui --config src/test/webapp/e2e/playwright.config.ts"))
.containing(nodeScript("e2e:headless", "playwright test --config src/test/webapp/e2e/playwright.config.ts"))
.and()
.hasFile("src/test/webapp/e2e/playwright.config.ts")
.containing("baseURL: process.env.URL || 'http://localhost:9000',")
.containing("outputFolder: '../../../../target/playwright-report/e2e-tests'")
.containing("outputDir: '../../../../target/playwright-results/e2e-tests'")
.and()
.hasPrefixedFiles("src/test/webapp/e2e/common/primary/app", "Home.spec.ts", "Home-Page.ts");
}
}
2 changes: 1 addition & 1 deletion tests-ci/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ elif [[ $application == 'vueapp' ]]; then
applyModules \
"vue-core" \
"vue-pinia" \
"playwright"
"playwright-component-tests"

elif [[ $application == 'svelteapp' ]]; then
init_server
Expand Down

0 comments on commit b9d6135

Please sign in to comment.