Skip to content

Commit

Permalink
Added actions for easier extension of page objects
Browse files Browse the repository at this point in the history
  • Loading branch information
seejaybee committed Mar 7, 2024
1 parent f05065d commit c33dfde
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package com.baloise.testautomation.taf.base._base;

import static com.baloise.testautomation.taf.base._base.TafAssert.assertFalse;
import static com.baloise.testautomation.taf.base._base.TafAssert.assertNotNull;
import static com.baloise.testautomation.taf.base._base.TafAssert.assertTrue;
import static com.baloise.testautomation.taf.base._base.TafAssert.fail;

import java.io.File;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baloise.testautomation.taf.base._interfaces.IAnnotations;
import com.baloise.testautomation.taf.base._interfaces.IAnnotations.Action;
import com.baloise.testautomation.taf.base._interfaces.IAnnotations.Check;
import com.baloise.testautomation.taf.base._interfaces.IAnnotations.CheckData;
import com.baloise.testautomation.taf.base._interfaces.IAnnotations.Data;
Expand All @@ -22,40 +45,24 @@
import com.baloise.testautomation.taf.base.types.TafString;
import com.baloise.testautomation.taf.base.types.TafType;
import com.baloise.testautomation.taf.common.interfaces.IFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import static com.baloise.testautomation.taf.base._base.TafAssert.assertFalse;
import static com.baloise.testautomation.taf.base._base.TafAssert.assertNotNull;
import static com.baloise.testautomation.taf.base._base.TafAssert.assertTrue;
import static com.baloise.testautomation.taf.base._base.TafAssert.fail;

public abstract class ABase implements IComponent {

public class MethodData {
Method method;
String data;
}

public static Logger logger = LoggerFactory.getLogger("TAF");

private static final String separator = "|";

protected TafString fillId = TafString.emptyString();

protected TafString checkId = TafString.emptyString();
public IComponent parent = null;

public IComponent parent = null;
public String name = "";

public Annotation by = null;

public Annotation check = null;
Expand Down Expand Up @@ -165,6 +172,34 @@ public void checkCustom() {
@Override
public void click() {}

protected void executeAction(TafString action) {
if (action.isSkip()) {
return;
}
MethodData methodData = getMethodData(action.asString());
TafAssert.assertNotNull("Action not found: " + action.asString(), methodData.method);
try {
if (methodData.data.isEmpty()) {
methodData.method.invoke(this);
}
else {
methodData.method.invoke(this, methodData.data);
}
}
catch (IllegalAccessException e) {
e.printStackTrace();
TafAssert.fail("Execution error: " + action.asString());
}
catch (IllegalArgumentException e) {
e.printStackTrace();
TafAssert.fail("Execution error: " + action.asString());
}
catch (InvocationTargetException e) {
e.printStackTrace();
TafAssert.fail("Execution error: " + action.asString() + " -> " + e.getTargetException().getMessage());
}
}

@Override
public void fill() {
if (!canFill()) {
Expand Down Expand Up @@ -209,6 +244,19 @@ private List<Field> getAllFields() {
return allFields;
}

private List<Method> getAllMethods() {
List<Method> allMethods = new ArrayList<>();
Class<?> currentClass = getClass();
while (currentClass != null) {
Method[] declaredMethods = currentClass.getDeclaredMethods();
for (Method method : declaredMethods) {
allMethods.add(method);
}
currentClass = currentClass.getSuperclass();
}
return allMethods;
}

@Override
public IFinder<?> getBrowserFinder() {
fail("method getBrowserFinder must be overridden (if it used)");
Expand Down Expand Up @@ -358,6 +406,22 @@ private Method getMethod(String prefix, Field f) {
return null;
}

private MethodData getMethodData(String action) {
List<Method> methods = getAllMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Action.class)) {
String value = method.getAnnotation(Action.class).value();
if (action.toLowerCase().startsWith(value.toLowerCase())) {
MethodData md = new MethodData();
md.method = method;
md.data = action.substring(value.length(), action.length());
return md;
}
}
}
return null;
}

private String getQualifier(String qualifierAndIdAndDetail) {
assertNotNull("qualifier/id/detail not set: " + getClass(), qualifierAndIdAndDetail);
String[] parts = qualifierAndIdAndDetail.split("\\" + separator);
Expand Down Expand Up @@ -450,6 +514,14 @@ public void initOtherFields() {
}
}

@Override
public boolean isCheckCustom() {
if (checkId == null) {
return false;
}
return checkId.isCustom();
}

private boolean isFieldRuleAnnotated(Field f) {
for (Annotation annotation : f.getAnnotations()) {
if (annotation.annotationType().getName().equals("org.junit.Rule")) {
Expand All @@ -459,14 +531,6 @@ private boolean isFieldRuleAnnotated(Field f) {
return false;
}

@Override
public boolean isCheckCustom() {
if (checkId == null) {
return false;
}
return checkId.isCustom();
}

@Override
public boolean isFillCustom() {
if (fillId == null) {
Expand Down Expand Up @@ -695,7 +759,8 @@ private void setFields(IDataRow data, List<Field> fields) {
try {
Object o = f.get(this);
if (o instanceof IType) {
assertNotNull("data not found in '" + getClass().getSimpleName() + "': " + f.getName(), data.get(f.getName()));
assertNotNull("data not found in '" + getClass().getSimpleName() + "': " + f.getName(),
data.get(f.getName()));
if (data.get(f.getName()).isSkip()) {
((IType)o).set(TafType.SKIP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public interface IAnnotations {

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface InitByTaf {}
public @interface InitByTaf {
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface By {}
public @interface By {
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
Expand Down Expand Up @@ -129,4 +131,10 @@ public enum DataProviderType {
public int value() default 0;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
public String value() default "";
}

}

0 comments on commit c33dfde

Please sign in to comment.