Skip to content

Commit

Permalink
Monkey Tests introduces
Browse files Browse the repository at this point in the history
  • Loading branch information
MalgorzataDzienia committed Jul 12, 2024
1 parent 26e24fe commit 0a344ef
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.capgemini.pages.demoqaforms.helper;

import com.microsoft.playwright.Page;
import io.qameta.allure.Allure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* @author Michal Carlo
* @since 2024-03-19
* Gremlins is a monkey testing library that performs random actions on the page.
*/

public final class GremlinsHelper {
public static final String GREMLIN_JS_EXPRESSION = "gremlin JS expression";
static final Path PATH_TO_GREMLINS = Paths.get("target/js/node_modules/gremlins.min.js");
private static final Logger LOGGER = LoggerFactory.getLogger(GremlinsHelper.class.getCanonicalName());

public GremlinsHelper() {
}

/**
* Get JS script for Gremlins.
*
* @throws IOException Gremlins script not found
*/
public static void copyGremlinsJSFromResources() throws IOException {
Files.createDirectories(Paths.get("target/js/node_modules"));
try (InputStream stream = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResourceAsStream("js/gremlins.min.js"))) {
Files.copy(stream, PATH_TO_GREMLINS);
}
}

/**
* Install Gremlins script in the environment.
*
* @param page Playwright page
* @throws IOException Gremlins script not found
*/
public static void setupGremlinsScript(
final Page page) throws IOException {
if (!Files.exists(PATH_TO_GREMLINS)) {
copyGremlinsJSFromResources();
}
page.evaluate( Files.readString( Path.of(System.getProperty("user.dir") + "/" + PATH_TO_GREMLINS), Charset.defaultCharset()));
}

/**
* unleash Gremlin horde with default settings.
*
* @param page Playwright page
*/
public static void startGremlinHorde(
final Page page) {
page.evaluate("() => gremlins.createHorde().unleash()");
}

/**
* unleash Gremlin horde with repeatable behaviour due to overwritten seed value.
*
* @param page Playwright page
* @param randomizerValue seed value to influence gremlin behaviour
*/
public static void startGremlinHordeWithRepeatableBehaviour(
final Page page,
final int randomizerValue) {
String expression = "const horde = gremlins.createHorde({randomizer: new gremlins.Chance(%%%)})\n"
.replace("%%%", String.valueOf(randomizerValue))
+ "horde.unleash()";
LOGGER.debug("random gremlin horde expression: {}", expression);
Allure.addAttachment(GREMLIN_JS_EXPRESSION, expression);
page.evaluate(expression);
}

/**
* unleash Gremlin horde with a particular Gremlin number.
*
* @param page Playwright page
* @param gremlinCount number of Gremlins, tne higher, the longer the attack lasts.
*/
public static void startGremlinHordeWithGremlinCount(
final Page page,
final int gremlinCount) {
String expression = "const horde = gremlins.createHorde({ strategies: [gremlins.strategies.allTogether({ nb: %%%})],}) \n"
.replace("%%%", String.valueOf(gremlinCount))
+ "horde.unleash()";
LOGGER.debug("gremlin count expression: {}", expression);
Allure.addAttachment(GREMLIN_JS_EXPRESSION, expression);
page.evaluate(expression);
}

/**
* unleash Gremlin horde with particular Gremlin species.
*
* @param page Playwright page
* @param gremlinSpeciesList list of gremlin species to use, allowed values "clicker", "toucher", "formFiller", "scroller", "typer"
*/
public static void startGremlinHordeWithGremlinTypes(
final Page page,
final List<String> gremlinSpeciesList) {
String speciesList = parseGremlinsSpeciesList(gremlinSpeciesList);
String expression = "gremlins.createHorde({\n"
+ " species: %%%\n ".replace(" %%%", speciesList)
+ " })\n"
+ " .unleash();";
LOGGER.debug("gremlin type expression: {}", expression);
Allure.addAttachment(GREMLIN_JS_EXPRESSION, expression);
page.evaluate(expression);
}

