Skip to content

Commit

Permalink
Feature/monkey tests (#429)
Browse files Browse the repository at this point in the history
* github actions, playwriht version update

* fix on working directory in github actions

* fix

* video off

* Monkey Tests introduces

* fix on Allure story
  • Loading branch information
MalgorzataDzienia authored Jul 12, 2024
1 parent 61c58f2 commit 52dfe30
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 261 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/test-execution.yml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions mrchecker-playwright-framework-empty/playwrightRecoding.txt
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 1 addition & 1 deletion mrchecker-playwright-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<thread.count>3</thread.count>
<env>ENV1</env>
<!-- PLAYWRIGHT -->
<playwright.verion>1.42.0</playwright.verion>
<playwright.verion>1.44.0</playwright.verion>

<!-- JUNIT -->
<junit.version>5.10.1</junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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<ElementHandle> 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<ElementHandle> 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<Dialog> handler = dialog -> {
Logger.logInfo("Popup message: " + dialog.message());
Logger.logInfo("Popup message will be OK: " + pressOK);
// Which option you want to click
if (dialog.message().contains(messageText)) {
if (pressOK) {
// Click on button "OK"
dialog.accept();
Logger.logInfo("Clicked on 'OK'");
} else {
// Click on button "Cancel"
dialog.dismiss();
Logger.logInfo("Clicked on 'Cancel'");
}
}else {
fail("Popup message is not as expected " + dialog.message() + " but it should be " + messageText);
}
};
getPage().onDialog(handler);
getPage().offDialog(handler);
}
@Step("Check that {fieldName} has text {expectedText}")
public static void checkText(String fieldName, String expectedText, Locator locator) {
assertThat(locator.textContent())
.as(fieldName)
.isEqualTo(expectedText);
}


}
Loading

0 comments on commit 52dfe30

Please sign in to comment.