Skip to content

Commit cf0e89e

Browse files
committed
Simplyfy creation of PageObject instances and fix bug with changePageObjectType
1 parent 966795a commit cf0e89e

File tree

6 files changed

+37
-47
lines changed

6 files changed

+37
-47
lines changed

pogen4selenium-api/src/main/java/io/toolisticon/pogen4selenium/api/CommonParentInterface.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.openqa.selenium.WebDriver;
66

7+
import io.toolisticon.pogen4selenium.runtime.PageObjectUtilities;
8+
79
public interface CommonParentInterface {
810

911
/**
@@ -13,36 +15,18 @@ public interface CommonParentInterface {
1315
WebDriver getDriver();
1416

1517
/**
16-
* Allows the creation of page object instances while using the fluent interface
17-
* @param <T>
18-
* @param pageObjectInterfaceType
19-
* @return
18+
* This method can be used to change the page object type.
19+
* This will be helpful if you encounter expected situations that differ from "happy path" like i.e. having a failing form validation.
20+
* @param <APO> the alternative page object type
2021
*/
21-
default <T extends PageObjectParent<T>> T getPageObjectInstance(Class<T> pageObjectInterfaceType) {
22+
default <APO extends PageObjectParent<APO>> APO changePageObjectType(Class<APO> targetPageObjectType) {
2223

23-
return getPageObjectInstance(pageObjectInterfaceType, getDriver());
24+
return PageObjectUtilities.getPageObjectInstance(targetPageObjectType, getDriver());
2425

2526
}
2627

27-
/**
28-
* Allows the creation of page object instances while using the fluent interface
29-
* @param <T>
30-
* @param pageObjectInterfaceType
31-
* @return
32-
*/
33-
@SuppressWarnings("unchecked")
34-
public static <T extends PageObjectParent<T>> T getPageObjectInstance(Class<T> pageObjectInterfaceType, WebDriver driver) {
35-
36-
try {
37-
38-
Class<?> clazz = Class.forName(pageObjectInterfaceType.getPackageName() + "." + pageObjectInterfaceType.getSimpleName() + "Impl");
39-
return (T) clazz.getConstructor(WebDriver.class).newInstance(driver);
40-
41-
} catch (ClassCastException |ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
42-
throw new IllegalArgumentException("Couldn't create instance for PageObject '" + pageObjectInterfaceType.getCanonicalName() + "'", e);
43-
}
44-
45-
}
28+
29+
4630

4731

4832

pogen4selenium-api/src/main/java/io/toolisticon/pogen4selenium/api/PageObjectParent.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,5 @@ public interface PageObjectParent<PAGEOBJECT extends PageObjectParent<PAGEOBJECT
4848
*/
4949
<OPO extends PageObjectParent<OPO>> OPO execute(ExecuteBlock<PAGEOBJECT, OPO> function);
5050

51-
/**
52-
* This method can be used to change the page object type.
53-
* This will be helpful if you encounter expected situations that differ from "happy path" like i.e. having a failing form validation.
54-
* @param <APO> the alternative page object type
55-
*/
56-
<APO extends PageObjectParent<APO>> APO changePageObjectType(Class<APO> targetPageObjectType);
51+
5752
}

pogen4selenium-api/src/main/java/io/toolisticon/pogen4selenium/runtime/PageObjectParentImpl.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.toolisticon.pogen4selenium.runtime;
22

3-
import java.lang.reflect.InvocationTargetException;
43
import java.time.Duration;
54

65
import org.openqa.selenium.By;
@@ -63,19 +62,7 @@ public <OPO extends PageObjectParent<OPO>> OPO execute(ExecuteBlock<PAGEOBJECT,O
6362

6463
return (OPO) function.execute((PAGEOBJECT)this);
6564
}
66-
67-
@Override
68-
public <APO extends PageObjectParent<APO>> APO changePageObjectType(Class<APO> targetPageObjectType) {
69-
try {
70-
return targetPageObjectType.getConstructor(WebDriver.class).newInstance(this.getDriver());
71-
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
72-
| NoSuchMethodException | SecurityException e) {
73-
throw new RuntimeException("Unable to instantate the passed page object class : " + targetPageObjectType.getCanonicalName(), e);
74-
}
75-
76-
}
77-
78-
65+
7966
protected void waitUntilUrl(String urlRegex) {
8067
PageObjectUtilities.waitForiPageToHaveMatchingUrl(driver, urlRegex);
8168
}

pogen4selenium-api/src/main/java/io/toolisticon/pogen4selenium/runtime/PageObjectUtilities.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.toolisticon.pogen4selenium.runtime;
22

3+
import java.lang.reflect.InvocationTargetException;
34
import java.time.Duration;
45

56
import org.openqa.selenium.By;
@@ -11,6 +12,8 @@
1112
import org.openqa.selenium.support.ui.FluentWait;
1213
import org.openqa.selenium.support.ui.Wait;
1314

15+
import io.toolisticon.pogen4selenium.api.PageObjectParent;
16+
1417
public class PageObjectUtilities {
1518

1619
/**
@@ -126,4 +129,25 @@ public static void doPageRefresh(WebDriver driver) {
126129
}
127130

128131

132+
/**
133+
* Allows the creation of page object instances while using the fluent interface
134+
* @param <T>
135+
* @param pageObjectInterfaceType
136+
* @return
137+
*/
138+
@SuppressWarnings("unchecked")
139+
public static <T extends PageObjectParent<T>> T getPageObjectInstance(Class<T> pageObjectInterfaceType, WebDriver driver) {
140+
141+
try {
142+
143+
Class<?> clazz = Class.forName(pageObjectInterfaceType.getPackageName() + "." + pageObjectInterfaceType.getSimpleName() + "Impl");
144+
return (T) clazz.getConstructor(WebDriver.class).newInstance(driver);
145+
146+
} catch (ClassCastException |ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
147+
throw new IllegalArgumentException("Couldn't create instance for PageObject '" + pageObjectInterfaceType.getCanonicalName() + "'", e);
148+
}
149+
150+
}
151+
152+
129153
}

pogen4selenium-processor/src/main/resources/DataToExtract.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ${ toImplementHelper.implementationClassName } extends DataObjectP
4646
${action.generateCode}
4747
!{/for}
4848
!{if method.returnsPageObject}
49-
return getPageObjectInstance(${method.getNextImplClassName}.class).pause(Duration.ofMillis(${method.afterPause}L));
49+
return changePageObjectType(${method.getNextImplClassName}.class).pause(Duration.ofMillis(${method.afterPause}L));
5050
!{else}
5151
return this;
5252
!{/if}

pogen4selenium-processor/src/main/resources/PageObject.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class ${ toImplementHelper.implementationClassName } ${toImplementHelper.
6565
!{elseif method.getExtractData.isPresent}
6666
return ${method.getExtractData.get.getFinalMethodCall}
6767
!{else}
68-
return getPageObjectInstance(${method.getNextImplClassName}.class).pause(Duration.ofMillis(${method.afterPause}L));
68+
return changePageObjectType(${method.getNextImplClassName}.class).pause(Duration.ofMillis(${method.afterPause}L));
6969
!{/if}
7070
}
7171
!{/for}

0 commit comments

Comments
 (0)