/**
* unleash Gremlin horde with selectable count and species.
*
* @param page Playwright page
* @param gremlinCount number of Gremlins, tne higher, the longer the attack lasts
* @param delay delay between attacks in ms
* @param gremlinSpeciesList list of gremlin species to use, allowed values "clicker", "toucher", "formFiller", "scroller", "typer"
*/
public static void startGremlinHordeFullyCustomisable(
final Page page,
final int gremlinCount,
final int delay,
final List<String> gremlinSpeciesList) {
String speciesList = parseGremlinsSpeciesList(gremlinSpeciesList);
String expression = "gremlins.createHorde({\n"
+ " species: %%%,\n".replace(" %%%", speciesList)
+ " strategies:[gremlins.strategies.allTogether({ nb: %%%})],\n".replace("%%%", String.valueOf(gremlinCount))
+ " delay: %%%,\n".replace("%%%", String.valueOf(delay))
+ " })\n"
+ " .unleash();";
LOGGER.debug("gremlin custom horde expression: {}", expression);
Allure.addAttachment(GREMLIN_JS_EXPRESSION, expression);
page.evaluate(expression);
}

private static String parseGremlinsSpeciesList(
final List<String> gremlinSpeciesList) {
List<String> parsedGremlinsList = new ArrayList<>();
for (String gremlinSpecies : gremlinSpeciesList) {
if (isSpeciesAllowed(gremlinSpecies)) {
parsedGremlinsList.add("gremlins.species." + gremlinSpecies + "()");
}
}
return parsedGremlinsList.toString();
}

private static boolean isSpeciesAllowed(final String gremlinSpecies) {
List<String> allowedSpecies = Arrays.asList("clicker", "toucher", "formFiller", "scroller", "typer");
return allowedSpecies.contains(gremlinSpecies);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.capgemini.monkeyTest;

import com.capgemini.framework.enums.PrjEpics;
import com.capgemini.framework.logger.AllureStepLogger;
import com.capgemini.framework.logger.Logger;
import com.capgemini.framework.playwright.BaseTest;
import com.capgemini.framework.tags.Status;
import com.capgemini.pages.demoqaforms.DemoQALoginPage;
import com.capgemini.pages.demoqaforms.helper.GremlinsHelper;
import io.qameta.allure.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static com.capgemini.framework.playwright.PlaywrightFactory.getPage;

/**
* @author Michal Carlo
* @author Malgorzata Dzienia
* @since 2024-03-19
* Gremlins is a monkey testing library that performs random actions on the page.
*/

@Epic(PrjEpics.DEMO_QA)
public class MonkeyTests extends BaseTest {

private final DemoQALoginPage demoQALoginPage = new DemoQALoginPage();


@Step("[SETUP]")
@BeforeEach
public void beforeEach() {
AllureStepLogger.step("This is example step inside setUpTest()");
demoQALoginPage.startPage();
getPage().onConsoleMessage((msg) -> {
Logger.logInfo(msg.text());
});

}
@TmsLink("Test Management System ID")
@Feature("GUI")
@Description("Monkey test description")
//JUnit annotations
@Test
@Tag(Status.REVIEW)
void MonkeyTest_fill_form_test() throws IOException {
GremlinsHelper gremlinsHelper = new GremlinsHelper();
GremlinsHelper.setupGremlinsScript(getPage());
GremlinsHelper.startGremlinHordeWithGremlinTypes(getPage(), List.of("clicker", "formFiller", "typer"));
}

@Test
@Tag(Status.REVIEW)
void MonkeyTest_test() throws IOException {
GremlinsHelper gremlinsHelper = new GremlinsHelper();
GremlinsHelper.setupGremlinsScript(getPage());
GremlinsHelper.startGremlinHorde(getPage());

}
}

Large diffs are not rendered by default.

0 comments on commit 0a344ef

Please sign in to comment.