Skip to content

Commit

Permalink
[#9]Enabled Actions and Traverses to PageObjects from DataObjects. It…
Browse files Browse the repository at this point in the history
…'s also possible to self reference the DataObjects in action methods
  • Loading branch information
tobiasstamann committed Jan 22, 2025
1 parent d33748e commit a3c6f32
Show file tree
Hide file tree
Showing 28 changed files with 323 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
*/
Class<? extends ActionImpl> value();


String[] attributeNameToConstructorMapping () default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.toolisticon.pogen4selenium.api;

public @interface ActionFileUpload {

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

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
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 {

SearchContext getSearchContext();

WebElement waitForElementToBeInteractable(By by);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Expand Down Expand Up @@ -110,6 +111,13 @@ public WebElement waitForElementToBePresent(By by) {

}



@Override
public SearchContext getSearchContext() {
return this.relativeParentWebElement;
}

public void pause(Duration duration) {
new Actions(driver).pause(duration).perform();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Expand Down Expand Up @@ -155,6 +156,10 @@ public WebElement waitForElementToBePresent(ExpectedCondition<WebElement> expect
}


@Override
public SearchContext getSearchContext() {
return this.driver;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import java.util.Collection;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import io.toolisticon.pogen4selenium.runtime.LocatorCondition;

public class ActionClickImpl extends BaseAction {

public ActionClickImpl(WebDriver driver, LocatorCondition sideCondition) {
super(driver, sideCondition);
public ActionClickImpl(WebDriver driver, SearchContext searchContext, LocatorCondition sideCondition) {
super(driver, searchContext, sideCondition);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collection;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Expand All @@ -13,8 +14,8 @@
public class ActionMoveToAndClickImpl extends BaseAction {


public ActionMoveToAndClickImpl(WebDriver driver, LocatorCondition sideCondition) {
super(driver, sideCondition);
public ActionMoveToAndClickImpl(WebDriver driver, SearchContext searchContext, LocatorCondition sideCondition) {
super(driver, searchContext, sideCondition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

Expand All @@ -15,8 +16,8 @@ public class ActionWriteImpl extends BaseAction {

private final String toSet;

public ActionWriteImpl(WebDriver driver, LocatorCondition sideCondition, String toSet) {
super(driver, sideCondition);
public ActionWriteImpl(WebDriver driver, SearchContext searchContext, LocatorCondition sideCondition, String toSet) {
super(driver, searchContext, sideCondition);

this.toSet = toSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.time.Duration;
import java.util.Collection;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand All @@ -22,10 +22,12 @@ public abstract class BaseAction implements LocatorCondition, ActionImpl{


protected final WebDriver driver;
protected final SearchContext searchContext;
private final LocatorCondition sideCondition;

protected BaseAction(WebDriver driver, LocatorCondition sideCondition) {
protected BaseAction(WebDriver driver, SearchContext searchContext, LocatorCondition sideCondition) {
this.driver = driver;
this.searchContext = searchContext;
this.sideCondition = sideCondition;
}

Expand Down Expand Up @@ -57,7 +59,7 @@ public void executeAction(By locator) {
.withMessage("Locator '" + locator.toString() + "' based action '" + this.getClass().getCanonicalName() + "' with side condition '" + this.sideCondition.getClass().getCanonicalName() + "'")
.ignoreAll(getExceptionsToIgnore());

applyAction(wait.until(new WithLocatorCondition(locator)));
applyAction(wait.until(new WithLocatorCondition(this.searchContext, locator)));

}

Expand All @@ -73,7 +75,7 @@ class OnElementCondition implements ExpectedCondition<WebElement> {
private OnElementCondition(
WebElement webElement) {

this.webElement = webElement;
this.webElement = webElement;

}

Expand All @@ -90,10 +92,12 @@ public WebElement apply(WebDriver input) {

class WithLocatorCondition implements ExpectedCondition<WebElement> {

private final SearchContext searchContext;
private final By locator;

private WithLocatorCondition(By locator) {
private WithLocatorCondition(SearchContext searchContext, By locator) {

this.searchContext = searchContext;
this.locator = locator;

}
Expand All @@ -103,7 +107,7 @@ public WebElement apply(WebDriver input) {

try {

WebElement element = input.findElement(locator);
WebElement element = searchContext.findElement(locator);
return new OnElementCondition(element).apply(input);

} catch (NoSuchElementException | StaleElementReferenceException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.toolisticon.pogen4selenium.example.withoutpagefactory;

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,6 +1,8 @@
package io.toolisticon.pogen4selenium.example.withoutpagefactory;

import io.toolisticon.pogen4selenium.api._By;
import io.toolisticon.pogen4selenium.api.ActionMoveToAndClick;
import io.toolisticon.pogen4selenium.api.ActionWrite;
import io.toolisticon.pogen4selenium.api.DataObject;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.ExtractDataValue.Kind;
Expand All @@ -17,7 +19,16 @@ public interface TestPageTableEntry {
@ExtractDataValue(by = _By.XPATH, value = "./td[3]/a", kind = Kind.ATTRIBUTE, name = "href")
String link();

@ActionMoveToAndClick(by = _By.XPATH, value = "./td[3]/a")
ExternalPage clickLink();

@ExtractDataValue(by = _By.XPATH, value = "./td[3]/a", kind = Kind.TEXT)
String linkText();

@ExtractDataValue(by = _By.XPATH, value = "./td[4]/input[@name='inputField']", kind = Kind.ATTRIBUTE, name = "value")
String inputField();

TestPageTableEntry writeToInputField(@ActionWrite(by = _By.XPATH, value = "./td[4]/input[@name='inputField']") String text);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.toolisticon.pogen4selenium.example.withpagefactory;

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,6 +1,8 @@
package io.toolisticon.pogen4selenium.example.withpagefactory;

import io.toolisticon.pogen4selenium.api._By;
import io.toolisticon.pogen4selenium.api.ActionMoveToAndClick;
import io.toolisticon.pogen4selenium.api.ActionWrite;
import io.toolisticon.pogen4selenium.api.DataObject;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.ExtractDataValue.Kind;
Expand All @@ -17,7 +19,16 @@ public interface TestPageTableEntry {
@ExtractDataValue(by = _By.XPATH, value = "./td[3]/a", kind = Kind.ATTRIBUTE, name = "href")
String link();

@ActionMoveToAndClick(by = _By.XPATH, value = "./td[3]/a")
ExternalPage clickLink();

@ExtractDataValue(by = _By.XPATH, value = "./td[3]/a", kind = Kind.TEXT)
String linkText();

@ExtractDataValue(by = _By.XPATH, value = "./td[4]/input", kind = Kind.ATTRIBUTE, name = "value")
String inputField();

TestPageTableEntry writeToInputField(@ActionWrite(by = _By.XPATH, value = "./td[4]/input") String text);


}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void start() throws Exception {
server.setHandler(servletHandler);

servletHandler.addServletWithMapping(TestPage.class, "/start");
servletHandler.addServletWithMapping(LinkedPageA.class, "/linkA");
servletHandler.addServletWithMapping(LinkedPageB.class, "/linkB");


server.start();
}
Expand Down Expand Up @@ -63,6 +66,72 @@ protected void doGet(

}

public static class LinkedPageA extends HttpServlet {

private static final long serialVersionUID = 547644172712833066L;

final String content;

public LinkedPageA (){
String tmpContent = null;
try {
tmpContent = new String(getClass().getResourceAsStream("/LinkedPageA.html").readAllBytes());
} catch (IOException e) {
e.printStackTrace();
tmpContent = e.toString();
}
content = tmpContent;
}



protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(content);
}



}

public static class LinkedPageB extends HttpServlet {

private static final long serialVersionUID = 547644172712833066L;

final String content;

public LinkedPageB (){
String tmpContent = null;
try {
tmpContent = new String(getClass().getResourceAsStream("/LinkedPageB.html").readAllBytes());
} catch (IOException e) {
e.printStackTrace();
tmpContent = e.toString();
}
content = tmpContent;
}



protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(content);
}



}

public void stop() throws Exception {
server.stop();
}
Expand Down
Loading

0 comments on commit a3c6f32

Please sign in to comment.