Skip to content

Commit

Permalink
minor tweaks to mvn CLE invocation:
Browse files Browse the repository at this point in the history
- parameterised environment value
- Created a @BeforeAll in PlaywrightTest4BrowserStack to read environment setting
  • Loading branch information
Moonbeam69 committed May 14, 2024
1 parent d4cfab0 commit d0cab04
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 77 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/CICD_Main_Pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
matrix:
browser: [ chrome ]
profile: [ Localtests ]
environment: [ DevVer ]
steps:
- uses: actions/checkout@v4

Expand All @@ -43,7 +44,7 @@ jobs:
- name: Start timing
run: echo "START_TIME=$(date +%s)" >> $GITHUB_ENV
- name: Build & Test DevVer with Maven
run: mvn -B package --file pom.xml -Denvironment=DevVer -Dbrowser=${{ matrix.browser }} -P ${{ matrix.profile}}
run: mvn -B package --file pom.xml -Denvironment=${{matrix.environment}} -Dbrowser=${{ matrix.browser }} -P ${{ matrix.profile}}
- name: End timing
run: |
END_TIME=$(date +%s)
Expand All @@ -62,6 +63,7 @@ jobs:
matrix:
browser: [ chrome, firefox ]
profile: [ Localtests ]
environment: [ DevInt ]
steps:
- uses: actions/checkout@v4
- name: Set up JDK 19
Expand All @@ -71,7 +73,7 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build & Test DevInt with Maven
run: mvn -B package --file pom.xml -Denvironment=DevInt -Dbrowser=${{ matrix.browser }} -P ${{ matrix.profile}}
run: mvn -B package --file pom.xml -Denvironment=${{matrix.environment}} -Dbrowser=${{ matrix.browser }} -P ${{ matrix.profile}}
- name: Upload test results
uses: actions/upload-artifact@v4
with:
Expand All @@ -83,8 +85,8 @@ jobs:
needs: DevInt
strategy:
matrix:
browser: [ chrome,firefox ]
profile: [ Browserstack ]
environment: [ SIT ]
steps:
- uses: actions/checkout@v4
- name: Set up JDK 19
Expand All @@ -94,7 +96,7 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build & test SIT with Maven
run: mvn -B package --file pom.xml -D environment=SIT -D browser=${{ matrix.browser }} -P ${{ matrix.profile}}
run: mvn -B package --file pom.xml -D environment=${{matrix.environment}} -P ${{ matrix.profile}}

# - name: Remove remove previous Docker DevInt container instance
# run: docker rm -f SIT
Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
# CICD Demo Project



## Description

I built this project to demonstrate the basics principles of CICD pipeline development. I've used Github Actions, (self-hosted) Runners, Junit test cases, Playwright test cases and Docker.
I built this project to demonstrate the basics principles of CICD pipeline development. I've used Github Actions, (self-hosted) Runners, Browserstack integrationb, Junit unit test cases, Playwright browser test cases and the creation of a Docker image.

### Environment
- Local env Windows 10
- 3x self-hosted runners: 2x openSUSE Leap 15.5, 1x Windows 10
- Intellij 2024.1 CE, SDK 19
- Browserstack trial account
- IDE Intellij 2024.1 CE, SDK 19
- OpenJDK 19 "temurin" on runners
- Personal Github account
- Playwright 1.42
- Junit5
- DockerDesktop 4.29

### CICD Approach
### CICD Release Strategy

1. After a Push or Pull Request into master: BuildandPushDocker.yml is executed
2. CICD_Main_Pipeline.yml performs the following 3 steps:
1. After a Push or Pull Request into master, CICD_Main_Pipeline.yml is executed
2. CICD_Main_Pipeline.yml performs the following 4 steps:
- Setup: JDK 19 (OpenJDK 'temurin' distribution) is deployed to the runner (self-hosted)
- Build: Maven builds the project into a JAR, run the tests (in main/test/java)
- Docker: Docker creates a new (local) Docker image from the project Dockerfile, copies and executes the mainClass from project Jar (no test execution)
- Docker: Docker creates a new Docker image from the project Dockerfile, copies and executes the mainClass from project Jar (no test execution)
- Pipeline yml to executes with the following test data combos:

