Skip to content

Commit

Permalink
Added reading of single value via annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Oct 21, 2024
1 parent 92581cb commit 564868d
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public interface TestPagePageObject extends PageObjectParent<TestPagePageObject>
static final String COUNTER_INCREMENT_BUTTON_ID = "counterIncrementButton";

@ActionMoveToAndClick(COUNTER_INCREMENT_BUTTON_ID)
@Pause(value = 500L)
TestPagePageObject clickCounterIncrementButton();


Expand All @@ -102,11 +103,14 @@ public interface TestPagePageObject extends PageObjectParent<TestPagePageObject>
@ExtractData(by = io.toolisticon.pogen4selenium.api.By.XPATH, value = DATA_EXTRACTION_FROM_TABLE_XPATH)
TestPageTableEntry getFirstTableEntry();

default String getCounter() {
@ExtractDataValue(by = By.XPATH, value="//fieldset[@name='counter']/span[@id='counter']")
String getCounter();

// you can always provide your own methods and logic
default String providedGetCounter() {
return getDriver().findElement(org.openqa.selenium.By.xpath("//fieldset[@name='counter']/span[@id='counter']")).getText();
}


// Custom entry point for starting your tests
public static TestPagePageObject init(WebDriver driver) {
driver.get("http://localhost:9090/start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public enum By {
ID("id"),
XPATH("xpath");
XPATH("xpath"),
/** ELEMENT MUST ONLY BE USED TO EXTRACT DATA IN PAGE OBJECTS */
ELEMENT("");

private final String correspondingByMethodName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@
String name() default "";

enum Kind{
TEXT,
ATTRIBUTE,
CSS_VALUE,
TAG_NAME,
ACCESSIBLE_NAME;
TEXT("getText"),
ATTRIBUTE("getAttribute"),
CSS_VALUE("getCssValue"),
TAG_NAME("getTagName"),
ACCESSIBLE_NAME("getAccessibleName");

private final String elementMethodName;

private Kind(String elementMethodName) {
this.elementMethodName = elementMethodName;
}

public String getElementMethodName() {
return this.elementMethodName;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,51 @@
import org.openqa.selenium.WebDriver;

import io.toolisticon.pogen4selenium.api.ActionMoveToAndClick;
import io.toolisticon.pogen4selenium.api.ActionWrite;
import io.toolisticon.pogen4selenium.api.By;
import io.toolisticon.pogen4selenium.api.ExtractData;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.ExtractDataValue.Kind;
import io.toolisticon.pogen4selenium.api.PageObject;
import io.toolisticon.pogen4selenium.api.PageObjectElement;
import io.toolisticon.pogen4selenium.api.PageObjectParent;
import io.toolisticon.pogen4selenium.api.Pause;

@PageObject
public interface TestPagePageObject extends PageObjectParent<TestPagePageObject>{

static final String DATA_EXTRACTION_FROM_TABLE_XPATH = "//table//tr[contains(@class,'data')]";

@PageObjectElement(elementVariableName=TestPagePageObject.INPUT_FIELD_ID, by = By.ID, value="" )
static final String INPUT_FIELD_ID = "searchField";
@PageObjectElement(elementVariableName=TestPagePageObject.INPUT_FIELD_ID, by = By.ID, value="input_field" )
static final String INPUT_FIELD_ID = "inputField";

@PageObjectElement(elementVariableName=TestPagePageObject.COUNTER_INCREMENT_BUTTON_ID, by = By.XPATH, value="//fieldset[@name='counter']/input[@type='button']" )
static final String COUNTER_INCREMENT_BUTTON_ID = "counterIncrementButton";

@ActionMoveToAndClick(COUNTER_INCREMENT_BUTTON_ID)
TestPagePageObject clickCounterIncrementButton();

TestPagePageObject writeToInputField(@ActionWrite(INPUT_FIELD_ID) String value);

@ExtractDataValue(by=By.ELEMENT, value = INPUT_FIELD_ID, kind=Kind.ATTRIBUTE, name="value")
String readInputFieldValue();

@ActionMoveToAndClick(COUNTER_INCREMENT_BUTTON_ID)
@Pause(value = 500L)
TestPagePageObject clickCounterIncrementButton();

@ExtractData(by = io.toolisticon.pogen4selenium.api.By.XPATH, value = DATA_EXTRACTION_FROM_TABLE_XPATH)
List<TestPageTableEntry> getTableEntries();

@ExtractData(by = io.toolisticon.pogen4selenium.api.By.XPATH, value = DATA_EXTRACTION_FROM_TABLE_XPATH)
TestPageTableEntry getFirstTableEntry();

default String getCounter() {
@ExtractDataValue(by = By.XPATH, value="//fieldset[@name='counter']/span[@id='counter']")
String getCounter();

// you can always provide your own methods and logic
default String providedGetCounter() {
return getDriver().findElement(org.openqa.selenium.By.xpath("//fieldset[@name='counter']/span[@id='counter']")).getText();
}


// Custom entry point for starting your tests
public static TestPagePageObject init(WebDriver driver) {
driver.get("http://localhost:9090/start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,17 @@ public void incrementCounterTest() {
;
}

@Test
public void writeToAndReadFromInputField() {

TestPagePageObject.init(webDriver)
.writeToInputField("TEST!!!")
.pause(Duration.ofMillis(200L))
.doAssertions(e -> {
MatcherAssert.assertThat(e.readInputFieldValue(), Matchers.is("TEST!!!"));
});

}


}
2 changes: 1 addition & 1 deletion 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.1.0</version>
<version>0.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pogen4selenium-processor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package io.toolisticon.pogen4selenium.processor.pageobject;

import io.toolisticon.aptk.annotationwrapper.api.CustomCodeMethod;
import io.toolisticon.pogen4selenium.api.By;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;

public class ExtractDataValueWrapperExtension {

@CustomCodeMethod(ExtractDataValue.class)
public static String getFinalMethodCall(ExtractDataValueWrapper extractDataValueWrapper) {

String command = (
extractDataValueWrapper.by() == By.ELEMENT ?
extractDataValueWrapper.value() + "Element"
: "getDriver().findElement(By." + extractDataValueWrapper.by().getCorrespondingByMethodName() + "(\"" + extractDataValueWrapper.value() + "\"))"
)
+ ".";

switch (extractDataValueWrapper.kind()) {
case ATTRIBUTE:
case CSS_VALUE: {

return command + extractDataValueWrapper.kind().getElementMethodName() + "(\"" + extractDataValueWrapper.name() + "\");";
}
default: {

return command + extractDataValueWrapper.kind().getElementMethodName() + "();";
}
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public Optional<ExtractDataWrapper> getExtractData() {
return Optional.ofNullable(ExtractDataWrapper.wrap(this.executableElementWrapper.unwrap()));
}

public Optional<ExtractDataValueWrapper> getExtractDataValue() {
return Optional.ofNullable(ExtractDataValueWrapper.wrap(this.executableElementWrapper.unwrap()));
}


public String getNextImplClassName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* This package contains the seleniumap annotation processor.
*/
@AnnotationWrapper(value={PageObject.class, PageObjectElement.class, ActionClick.class, ActionMoveToAndClick.class, ActionWrite.class, ExtractData.class,Pause.class}
,bindCustomCode = {PageObjectWrapperExtension.class, ExtractDataWrapperExtension.class}
@AnnotationWrapper(value={PageObject.class, PageObjectElement.class, ActionClick.class, ActionMoveToAndClick.class, ActionWrite.class, ExtractData.class, ExtractDataValue.class ,Pause.class}
,bindCustomCode = {PageObjectWrapperExtension.class, ExtractDataWrapperExtension.class, ExtractDataValueWrapperExtension.class}
,usePublicVisibility = true)
package io.toolisticon.pogen4selenium.processor.pageobject;

Expand All @@ -11,6 +11,7 @@
import io.toolisticon.pogen4selenium.api.ActionMoveToAndClick;
import io.toolisticon.pogen4selenium.api.ActionWrite;
import io.toolisticon.pogen4selenium.api.ExtractData;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.PageObject;
import io.toolisticon.pogen4selenium.api.PageObjectElement;
import io.toolisticon.pogen4selenium.api.Pause;
Expand Down
6 changes: 3 additions & 3 deletions pogen4selenium-processor/src/main/resources/PageObject.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public class ${ toImplementHelper.implementationClassName } ${toImplementHelper.
// Move to Element and click
new Actions(getDriver()).moveToElement(${method.getElementToMoveToAndClick.get}Element).pause(300).click().build().perform();
!{/if}
!{if method.getExtractData.isPresent}
!{if method.getExtractDataValue.isPresent}
return ${method.getExtractDataValue.get.getFinalMethodCall}
!{elseif method.getExtractData.isPresent}
!{if method.getExtractData.get.isList}
return getDriver().findElements(By.${method.getExtractData.get.by.correspondingByMethodName}("${method.getExtractData.get.value}")).stream().map( ${method.getExtractData.get.extractedDataImplName}::new).collect(Collectors.toList());
!{elseif method.getExtractData.get.isString}
return
!{else}
return new ${method.getExtractData.get.extractedDataImplName}(getDriver().findElement(By.${method.getExtractData.get.by.correspondingByMethodName}("${method.getExtractData.get.value}")));
!{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.toolisticon.cute.PassIn;
import io.toolisticon.pogen4selenium.api.ActionClick;
import io.toolisticon.pogen4selenium.api.ActionWrite;
import io.toolisticon.pogen4selenium.api.By;
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
import io.toolisticon.pogen4selenium.api.PageObject;
import io.toolisticon.pogen4selenium.api.PageObjectElement;
import io.toolisticon.pogen4selenium.api.PageObjectParent;
Expand All @@ -26,6 +28,9 @@ public interface LoginPage extends PageObjectParent<LoginPage>{
LoginPage writeUserName(@ActionWrite(USERNAME_ID) String username);
LoginPage writePassword(@ActionWrite(PASSWORD_ID) String password);

@ExtractDataValue(by=By.XPATH, value="/div", kind=ExtractDataValue.Kind.ATTRIBUTE, name="href")
String getLinkHref();

@ActionClick(SUBMIT_BUTTON_ID)
LoginPage clickSubmitButton();

Expand Down

0 comments on commit 564868d

Please sign in to comment.