Skip to content

Commit

Permalink
Merge pull request #59 from jdi-testing/1.1.5jdi_events
Browse files Browse the repository at this point in the history
add jdi events
  • Loading branch information
RomanIovlev authored Oct 14, 2021
2 parents 9d146a6 + 00050a5 commit 115aaf8
Show file tree
Hide file tree
Showing 29 changed files with 462 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
if: steps.allure-setup.outcome == 'success'
timeout-minutes: 10
continue-on-error: true
run: mvn verify $WITH_PARAMS -pl $GROUP_ID:tests-jdi-nova -Drun.performance=false --fail-at-end
run: mvn verify $WITH_PARAMS -pl $GROUP_ID:tests-jdi-nova -Drun.performance=true --fail-at-end

- name: E2E Without Page objects
id: e2e-tests-no-po
Expand Down
4 changes: 2 additions & 2 deletions jdi-nova/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<artifactId>jdi-nova</artifactId>
<name>JDI Nova</name>
<groupId>com.jdiai</groupId>
<version>1.1.4</version>
<version>1.1.5</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>

<dependencies>
<dependency>
<groupId>com.jdiai</groupId>
<artifactId>js-wrappers</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.jdiai</groupId>
Expand Down
4 changes: 1 addition & 3 deletions jdi-nova/src/main/java/com/jdiai/DataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public T get(int index) {
}

public void select(String value) {
// hasLabel(value);
getLabelElement().core().select(value);
}