DevVer with profile for self-hosted runner: linux + chrome
DevInt with profile for self-hosted runner: linux + chrome + firefox
SIT with profile for Browserstack: Windows10 + chrome

### Backlog

0. Support for test executions on multiple browser and platforms, using in-built browser support (FF & Chr)), others incl Browserstack

Done. using matrix syntax in workflow yml. Safari is supported if self-hosted host has Safari installed. No Safari
Done. using matrix syntax in workflow yml. Safari is supported if self-hosted host has Safari installed. No Safari support on Linux.
Multiple profiles in pom.xml can be used to refine which test cases are executed.

1. A JAR deployment for CICD will require the test cases to be packaged and deployed also. If the JAR itself is a managed item that can, in theory,
be promoted to production, it should not contain any test classes. It is possible to build a jar with the SUT classes and a separate JAR with test classes:
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/tests/Playwright1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ class Playwright1Test {
@BeforeAll
static void setup() {
String browser = System.getProperty("browser");
String environment = System.getProperty("environment");

com.microsoft.playwright.Playwright playwright = com.microsoft.playwright.Playwright.create();

logger.info("Executing agaist " + browser);
logger.info("Executing in " + environment);

switch (browser) {
case "chrome":
browserType = playwright.chromium();
Expand Down
106 changes: 42 additions & 64 deletions src/test/java/tests/PlaywrightTest4BrowserStack.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package tests;

import com.microsoft.playwright.*;
import org.junit.jupiter.api.*;
import runners.*;

import java.util.regex.*;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;


class PlaywrightTest4BrowserStack {

private static final org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(PlaywrightTest4BrowserStack.class);


@BeforeAll
static void setup() {
String environment = System.getProperty("environment");
logger.info("Executing in " + environment);
}

@PlaywrightTest
void tc1_Navigation(Browser browser) {
long startTime = System.currentTimeMillis();
BrowserContext context = browser.newContext();

try (Playwright playwright = Playwright.create()) {
BrowserType browserType = playwright.firefox();

BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://playwright.dev");

Page page = context.newPage();
page.navigate("https://playwright.dev");
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());

// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());
page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");

page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");
} catch (Exception e) {
throw new RuntimeException(e);
}
browser.close();

long endTime = System.currentTimeMillis();
Expand All @@ -46,26 +44,19 @@ void tc2_Navigation(Browser browser) {

long startTime = System.currentTimeMillis();

try (Playwright playwright = Playwright.create()) {
BrowserType browserType = playwright.chromium();

BrowserContext context = browser.newContext();

Page page = context.newPage();
page.navigate("https://playwright.dev");
BrowserContext context = browser.newContext();

// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());
Page page = context.newPage();
page.navigate("https://playwright.dev");

page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");
// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());

browser.close();
page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");

} catch (Exception e) {
throw new RuntimeException(e);
}
browser.close();

long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
Expand All @@ -79,57 +70,44 @@ void tc3_Navigation(Browser browser) {

long startTime = System.currentTimeMillis();

try (Playwright playwright = Playwright.create()) {
BrowserType browserType = playwright.chromium();

BrowserContext context = browser.newContext();
BrowserContext context = browser.newContext();

Page page = context.newPage();
page.navigate("https://playwright.dev");
Page page = context.newPage();
page.navigate("https://playwright.dev");

// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());
// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());

page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");
page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");

browser.close();
browser.close();

} catch (Exception e) {
throw new RuntimeException(e);
}
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
logger.info("Elapsed time in milliseconds: " + elapsedTime);


}

@PlaywrightTest
void tc4_Navigation(Browser browser) {

long startTime = System.currentTimeMillis();

try (Playwright playwright = Playwright.create()) {
BrowserContext context = browser.newContext();

BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://playwright.dev");

Page page = context.newPage();
page.navigate("https://playwright.dev");
// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());

// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));
logger.info(page.title());
page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");

page.locator("//html/body/div/div[2]/header/div/div/a").click();
assertThat(page).hasTitle("Installation | Playwright");

browser.close();

} catch (Exception e) {
throw new RuntimeException(e);
}
browser.close();

long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
Expand Down

0 comments on commit d0cab04

Please sign in to comment.