From 52dfe309ffbec52831187801371605934e1f2ab9 Mon Sep 17 00:00:00 2001
From: Malgorzata Dzienia <39510143+MalgorzataDzienia@users.noreply.github.com>
Date: Fri, 12 Jul 2024 07:49:37 +0200
Subject: [PATCH] Feature/monkey tests (#429)
* github actions, playwriht version update
* fix on working directory in github actions
* fix
* video off
* Monkey Tests introduces
* fix on Allure story
---
.github/workflows/test-execution.yml | 46 ++++
.../playwrightRecoding.txt | 39 +++
mrchecker-playwright-framework/pom.xml | 2 +-
.../framework/actions/ActionGui.java | 169 +++++++++++-
.../demoqaforms/helper/GremlinsHelper.java | 162 +++++++++++
.../com/capgemini/monkeyTest/MonkeyTests.java | 61 +++++
.../TestTemplateGenerated.java | 254 ------------------
.../src/test/resources/js/gremlins.min.js | 2 +
.../src/test/resources/playwright.properties | 2 +-
9 files changed, 476 insertions(+), 261 deletions(-)
create mode 100644 .github/workflows/test-execution.yml
create mode 100644 mrchecker-playwright-framework-empty/playwrightRecoding.txt
create mode 100644 mrchecker-playwright-framework/src/main/java/com/capgemini/pages/demoqaforms/helper/GremlinsHelper.java
create mode 100644 mrchecker-playwright-framework/src/test/java/com/capgemini/monkeyTest/MonkeyTests.java
delete mode 100644 mrchecker-playwright-framework/src/test/java/com/capgemini/testTemplateGeneratorFromXrayCsv/TestTemplateGenerated.java
create mode 100644 mrchecker-playwright-framework/src/test/resources/js/gremlins.min.js
diff --git a/.github/workflows/test-execution.yml b/.github/workflows/test-execution.yml
new file mode 100644
index 00000000..eeaf8471
--- /dev/null
+++ b/.github/workflows/test-execution.yml
@@ -0,0 +1,46 @@
+name: Test execution
+
+on:
+ push:
+ paths:
+ - mrchecker-playwright-framework/**
+
+jobs:
+ test:
+ timeout-minutes: 30
+ runs-on: ubuntu-latest
+ container:
+ image: mcr.microsoft.com/playwright/java:v1.44.0-jammy
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Set up JDK and Maven
+ uses: actions/setup-java@v2
+ with:
+ java-version: '21'
+ distribution: 'zulu'
+ maven-version: '3.8.1'
+
+ - name: Install git
+ run: apt-get -y update && apt-get install -y git-lfs
+
+ - name: Install tests
+ run: mvn install -DskipTests --no-transfer-progress
+ working-directory: ./mrchecker-playwright-framework
+
+ - name: Run tests
+ run: mvn test -Dheadless=true
+ working-directory: ./mrchecker-playwright-framework
+
+ - name: Generate Allure report
+ run: mvn allure:report
+ working-directory: ./mrchecker-playwright-framework
+
+ - name: Publish test report
+ uses: peaceiris/actions-gh-pages@v4
+ if: always()
+ with:
+ github_token: ${{ secrets.GITHUB_TOKEN }}
+ publish_branch: allure-report
+ publish_dir: ./mrchecker-playwright-framework/target/allure-report
\ No newline at end of file
diff --git a/mrchecker-playwright-framework-empty/playwrightRecoding.txt b/mrchecker-playwright-framework-empty/playwrightRecoding.txt
new file mode 100644
index 00000000..cc991d2a
--- /dev/null
+++ b/mrchecker-playwright-framework-empty/playwrightRecoding.txt
@@ -0,0 +1,39 @@
+ page.navigate("https://demoqa.com/login");
+ page.locator("span").filter(new Locator.FilterOptions().setHasText("Forms")).locator("div").first().click();
+ page.getByRole(AriaRole.LISTITEM).click();
+ page.getByPlaceholder("First Name").click();
+ page.getByPlaceholder("First Name").fill("Name");
+ page.getByPlaceholder("Last Name").click();
+ page.getByPlaceholder("Last Name").fill("LastName");
+ page.getByPlaceholder("name@example.com").click();
+ page.getByPlaceholder("name@example.com").fill("my@google.pl");
+ page.getByText("Male", new Page.GetByTextOptions().setExact(true)).click();
+ page.getByText("Female").click();
+ page.getByText("Other").click();
+ page.getByPlaceholder("Mobile Number").click();
+ page.getByPlaceholder("Mobile Number").fill("1234567");
+ page.locator("#dateOfBirthInput").click();
+ page.getByLabel("Choose Sunday, April 14th,").click();
+ page.locator(".subjects-auto-complete__value-container").click();
+ page.locator("#subjectsInput").fill("Math");
+ page.locator("div").filter(new Locator.FilterOptions().setHasText(Pattern.compile("^Math$"))).nth(1).click();
+ page.locator(".subjects-auto-complete__value-container").click();
+ page.locator("#subjectsInput").fill("a");
+ Page page1 = page.waitForPopup(() -> {
+ page.frameLocator("iframe[name=\"google_ads_iframe_\\/21849154601\\,22343295815\\/Ad\\.Plus-Anchor_0\"]").locator("html").click();
+ });
+ page.locator(".subjects-auto-complete__value-container").click();
+ page.locator("#subjectsInput").fill("a");
+ page.getByText("Maths", new Page.GetByTextOptions().setExact(true)).click();
+ page.getByText("Sports").click();
+ page.getByText("Reading").click();
+ page.getByText("Music").click();
+ page.getByPlaceholder("Current Address").click();
+ page.getByPlaceholder("Current Address").fill("ala ma kota");
+ page.locator("#state svg").click();
+ page.getByText("Uttar Pradesh", new Page.GetByTextOptions().setExact(true)).click();
+ page.locator("#city svg").click();
+ page.getByText("Agra", new Page.GetByTextOptions().setExact(true)).click();
+ page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).click();
+ page.getByPlaceholder("Mobile Number").click();
+ page.getByPlaceholder("Mobile Number").fill("1234567890");
\ No newline at end of file
diff --git a/mrchecker-playwright-framework/pom.xml b/mrchecker-playwright-framework/pom.xml
index f0874890..3ac4ed7c 100644
--- a/mrchecker-playwright-framework/pom.xml
+++ b/mrchecker-playwright-framework/pom.xml
@@ -34,7 +34,7 @@
3
ENV1
- 1.42.0
+ 1.44.0
5.10.1
diff --git a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/actions/ActionGui.java b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/actions/ActionGui.java
index 798ad31b..9395dc75 100644
--- a/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/actions/ActionGui.java
+++ b/mrchecker-playwright-framework/src/main/java/com/capgemini/framework/actions/ActionGui.java
@@ -2,18 +2,30 @@
import com.capgemini.framework.logger.AllureStepLogger;
import com.capgemini.framework.logger.Logger;
+import com.microsoft.playwright.Dialog;
+import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.LoadState;
+import com.microsoft.playwright.options.WaitForSelectorState;
import io.qameta.allure.Step;
+import org.assertj.core.api.Assertions;
+import org.assertj.core.api.Fail;
+import java.util.List;
+import java.util.function.Consumer;
+
+import static com.capgemini.framework.playwright.PlaywrightFactory.getBrowserContext;
import static com.capgemini.framework.playwright.PlaywrightFactory.getPage;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
public final class ActionGui {
@Step("Fill text field \"{fieldName}\" with \"{inputValue}\"")
public static void fillTextBox(String fieldName, String inputValue, Locator locator) {
locator.click();
locator.fill(inputValue);
+ AllureStepLogger.info(inputValue + " in textbox " + fieldName + " is typed");
}
public static void fillTextBox(String fieldName, String inputValue, String selector) {
@@ -23,6 +35,7 @@ public static void fillTextBox(String fieldName, String inputValue, String selec
@Step("Set radiobutton \"{fieldName}\"")
public static void setRadioButton(String fieldName, Locator locator) {
locator.click();
+ AllureStepLogger.info(fieldName + " is selected");
}
public static void setRadioButton(String fieldName, String selector) {
@@ -31,29 +44,111 @@ public static void setRadioButton(String fieldName, String selector) {
@Step("Click \"{fieldName}\"")
public static void click(String fieldName, Locator locator) {
- locator.click();
+ if(!locator.isVisible()){
+ Fail.fail(fieldName + " is not visible and cannot be clicked.");
+ }
+ if(!locator.isEnabled()){
+ Fail.fail(fieldName + " is not enabled and cannot be clicked.");
+ }
+ try {
+ locator.click(new Locator.ClickOptions().setTimeout(20000));
+ } catch (Exception e) {
+ Logger.logError("Error while clicking on " + fieldName + ": " + e.getMessage());
+ Fail.fail(fieldName + " cannot be clicked.");
+ }
+ AllureStepLogger.info(fieldName + " is clicked");
+ }
+
+ public static void click(String fieldName, Locator locator, double timeout) {
+ if(!locator.isVisible()){
+ Fail.fail(fieldName + " is not visible and cannot be clicked.");
+ }
+ if(!locator.isEnabled()){
+ Fail.fail(fieldName + " is not enabled and cannot be clicked.");
+ }
+ try {
+ locator.click(new Locator.ClickOptions().setTimeout(timeout));
+ } catch (Exception e) {
+ Logger.logError("Error while clicking on " + fieldName + ": " + e.getMessage());
+ Fail.fail(fieldName + " cannot be clicked. " + e.getMessage());
+ }
+ AllureStepLogger.info(fieldName + " is clicked");
+ }
+
+ public static void click(String fieldName, Locator locator, boolean waitForPageLoadingToFinish) {
+ click(fieldName, locator);
+ if (waitForPageLoadingToFinish) {
+ ActionGui.waitForPageLoadingFinish();
+ }
}
public static void pressEnter(Locator locator) {
locator.press("Enter");
}
-
+ @Step("Select value \"{value}\" in Dropdown \"{fieldName}\"")
+ public static void selectDropdownValue(String value, String fieldName, Locator locator) {
+ locator.click();
+ locator.selectOption(value);
+ AllureStepLogger.info(value + " in Dropdown " + fieldName + " is selected");
+ }
@Step("Verify {fieldName} is visible")
public static void verifyElementVisible(String fieldName, Locator locator) {
- locator.isVisible();
+ Assertions.assertThat(locator.isVisible()).as(fieldName + " visible").withFailMessage(fieldName + " is not visible").isTrue();
}
public static void verifyElementVisible(String fieldName, String selector) {
verifyElementVisible(fieldName, getPage().locator(selector));
}
-
+ @Step("Wait for {fieldName} to disappear")
+ public static void waitForElementToDisappear(String fieldName, Locator locator) {
+ locator.waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN));
+ }
+
@Step("Open page {url}")
public static void navigate(String url, int pageLoadingTimeout) {
getPage().navigate(url, new Page.NavigateOptions().setTimeout(pageLoadingTimeout));
getPage().onLoad(p -> AllureStepLogger.info("Page loaded!"));
}
-
+ @Step("Check {fieldName} checkbox")
+ public static void checkCheckBox(String fieldName, Locator locator) {
+ locator.check();
+ AllureStepLogger.info(fieldName + " checkbox is checked");
+ }
+ public static void clickIfVisible(String fieldName, Locator locator, boolean b) {
+ if (locator.isVisible()) {
+ click(fieldName, locator, b);
+ }
+ }
+
+ @Step("Check Banner has text {bannerText}")
+ public static void checkBannerText(String bannerText, Locator locator) {
+ AllureStepLogger.info("Banner text is: " + locator.textContent());
+ assertThat(locator.textContent()).withFailMessage("Text on banner is " + locator.textContent() + " but it should be " + bannerText)
+ .contains(bannerText);
+ }
+
+ @Step("Get Banner text")
+ public static String getBannerText() {
+ Locator banner;
+ try{ banner = getPage().locator("div[class*='PageControllerStatusLine']");}
+ catch(Exception e){
+ return "";
+ };
+ AllureStepLogger.info("Banner text is: " + banner.textContent());
+ return banner.textContent();
+ }
+ @Step("Check that {element} has text {expectedText}")
+ public static void verifyElementHasText(String element, String expectedText, Locator locator) {
+ AllureStepLogger.info(element + " text is: " + locator.textContent());
+ assertThat(locator.textContent()).withFailMessage("Text is " + locator.textContent() + " but it should be " + expectedText)
+ .contains(expectedText);
+ }
+
+ public static boolean isElementPresent(Locator locator) {
+ return locator.isVisible();
+ }
+
public void waitForPageToLoad() {
getPage().waitForLoadState(LoadState.NETWORKIDLE);
}
@@ -72,4 +167,68 @@ public static void waitMilliseconds(Integer milliseconds) {
}
}
+ @Step("Wait for page loading to finish")
+ public static void waitForPageLoadingFinish(){
+ String loadingAnimation = "div[id^='RadAjaxLoadingPanel']";
+ waitForElementToDisappear("Loading animation", getPage().locator(loadingAnimation).first());
+ //second version: getPage().waitForSelector(loadingAnimation).waitForElementState(ElementState.HIDDEN);
+ }
+
+
+ @Step("Check on column name {columnName} for first row text {searchText}")
+ public static boolean isTextPresentInFirstRow(String columnName, String searchText) {
+ int columnIndex = getColumnIndex(columnName);
+ List rows = getPage().querySelectorAll("table tr");
+ if (!rows.isEmpty()) {
+ ElementHandle firstRow = rows.get(0);
+ String cellText = firstRow.querySelectorAll("td span").get(columnIndex).textContent();
+ return cellText.contains(searchText);
+ } else {
+ return false;
+ }
+ }
+
+ @Step("Find index for column name {columnName}")
+ private static int getColumnIndex(String columnName) {
+ List headers = getPage().querySelectorAll("table th a span");
+ for (int i = 0; i < headers.size(); i++) {
+ String headerText = headers.get(i).textContent();
+ if (headerText.equals(columnName)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ @Step("Create a dialog handler that will check message text and press OK/Cancel")
+ public static void checkPopupMessageTextAndPressOKCancel(boolean pressOK, String messageText) {
+ Consumer