Expand Down Expand Up @@ -347,14 +346,13 @@ protected List<String> getAllLabels(List<T> data) {
public List<T> getList(int minAmount) {
List<T> list = new ArrayList<>();
Timer timer = new Timer();
Exception error = null;
Exception error;
do {
error = null;
try {
list = core().getEntityList(dataClass);
} catch (Exception ex) {
error = ex;
System.out.println(error.getMessage());
}
} while ((error != null || list.size() < minAmount) && timer.isRunning());
if (list.size() < minAmount) {
Expand Down
107 changes: 102 additions & 5 deletions jdi-nova/src/main/java/com/jdiai/JDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import com.jdiai.jswraper.JSEngine;
import com.jdiai.jswraper.driver.DriverManager;
import com.jdiai.jswraper.driver.DriverTypes;
import com.jdiai.jswraper.driver.JDIDriver;
import com.jdiai.tools.ILogger;
import com.jdiai.tools.Safe;
import com.jdiai.tools.StringUtils;
import com.jdiai.tools.func.JAction2;
import com.jdiai.tools.func.JAction3;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
Expand All @@ -31,10 +34,12 @@
import java.util.function.Function;
import java.util.function.Supplier;

import static com.jdiai.JDIEvents.*;
import static com.jdiai.LoggerTypes.CONSOLE;
import static com.jdiai.LoggerTypes.SLF4J;
import static com.jdiai.asserts.Conditions.above;
import static com.jdiai.asserts.Conditions.onLeftOf;
import static com.jdiai.asserts.ShouldUtils.SOFT_ASSERTION_MODE;
import static com.jdiai.asserts.ShouldUtils.waitForResult;
import static com.jdiai.jsbuilder.GetTypes.dataType;
import static com.jdiai.jsbuilder.QueryLogger.*;
Expand All @@ -43,18 +48,23 @@
import static com.jdiai.jsdriver.JSDriverUtils.getByLocator;
import static com.jdiai.jswraper.JSWrappersUtils.*;
import static com.jdiai.jswraper.driver.DriverManager.useDriver;
import static com.jdiai.jswraper.driver.JDIDriver.BROWSER_SIZE;
import static com.jdiai.jswraper.driver.JDIDriver.DRIVER_OPTIONS;
import static com.jdiai.page.objects.PageFactory.initSite;
import static com.jdiai.page.objects.PageFactoryUtils.getLocatorFromField;
import static com.jdiai.tools.Alerts.acceptAlert;
import static com.jdiai.tools.BrowserTabs.getWindowHandles;
import static com.jdiai.tools.BrowserTabs.setTabName;
import static com.jdiai.tools.JsonUtils.getDouble;
import static com.jdiai.tools.LinqUtils.any;
import static com.jdiai.tools.LinqUtils.newList;
import static com.jdiai.tools.PrintUtils.print;
import static com.jdiai.tools.ReflectionUtils.getFieldsDeep;
import static com.jdiai.tools.ReflectionUtils.isInterface;
import static com.jdiai.tools.StringUtils.format;
import static java.lang.Integer.parseInt;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class JDI {
Expand All @@ -67,6 +77,7 @@ public class JDI {
public static ILogger logger() {
return logger;
}

public static void setLogger(ILogger newLogger) {
logger = newLogger;
}
Expand All @@ -75,12 +86,85 @@ public static void setLogger(ILogger newLogger) {

public static ConditionTypes findFilters = new ConditionTypes();

public static String SELECT_FIND_TEXT_LOCATOR = ".//*[text()='%s']";
public static String browserSize = null;

public static String selectFindTextLocator = ".//*[text()='%s']";

public static Function<List<By>, JS> initJSFunc = locators -> new JSStable(JDI::driver, locators);

public static Function<List<By>, JS> initCoreFunc = initJSFunc;

public static JAction2<String, JS> beforeJSActions = (actionName, element) -> { };

public static JAction3<String, JS, Object> afterJSActions = (actionName, element, result) -> { };

public static void alwaysCloseNativeAlerts() {
registerActionListener(BEFORE_ACTION_EVENT, args -> {
acceptAlert();
});
}
public static void screenshotAfterFail() {
registerActionListener(AFTER_ACTION_FAIL_EVENT, args -> {
if (args.length > 3) {
JS element = (JS) args[2];
try {
element.highlight();
} catch (Throwable ignore) { }
try {
logger().info("Screenshot: " + makeScreenshot().getAbsolutePath());
} catch (Throwable ignore) { }
}
});
}

public static void maximizeBrowser() {
JDIDriver.maximizeBrowser(driver());
}

public static void setBrowserSize(int width, int height) {
JDIDriver.setBrowserSize(driver(), width, height);
}

public static void alwaysShowElement() {
registerActionListener(BEFORE_ACTION_EVENT, args -> {
if (args.length > 3) {
JS element = (JS) args[2];
String step = args[1].toString();
if (isInterface(element.getClass(), JS.class) && !step.contains("Show")) {
element.showIfNotInView();
}
}
});
}

public static void logNovaActions() {
registerActionListener(BEFORE_ACTION_EVENT, args -> {
if (args.length > 2) {
String step = args[1].toString();
if (isNotBlank(step)) {
logger().info(step);
} else {
logger().debug(args[0].toString());
}
}
});

registerActionListener(AFTER_SUCCESS_ACTION_EVENT, args -> {
if (args.length > 4) {
String result = args[4].toString();
if (result != null) {
String step = args[1].toString();
String logMessage = ">>> " + result;
if (isNotBlank(step)) {
logger().info(logMessage);
} else {
logger().debug(logMessage);
}
}
}
});
}

public static Function<Supplier<WebDriver>, IJSBuilder> initBuilder = driver -> {
BuilderFunctions bf = new BuilderFunctions().set(f -> {
f.oneToOne = JSOneToOne.ONE_TO_ONE;
Expand Down Expand Up @@ -230,6 +314,14 @@ public static File makeScreenshot() {
return new WebPage().setName(getTitle()).makeScreenshot();
}

public static void strictAssertions() {
SOFT_ASSERTION_MODE = false;
}

public static void softAssertions() {
SOFT_ASSERTION_MODE = true;
}

private static void init() {
if (initialized) {
return;
Expand All @@ -245,6 +337,12 @@ private static void init() {
setLogger(new ConsoleLogger(getLoggerName(CONSOLE)));
break;
}
try {
String[] split = browserSize.split("x");
BROWSER_SIZE = new Dimension(parseInt(split[0]), parseInt(split[1]));
} catch (Exception ignore) {
logger().info("Failed to setup browser size: " + browserSize);
}
initialized = true;
}

Expand Down Expand Up @@ -273,7 +371,7 @@ public static void openSite() {

public static void openSite(int width, int height) {
openSite();
driver().manage().window().setSize(new Dimension(width, height));
setBrowserSize(width, height);
}

public static void openSiteHeadless() {
Expand All @@ -282,7 +380,7 @@ public static void openSiteHeadless() {
}
public static void openSiteHeadless(int width, int height) {
openSiteHeadless();
driver().manage().window().setSize(new Dimension(width, height));
setBrowserSize(width, height);
}

public static void reopenSite() {
Expand All @@ -301,7 +399,7 @@ public static void reopenSite(Class<?> cl) {

public static void reopenSite(int width, int height) {
reopenSite();
driver().manage().window().setSize(new Dimension(width, height));
setBrowserSize(width, height);
}

public static void openSite(Class<?> cl) {
Expand Down Expand Up @@ -339,7 +437,6 @@ public static void complexLayout(HasCore[][] elements) {
}
}


public static void gridLayout(HasCore[][] elements) {
if (isEmpty(elements)) {
return;
Expand Down
43 changes: 43 additions & 0 deletions jdi-nova/src/main/java/com/jdiai/JDIEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.jdiai;

import com.jdiai.tools.func.JAction1;
import com.jdiai.tools.map.MultiMap;

import java.util.List;

public class JDIEvents {
private static final MultiMap<String, JAction1<Object[]>> LISTENERS = new MultiMap<>();

public static void registerActionListener(String actionName, JAction1<Object[]> action) {
LISTENERS.add(actionName, action);
}
public static void rewriteActionListener(String actionName, JAction1<Object[]> action) {
LISTENERS.removeByKey(actionName);
LISTENERS.add(actionName, action);
}
public static void clearAllListeners() {
LISTENERS.clear();
}
public static void clearListenersForAction(String actionName) {
LISTENERS.removeByKey(actionName);
}
public static void fireEvent(String actionName, Object... args) {
List<JAction1<Object[]>> actions = LISTENERS.getList(actionName);
for (JAction1<Object[]> action : actions) {
action.execute(args);
}
}

public static final String BEFORE_ACTION_EVENT = "beforeAction";
// Args: [actionName, step | null, element]
public static final String AFTER_ACTION_EVENT = "afterAction";
// Args: [actionName, step | null, element]
public static final String AFTER_SUCCESS_ACTION_EVENT = "afterSuccessAction";
// Args: [actionName, step | null, element, result | null]
public static final String AFTER_ACTION_FAIL_EVENT = "afterActionFail";
// Args: [actionName, step | null, element, ex | null]
public static final String SHOULD_OUT_OF_TIME_EVENT = "shouldOutOfTime";
// Args: [element, timeout, failAssertName, lastResult]
public static final String SHOULD_ASSERT_FAILED_EVENT = "shouldAssertFailed";
// Args: [element, timeout, failAssertName, lastResult]
}
27 changes: 24 additions & 3 deletions jdi-nova/src/main/java/com/jdiai/JSLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static com.jdiai.JDI.*;
import static com.jdiai.jsbuilder.GetTypes.dataType;
import static com.jdiai.jsdriver.JDINovaException.THROW_ASSERT;
import static com.jdiai.jsdriver.JSDriverUtils.*;
import static com.jdiai.jswraper.JSWrappersUtils.NAME_TO_LOCATOR;
import static com.jdiai.jswraper.JSWrappersUtils.setStringAction;
Expand Down Expand Up @@ -114,6 +115,10 @@ public void click(int timeoutWait) {
public void click() {
engine().doAction("click();");
}
public JS clickJS() {
engine().doAction("click();");
return this;
}

public JS clickCenter() {
engine().doAction("rect = element.getBoundingClientRect();" +
Expand Down Expand Up @@ -1088,8 +1093,11 @@ protected <T> T useFilter(Supplier<T> func) {
result = func.get();
} else {
jsDriver().setFilter(defaultFilter);
result = func.get();
jsDriver().setFilter(null);
try {
result = func.get();
} finally {
jsDriver().setFilter(null);
}
}
return result;
}
Expand Down Expand Up @@ -1146,11 +1154,24 @@ public WebElement rawWe() {
}
SearchContext ctx = driver();
for (By locator : locators()) {
ctx = ctx.findElement(locator);
List<WebElement> elements = ctx.findElements(locator);
ctx = getContext(elements);
}
return (WebElement) ctx;
}

private SearchContext getContext(List<WebElement> elements) {
switch (elements.size()) {
case 0:
THROW_ASSERT.accept("Failed to find element (" + this + ")");
case 1:
return elements.get(0);
default:
WebElement visible = LinqUtils.first(elements, WebElement::isDisplayed);
return visible != null ? visible : elements.get(0);
}
}

protected int elementTimeout = -1;
public int elementTimeout() {
return elementTimeout < 0 ? timeout : elementTimeout;
Expand Down
Loading

0 comments on commit 115aaf8

Please sign in to comment.