Skip to content

Commit

Permalink
Merge pull request #51 from jdi-testing/1.0.10page_factory
Browse files Browse the repository at this point in the history
1.0.10page factory
  • Loading branch information
RomanIovlev authored Jul 30, 2021
2 parents 5143d68 + e9c52b5 commit 6d52968
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 66 deletions.
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.0.9</version>
<version>1.0.10</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>

<dependencies>
<dependency>
<groupId>com.jdiai</groupId>
<artifactId>js-wrappers</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.jdiai</groupId>
Expand Down
2 changes: 1 addition & 1 deletion jdi-nova/src/main/java/com/jdiai/CoreElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CoreElement implements HasCore {
private JS core;

public JS core() {
return core != null ? core : initJSFunc.execute(null, null, null);
return core != null ? core : initJSFunc.apply(null, null);
}

public JS setCore(JS core) { this.core = core; return core; }
Expand Down
27 changes: 8 additions & 19 deletions jdi-nova/src/main/java/com/jdiai/JDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.epam.jdi.tools.ILogger;
import com.epam.jdi.tools.Safe;
import com.epam.jdi.tools.func.JFunc3;
import com.jdiai.annotations.UI;
import com.jdiai.asserts.Condition;
import com.jdiai.asserts.ConditionTypes;
Expand All @@ -29,6 +28,7 @@
import static com.epam.jdi.tools.JsonUtils.getDouble;
import static com.epam.jdi.tools.LinqUtils.newList;
import static com.epam.jdi.tools.PrintUtils.print;
import static com.epam.jdi.tools.ReflectionUtils.getFieldsDeep;
import static com.jdiai.LoggerTypes.CONSOLE;
import static com.jdiai.LoggerTypes.SLF4J;
import static com.jdiai.jsbuilder.GetTypes.dataType;
Expand All @@ -51,13 +51,10 @@ public class JDI {
public static ILogger logger;
public static int timeout = 10;
public static ConditionTypes conditions = new ConditionTypes();
public static JFunc3<Object, By, List<By>, JS> initJSFunc = (parent, locator, locators) -> {
public static BiFunction<By, List<By>, JS> initJSFunc = (locator, locators) -> {
if (locators != null) {
return new JSLight(JDI::driver, locators);
}
if (parent != null && locator != null) {
return new JSLight(parent, locator);
}
if (locator != null) {
return new JSLight(JDI::driver, locator);
}
Expand Down Expand Up @@ -95,7 +92,7 @@ public class JDI {
};

public static Function<Class<?>, String> GET_OBJECT_MAP = cl -> {
Field[] allFields = cl.getDeclaredFields();
List<Field> allFields = getFieldsDeep(cl);
List<String> mapList = new ArrayList<>();
for (Field field : allFields) {
String value = GET_COMPLEX_VALUE.apply(field);
Expand Down Expand Up @@ -258,25 +255,17 @@ public static void openPage(String url) {
}

public static JS $(By locator) {
return initJSFunc.execute(null, locator, null);
}

public static JS $(Object parent, By locator) {
return initJSFunc.execute(parent, locator, null);
return initJSFunc.apply(locator, null);
}

public static JS $(By... locators) {
return initJSFunc.execute(null, null, newList(locators));
return initJSFunc.apply(null, newList(locators));
}

public static JS $(String locator) {
return $(NAME_TO_LOCATOR.apply(locator));
}

public static JS $(Object parent, String locator) {
return $(parent, NAME_TO_LOCATOR.apply(locator));
}

public static JS $(String... locators) {
return $(locatorsToBy(locators));
}
Expand All @@ -286,23 +275,23 @@ public static void loginAs(String formLocator, Object user) {
}

public static void loginAs(Object user) {
initJSFunc.execute(null, null, null).loginAs(user);
initJSFunc.apply(null, null).loginAs(user);
}

public static void submitForm(String formLocator, Object user) {
$(formLocator).submit(user);
}

public static void submitForm(Object user) {
initJSFunc.execute(null, null, null).submit(user);
initJSFunc.apply(null, null).submit(user);
}

public static void fillFormWith(String formLocator, Object user) {
$(formLocator).fill(user);
}

public static void fillFormWith(Object user) {
initJSFunc.execute(null, null, null).fill(user);
initJSFunc.apply(null, null).fill(user);
}

public static DragAndDrop drag(JS dragElement) { return new DragAndDrop(dragElement);}
Expand Down
1 change: 1 addition & 0 deletions jdi-nova/src/main/java/com/jdiai/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public interface JS extends WebElement, HasLocators, HasParent, HasCore {
List<String> validateRelations();
Point getCenter();
String textType();
JS setParent(Object parent);

default String getFullName() {
if (parent() == null) {
Expand Down
27 changes: 20 additions & 7 deletions jdi-nova/src/main/java/com/jdiai/JSLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import com.jdiai.jsproducer.Json;
import com.jdiai.jswraper.JSEngine;
import com.jdiai.scripts.Whammy;
import com.jdiai.tools.*;
import com.jdiai.tools.ClientRect;
import com.jdiai.tools.GetTextTypes;
import com.jdiai.tools.JSImages;
import com.jdiai.tools.Keyboard;
import com.jdiai.visual.Direction;
import com.jdiai.visual.ImageTypes;
import com.jdiai.visual.OfElement;
Expand Down Expand Up @@ -42,6 +45,7 @@
import static com.jdiai.jswraper.JSWrappersUtils.defineLocator;
import static com.jdiai.tools.FilterConditions.textEquals;
import static com.jdiai.tools.GetTextTypes.INNER_TEXT;
import static com.jdiai.tools.JSUtils.getLocators;
import static com.jdiai.tools.Keyboard.pasteText;
import static com.jdiai.tools.VisualSettings.*;
import static com.jdiai.visual.Direction.VECTOR_SIMILARITY;
Expand Down Expand Up @@ -84,13 +88,15 @@ public JSLight(WebDriver driver, By... locators) {
this(() -> driver, locators);
}
public JSLight(Object parent, By locator) {
this(JDI::driver, locator, parent);
this(JDI::driver, locator);
setParent(parent);
}
public JSLight(WebDriver driver, By locator, Object parent) {
this(() -> driver, locator, parent);
this(() -> driver, locator);
setParent(parent);
}
public JSLight(Supplier<WebDriver> driver, By locator, Object parent) {
this(driver, JSUtils.getLocators(locator, parent));
this(driver, getLocators(locator, parent));
this.parent = parent;
if (parent != null && isInterface(parent.getClass(), HasCore.class)) {
this.engine().updateDriver(((HasCore) parent).core().engine().jsDriver());
Expand Down Expand Up @@ -243,7 +249,13 @@ public Object parent() {
}

public JS setParent(Object parent) {
List<By> locators = getLocators(parent);
locators.addAll(locators());
setLocators(locators);
this.parent = parent;
if (parent != null && isInterface(parent.getClass(), HasCore.class)) {
this.engine().updateDriver(((HasCore) parent).core().engine().jsDriver());
}
return this;
}

Expand Down Expand Up @@ -275,7 +287,7 @@ public JS select(String value) {
? new ArrayList<>()
: locators().subList(0, locators().size() - 2);
locators.add(fillByTemplate(lastLocator, value));
initJSFunc.execute(null, null, locators).click();
initJSFunc.apply(null, locators).click();
} else {
findFirst(textEquals(value)).click();
}
Expand Down Expand Up @@ -767,7 +779,8 @@ public JS find(String by) {
}

public JS find(By by) {
return initJSFunc.execute(this, by, null);
return initJSFunc.apply(by, null)
.setParent(this);
}

public JS children() {
Expand Down Expand Up @@ -921,7 +934,7 @@ public long indexOf(Function<JS, String> condition) {
}

protected JS listToOne(String script) {
JS result = initJSFunc.execute(null, null, null);
JS result = initJSFunc.apply(null, null);
result.engine().jsDriver().setScriptInElementContext(engine().jsDriver(), script);
engine().jsDriver().builder().cleanup();
return result;
Expand Down
6 changes: 5 additions & 1 deletion jdi-nova/src/main/java/com/jdiai/asserts/DisplayedTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

public class DisplayedTypes {
public static final String isExist = "element !== null";

public static final String isDisplayed =
"element !== null && styles.visibility === 'visible' && styles.display !== 'none' " +
"&& !element.hasAttribute('hidden')";

public static final String isEnabled = isDisplayed + " && element.hasAttribute('enabled')";

// onmousemove = function(e){ console.log("mouse location:", e.clientX, e.clientY); }
public static final String isVisible =
"if (!element) { return false; }\n" +
"const rect = element.getBoundingClientRect();\n" +
"if (!styles || !rect || styles.visibility !== 'visible' || styles.display === 'none' || " +
"element.hasAttribute('hidden') || styles.opacity < 0.05 || rect.width < 3 || rect.height < 3) {\n" +
" return false;\n" +
"}\n" +
"element.scrollIntoView({behavior:'auto',block:'center',inline:'center'})\n" +
"element.scrollIntoView({behavior:'auto',block:'center',inline:'center'});\n" +
"cx = rect.left + rect.width / 2;\n" +
"cy = rect.top + rect.height / 2;\n" +
"e = document.elementFromPoint(cx, cy);\n" +
Expand Down
25 changes: 21 additions & 4 deletions jdi-nova/src/main/java/com/jdiai/page/objects/JDIPageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.function.Function;

Expand All @@ -19,7 +20,7 @@
public class JDIPageFactory {
public static boolean useSmartLocatorsWithoutUI = false;

public static Function<Field, By> LOCATOR_FROM_FIELD = field -> {
public static By defaultLocatorFromField(Field field) {
if (field.isAnnotationPresent(FindBy.class)) {
FindBy findBy = field.getAnnotation(FindBy.class);
return findByToBy(findBy);
Expand All @@ -32,8 +33,24 @@ public class JDIPageFactory {
}
return locator;
}
UI classUI = getClassAnnotation(field.getType(), UI.class);
if (classUI != null) {
return uiToBy(classUI);
}
return useSmartLocatorsWithoutUI && !isClass(field, Section.class) && (isInterface(field, HasCore.class) || isInterface(field, WebElement.class))
? SMART_LOCATOR.apply(field.getName())
: null;
};
? SMART_LOCATOR.apply(field.getName())
: null;
}

static <T extends Annotation> T getClassAnnotation(Class<?> cl, Class<T> annotation) {
while (cl != null && cl != Object.class) {
if (cl.isAnnotationPresent(annotation)) {
return cl.getAnnotation(annotation);
}
cl = cl.getSuperclass();
}
return null;
}

public static Function<Field, By> LOCATOR_FROM_FIELD = JDIPageFactory::defaultLocatorFromField;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static PagesFactory getFactory() {
|| pageFactory.isUIElementField.apply(f)
);
pageFactory.createRules = map(
$("WebElement", cRule(WebElement.class, cl -> initJSFunc.execute(null, null, null))),
$("WebElement", cRule(WebElement.class, cl -> initJSFunc.apply(null, null))),
$("List", cRule(List.class, cl -> new DataList<>()))
);
pageFactory.setupRules = new MapArray<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.jdiai.JDI.initJSFunc;
import static com.jdiai.page.objects.JDIPageFactory.LOCATOR_FROM_FIELD;
import static com.jdiai.page.objects.PageFactory.getFactory;
import static com.jdiai.tools.JSUtils.getLocators;
import static java.lang.String.format;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

Expand Down Expand Up @@ -49,12 +48,8 @@ public static String getPageTitle(Class<?> cl, Field field) {
}
static void setupCoreElement(InitInfo info) {
By locator = LOCATOR_FROM_FIELD.apply(info.field);
JS core;
List<By> locators = getLocators(info.parent);
if (locator != null) {
locators.add(locator);
}
core = initJSFunc.execute(null, null, locators);
JS core = initJSFunc.apply(locator, null);
core.setParent(info.parent);
((HasCore) info.instance).setCore(core);
}
static boolean isUIObject(Field field) {
Expand Down
30 changes: 20 additions & 10 deletions jdi-nova/src/main/java/com/jdiai/tools/JSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,36 @@ public static By uiToBy(UI locator) {
if (!locator.value().isEmpty()) {
return NAME_TO_LOCATOR.apply(locator.value());
}
if (!locator.id().isEmpty())
if (!locator.id().isEmpty()) {
return By.id(locator.id());
if (!locator.clazz().isEmpty())
}
if (!locator.clazz().isEmpty()) {
return By.className(locator.clazz());
if (!locator.xpath().isEmpty())
}
if (!locator.xpath().isEmpty()) {
return By.xpath(locator.xpath());
if (!locator.css().isEmpty())
}
if (!locator.css().isEmpty()) {
return By.cssSelector(locator.css());
if (!locator.text().isEmpty())
}
if (!locator.text().isEmpty()) {
return By.xpath(format(".//*/text()[normalize-space(.) = %s]/parent::*", escape(locator.text())));
if (!locator.hasText().isEmpty())
}
if (!locator.hasText().isEmpty()) {
return By.xpath(format(".//*/text()[contains(normalize-space(.), %s)]/parent::*", escape(locator.hasText())));
if (!locator.tag().isEmpty())
}
if (!locator.tag().isEmpty()) {
return By.tagName(locator.tag());
if (!locator.label().isEmpty())
}
if (!locator.label().isEmpty()) {
return By.cssSelector("[label=" + locator.label() + "]");
if (!locator.alt().isEmpty())
}
if (!locator.alt().isEmpty()) {
return By.cssSelector("[alt=" + locator.alt() + "]");
if (!locator.hasValue().isEmpty())
}
if (!locator.hasValue().isEmpty()) {
return By.cssSelector("[value=" + locator.hasValue() + "]");
}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion js-driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>js-driver</artifactId>
<name>JDI JS Driver</name>
<groupId>com.jdiai</groupId>
<version>1.0.9</version>
<version>1.0.10</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions js-wrappers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<artifactId>js-wrappers</artifactId>
<name>JDI JS Wrappers</name>
<groupId>com.jdiai</groupId>
<version>1.0.9</version>
<version>1.0.10</version>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>

<dependencies>
<dependency>
<groupId>com.jdiai</groupId>
<artifactId>js-driver</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.epam.jdi.tools</groupId>
<artifactId>jdi-nova-framework</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
<name>JS WebDriver</name>
<packaging>pom</packaging>

Expand Down
Loading

0 comments on commit 6d52968

Please sign in to comment.