Skip to content

Commit

Permalink
[#5] Added possibility to use actions in DataObjects - warning: must …
Browse files Browse the repository at this point in the history
…add validations and decide how to handle return types
  • Loading branch information
tobiasstamann committed Nov 8, 2024
1 parent b30459a commit c27a6e7
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.toolisticon.pogen4selenium.runtime;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
* These are the locators which should be used by actions, that are both available in {@link DataObjectParentImpl} and {@link PageObjectParentImpl}.
*/
public interface CommonByLocators {

WebElement waitForElementToBeInteractable(By by);

WebElement waitForElementToBePresent(By by);

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package io.toolisticon.pogen4selenium.runtime;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import io.toolisticon.pogen4selenium.api._By;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;

public class DataObjectParentImpl {
public class DataObjectParentImpl implements CommonByLocators{

protected final WebDriver driver;
private final WebElement relativeParentWebElement;

protected DataObjectParentImpl(WebElement relativeParentWebElement) {
protected DataObjectParentImpl(WebDriver driver, WebElement relativeParentWebElement) {
this.driver = driver;
this.relativeParentWebElement = relativeParentWebElement;
}

public WebDriver getDriver() {
return this.driver;
}

protected WebElement getRelativeParentWebElement() {
return this.relativeParentWebElement;
}
Expand Down Expand Up @@ -69,4 +85,33 @@ protected String getValue(_By by, String locatorString, ExtractDataValue.Kind ki
return null;
}


@Override
public WebElement waitForElementToBeInteractable(By by) {
Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(300))
.ignoring(ElementNotInteractableException.class);

return wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(relativeParentWebElement, by));
}


@Override
public WebElement waitForElementToBePresent(By by) {
Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(300))
.ignoring(NoSuchElementException.class);

return wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(relativeParentWebElement, by));

}

public void pause(Duration duration) {
new Actions(driver).pause(duration).perform();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.toolisticon.pogen4selenium.api.PageObjectParent;

@SuppressWarnings("unchecked")
public abstract class PageObjectParentImpl <PAGEOBJECT extends PageObjectParent<PAGEOBJECT>> implements PageObjectParent<PAGEOBJECT>{
public abstract class PageObjectParentImpl <PAGEOBJECT extends PageObjectParent<PAGEOBJECT>> implements PageObjectParent<PAGEOBJECT>, CommonByLocators{

protected WebDriver driver;

Expand Down Expand Up @@ -92,7 +92,8 @@ protected void waitForElementToBeInteractable(String xpath) {
waitForElementToBeInteractable(By.xpath(xpath));
}

protected WebElement waitForElementToBeInteractable(By by) {
@Override
public WebElement waitForElementToBeInteractable(By by) {
Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
Expand Down Expand Up @@ -120,7 +121,8 @@ protected void waitForElementToBePresent(String xpath) {
waitForElementToBePresent(By.xpath(xpath));
}

protected WebElement waitForElementToBePresent(By by) {
@Override
public WebElement waitForElementToBePresent(By by) {
Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.toolisticon.pogen4selenium.example.googleseach;

import io.toolisticon.pogen4selenium.api.PageObject;
import io.toolisticon.pogen4selenium.api.PageObjectParent;

@PageObject
public interface ExternalPage extends PageObjectParent<ExternalPage>{

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.toolisticon.pogen4selenium.example.googleseach;

import io.toolisticon.pogen4selenium.api.ActionClick;
import io.toolisticon.pogen4selenium.api.DataObject;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.ExtractDataValue.Kind;
Expand All @@ -17,4 +18,7 @@ public interface GoogleSearchResult {
@ExtractDataValue(by = _By.XPATH, value = ".//a[@jsname='UWckNb']", kind = Kind.ATTRIBUTE, name="href")
String getLink();

@ActionClick(by = _By.XPATH, value = ".//a[@jsname='UWckNb']")
ExternalPage clickLink();

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public void testGoogleSearch() {
MatcherAssert.assertThat(searchResults.get(0).getLink(), Matchers.is("https://github.com/toolisticon/aptk"));

})
.execute(e -> {
return e.getSearchResults().get(0).clickLink();
})
.doAssertions(e -> MatcherAssert.assertThat(e.getDriver().getCurrentUrl(), Matchers.is("https://github.com/toolisticon/aptk")))

;

Expand Down
4 changes: 2 additions & 2 deletions pogen4selenium-processor/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>pogen4selenium</artifactId>
<groupId>io.toolisticon.pogen4selenium</groupId>
<version>0.5.1-SNAPSHOT</version>
<version>0.6.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pogen4selenium-processor</artifactId>
Expand Down Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
<version>4.26.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject;
package io.toolisticon.pogen4selenium.processor.common;

import java.time.Duration;
import java.util.HashSet;
Expand All @@ -7,18 +7,16 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import org.openqa.selenium.interactions.Actions;

import io.toolisticon.aptk.tools.wrapper.ElementWrapper;
import io.toolisticon.aptk.tools.wrapper.ExecutableElementWrapper;
import io.toolisticon.aptk.tools.wrapper.TypeElementWrapper;
import io.toolisticon.aptk.tools.wrapper.VariableElementWrapper;
import io.toolisticon.pogen4selenium.api.Action;
import io.toolisticon.pogen4selenium.api.PageObject;
import io.toolisticon.pogen4selenium.processor.pageobject.actions.ActionWrapper;
import io.toolisticon.pogen4selenium.processor.common.actions.ActionHelper;
import io.toolisticon.pogen4selenium.processor.common.actions.ActionWrapper;
import io.toolisticon.pogen4selenium.processor.pageobject.ExtractDataValueWrapper;
import io.toolisticon.pogen4selenium.processor.pageobject.ExtractDataWrapper;
import io.toolisticon.pogen4selenium.processor.pageobject.PauseWrapper;

public class MethodsToImplementHelper {

Expand Down Expand Up @@ -58,28 +56,9 @@ public Set<String> getImports() {
}

public List<ActionWrapper> getActions() {

// Must get annotations by Meta annotation Action
List<ActionWrapper> actions = getActionsForElement(this.executableElementWrapper);

List<VariableElementWrapper> annotatedParameters = this.executableElementWrapper.getParameters();

for (VariableElementWrapper annotatedParameter : annotatedParameters) {
actions.addAll(getActionsForElement(annotatedParameter));
}

return actions;
return ActionHelper.getActions(executableElementWrapper);
}

private static List<ActionWrapper> getActionsForElement(ElementWrapper<? extends Element> element) {
return element.getAnnotations().stream()
.filter(e -> e.asElement().hasAnnotation(Action.class))
.<ActionWrapper>map(e -> {

return new ActionWrapper(TypeElementWrapper.wrap((TypeElement)(e.getAnnotationType().asElement())).getQualifiedName(), element.unwrap());
})
.collect(Collectors.toList());
}

public Optional<ExtractDataWrapper> getExtractData() {
return Optional.ofNullable(ExtractDataWrapper.wrap(this.executableElementWrapper.unwrap()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.Collections;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.List;
import java.util.stream.Collectors;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import io.toolisticon.aptk.tools.wrapper.ElementWrapper;
import io.toolisticon.aptk.tools.wrapper.ExecutableElementWrapper;
import io.toolisticon.aptk.tools.wrapper.TypeElementWrapper;
import io.toolisticon.aptk.tools.wrapper.VariableElementWrapper;
import io.toolisticon.pogen4selenium.api.Action;

public class ActionHelper {

private ActionHelper() {

}

public static boolean hasActions(ExecutableElementWrapper executableElementWrapper) {
return !getActions(executableElementWrapper).isEmpty();
}

public static List<ActionWrapper> getActions(ExecutableElementWrapper executableElementWrapper) {

// Must get annotations by Meta annotation Action
List<ActionWrapper> actions = getActionsForElement(executableElementWrapper);

List<VariableElementWrapper> annotatedParameters = executableElementWrapper.getParameters();

for (VariableElementWrapper annotatedParameter : annotatedParameters) {
actions.addAll(getActionsForElement(annotatedParameter));
}

return actions;
}

private static List<ActionWrapper> getActionsForElement(ElementWrapper<? extends Element> element) {
return element.getAnnotations().stream()
.filter(e -> e.asElement().hasAnnotation(Action.class))
.<ActionWrapper>map(e -> {

return new ActionWrapper(TypeElementWrapper.wrap((TypeElement)(e.getAnnotationType().asElement())).getQualifiedName(), element.unwrap());
})
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.Collections;
import java.util.Set;
Expand All @@ -7,7 +7,6 @@

import io.toolisticon.pogen4selenium.api.ActionMoveToAndClick;
import io.toolisticon.pogen4selenium.api._By;
import io.toolisticon.pogen4selenium.processor.pageobject.ActionClickWrapper;
import io.toolisticon.pogen4selenium.processor.pageobject.ActionMoveToAndClickWrapper;
import io.toolisticon.spiap.api.SpiService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.Collections;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.pogen4selenium.processor.pageobject.actions;
package io.toolisticon.pogen4selenium.processor.common.actions;

import java.util.Collections;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.toolisticon.aptk.tools.wrapper.ElementWrapper;
import io.toolisticon.aptk.tools.wrapper.TypeElementWrapper;
import io.toolisticon.pogen4selenium.api.DataObject;
import io.toolisticon.pogen4selenium.processor.common.MethodsToImplementHelper;
import io.toolisticon.pogen4selenium.processor.common.actions.ActionHelper;
import io.toolisticon.pogen4selenium.runtime.DataObjectParentImpl;
import io.toolisticon.spiap.api.SpiService;

Expand Down Expand Up @@ -130,6 +132,8 @@ private void createClass(TypeElementWrapper wrappedTypeElement, DataObjectWrappe
e.getEnclosingElement().get()
).getQualifiedName().equals(DataObjectParentImpl.class.getCanonicalName()))
)
// just keep methods with action annotations
.filter(e -> ActionHelper.hasActions(e))
.map(MethodsToImplementHelper::new)
.collect(Collectors.toSet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public static List<ExtractDataValueWrapper> value(DataObjectWrapper dataObjectWr

}



}

This file was deleted.

Loading

0 comments on commit c27a6e7

Please sign in to comment.