Unable to initialise PageFactory for PageObject
I have updated the java-client jar and tried to initialise PageFactory as mention below.
PageObject PageObject1 = PageFactory.initElements(new AppiumFieldDecorator(driver),PageObject.class);
But I am getting type mismatch error. Is there something I am missing ???
same problem
Hi! Let see this (is taken from here https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/PageFactory.java):
* @param pageClassToProxy A class which will be initialised.
* @return An instantiated instance of the class with WebElement and List<WebElement> fields proxied
*/
public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy)
/**
* As
* {@link org.openqa.selenium.support.PageFactory#initElements(org.openqa.selenium.WebDriver, Class)}
* but will only replace the fields of an already instantiated Page Object.
*
* @param driver The driver that will be used to look up the elements
* @param page The object with WebElement and List<WebElement> fields that should be proxied.
*/
public static void initElements(WebDriver driver, Object page)
/**
* Similar to the other "initElements" methods, but takes an {@link ElementLocatorFactory} which
* is used for providing the mechanism for fniding elements. If the ElementLocatorFactory returns
* null then the field won't be decorated.
*
* @param factory The factory to use
* @param page The object to decorate the fields of
*/
public static void initElements(ElementLocatorFactory factory, Object page)
/**
* Similar to the other "initElements" methods, but takes an {@link FieldDecorator} which is used
* for decorating each of the fields.
*
* @param decorator the decorator to use
* @param page The object to decorate the fields of
*/
public static void initElements(FieldDecorator decorator, Object page)
You should istantiate your "page object" before PageFactory.initElements
PageObject po = new PageObject();
PageFactory.initElements(new AppiumFieldDecorator(driver),po)
Please pay attention: PageFactory.initElements is "void".
"You should istantiate your "page object" before " - before what?
let say in test class we have following
Abstract Page:
public abstract class Page { protected AppiumDriver driver; public Page(AppiumDriver driver) { this.driver = driver; } }
Test Page:
public class FirstPage extends Page {
@FindBy(id = "something")
private List
Test class:
private FirstPage firstPage; @Test public void doTest() { // was before: firstPage = PageFactory.initElements(driver, FirstPage.class); PageFactory.initElements(new AppiumFieldDecorator(driver), FirstPage.class); }
Ok. Let see two different examples below:
public class Test {
public static class PageObject {
private final WebDriver driver;
@FindBy(className = "android.widget.TextView")
private WebElement element;
public PageObject(WebDriver driver){
this.driver = driver;
}
public void click(){
element.click();
}
}
private PageObject po;
@Before
public void setup() throws Exception {
File appDir = new File("src/test/java/io/appium/java_client");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
/**
* Attention please
*/
po = PageFactory.initElements(new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities), PageObject.class);
}
@org.junit.Test
public void test() {
po.click();
}
@After
public void tearDown() throws Exception {
po.driver.quit();
}
}
If you use PageFactory only with @FindBy, there is nothing to change. Your example will work well without AppiumFieldDecorator.
But... Are you sure that all your browser locators are suitable for Android\iOS native\hybrid app?
/**
*
* Imagine, that we have application API Demos with the same UI
* But different locators (browser/Android)
*
*/
public class Test2 {
public static class PageObject {
private final WebDriver driver;
@FindBy(className = "someDOMClass")
@AndroidFindBy(className = "android.widget.TextView")
private WebElement element;
public PageObject(WebDriver driver){
this.driver = driver;
/**
* Attention please
*/
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
public void click(){
element.click();
}
}
private PageObject po;
@Before
public void setup() throws Exception {
File appDir = new File("src/test/java/io/appium/java_client");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
/**
* Attention please
*/
po = new PageObject(new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities));
}
@org.junit.Test
public void test() {
po.click();
}
@After
public void tearDown() throws Exception {
po.driver.quit();
}
AppiumFieldDecorator decorates Webelement-fields which marked by @AndroidFindBy/@iOSFindBy. This decorator supports @FindBy too.
thanks!
Sergey,
still not clear how to initiate pages one by one inside test class?
I can't start this with every page change. /** * Attention please */ po = PageFactory.initElements(new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities), PageObject.class);
mine example:
Abstract Page:
public abstract class Page { protected AppiumDriver driver; public Page(AppiumDriver driver) { this.driver = driver; PageFactory.initElements(new AppiumFieldDecorator(driver), this); } }
Test Page:
public class FirstPage extends Page {
@FindBy(id = "something")
private List elemList;
public FirstPage(AppiumDriver driver) {
super(driver);
}
//some staff
}
Test class:
public class testDoTests extends AppiumSetup { private FirstPage firstPage; private SecondPage secondPage; @Test public void doTest1() { firstPage = PageFactory.initElements(driver, FirstPage.class); // CRASH HERE! //do some tests on first page secondPage = PageFactory.initElements(driver, SecondPage.class); //do some tests on second page } }
Appium Setup class:
public class AppiumSetup { protected static AppiumDriver driver = null; driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); }
Please, let me see stacktrace.
java.lang.RuntimeException: java.lang.InstantiationException: com.cardola.tests.page.android.KeywordPage at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136) at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66) at com.cardola.tests.test.android.testKeywordPage.testKeywordPage(testKeywordPage.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:348) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305) at org.testng.SuiteRunner.run(SuiteRunner.java:254) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.InstantiationException: com.cardola.tests.page.android.KeywordPage at java.lang.Class.newInstance0(Class.java:342) at java.lang.Class.newInstance(Class.java:310) at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:133) ... 32 more
It looks strange....
public static abstract class AbstractPageObject {
protected final WebDriver driver;
public AbstractPageObject(WebDriver driver){
this.driver = driver;
/**
* Attention please
*/
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
}
public static class PageObject extends AbstractPageObject {
public PageObject(WebDriver driver) {
super(driver);
}
@FindBy(className = "android.widget.TextView")
private WebElement element;
public void click(){
element.click();
}
}
protected static AppiumDriver driver;
@Before
public void setup() throws Exception {
File appDir = new File("src/test/java/io/appium/java_client");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@org.junit.Test
public void test() {
PageObject po = PageFactory.initElements(driver, PageObject.class);
po.click();
}
}
This code works!
I could reproduce the similar exception on this code:
public static abstract class AbstractPageObject {
protected final WebDriver driver;
public AbstractPageObject(WebDriver driver){
this.driver = driver;
/**
* Attention please
*/
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
}
//!!!
public static abstract class PageObject extends AbstractPageObject {
public PageObject(WebDriver driver) {
super(driver);
}
@FindBy(className = "android.widget.TextView")
private WebElement element;
public void click(){
element.click();
}
}
protected static AppiumDriver driver;
@Before
public void setup() throws Exception {
File appDir = new File("src/test/java/io/appium/java_client");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@org.junit.Test
public void test() {
PageObject po = PageFactory.initElements(driver, PageObject.class);
po.click();
}
java.lang.RuntimeException: java.lang.InstantiationException at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136) at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66) at io.appium.java_client.Test2.test(Test2.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.InstantiationException at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:131) ... 26 more
How does your code fall? See listing from Selenium:
private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {
try {
try {
Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
return constructor.newInstance(driver);
} catch (NoSuchMethodException e) {
return pageClassToProxy.newInstance(); //cause
}
} catch (InstantiationException e) {
throw new RuntimeException(e); //thrown
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
so you reproduced same problem with:
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136) at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66)
Questions:
- Do you want me to try make code changes as with your succeeded example?
- or do i need wait for a fix?
Please, make sure that your target class is not abstract and has constructor like this: new PageObject(driver)
Anyway, the best way to use AppiumDecorator is (if it really need to be used, I wrote above):
po = new PageObject(some params); PageFactory.PageFactory.initElements(new AppiumFieldDecorator(driver), po);
The reason I will describe over five minutes.
So, let see this code:
* @see FindBy
* @see CacheLookup
* @param driver The driver that will be used to look up the elements
* @param pageClassToProxy A class which will be initialised.
* @return An instantiated instance of the class with WebElement and List<WebElement> fields proxied
*/
public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) {
T page = instantiatePage(driver, pageClassToProxy); (1)
initElements(driver, page); (2)
return page;
}
At (1) constructor of page object will populate fields.
You attempted to use this
public Page(AppiumDriver driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
}
If there is a field @AndroidFindBy(someLocator) //iOSFildBy(someLocator) WebElement semeElement
at (2) it turns into null because of
*
* @param driver The driver that will be used to look up the elements
* @param page The object with WebElement and List<WebElement> fields that should be proxied.
*/
public static void initElements(WebDriver driver, Object page) {
final WebDriver driverRef = driver;
initElements(new DefaultElementLocatorFactory(driverRef), page); //(!!!)
//All webelement fields which
//are not annotated by @FindBy turn into null here
//This is made by Selenium developers. It is so frustrating.
}
Still not understanding how I could use PageFactory with other classes...
Hi @lolemouine !
I am trying to understand your situation too...
It is impossible to use PageFactory with parameters:
- FieldDecorator;
- Class
The only way (using class, not instance) is:
PageFactory.initElements(driver, pageClassToProxy)
Details are described above.
Is it possible that you are using not default PageFactory (from org.openqa.selenium.support)? Your code is compiled but throws some exception. So let me see stack trace. It would be nice if I could see sources of your custom PageFactory class.
Maybe... Do you want to use AppiumPageFactory that has method like this:
AppiumPageFactory.initElements(new AppiumFieldDecorator(driver), ClassToProxy.class)
? It is possible. Do you want to use new FindBy annotations (see this: #68) and it will solve your problems? If yes, you have to make some improvement of your code anyway.
For now the legal way is (with org.openqa.selenium.support.PageFactory): (1)
Pageobject po = new PageObject(webdriver and some parameters);
PageFactory.initElements(new AppiumFieldDecorator(webdriver), po);
or (2)
public PageObject(someParams){
//staff
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
//at automated test
PageObject pageObject = new PageObject(someParams);
instead of
PageFactory.initElements(webdriver, Pageobject.class); //it is the same as (1)
//or (2) but with Selenium DefaultFieldDecorator inside
https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/pagefactory/DefaultFieldDecorator.java
PageFactory doesn't work with class or PageFactory doesn't work with Class on Appium ? This is two different issues.
This would work on google chrome from a computer and selenium :
PageFactory.initElements(Singleton.getInstance().getDriver(), LoginPage.class
Singleton.getInstance().getDriver() would be my driver...
TKs !
I have java-client 1.6.0. But it seams that eclipse doesn't recognize AppiumPageFactory neither
Because AppiumPageFactory doesn't exist. But it could be created.
PageFactory doesn't work with class or PageFactory doesn't work with Class on Appium ? This is different issues.
Ok! Page factory works with class and Webdriver instance like
PageFactory.initElements(Singleton.getInstance().getDriver(), LoginPage.class);
But it doesn't work with Class and FieldDecorator implementation (AppiumFieldDecorator) because there is no suitable method (only with an instance of the class). This method is not implemented by Selenium developers. It is sad. Please read about Selenium PageFactory class. At least I gave links to Selenium github.
But I am agree that it would be useful method. So if there is an issue it should be fixed on Selenium side. It is my opinion.
OK, and it's really impossible to add AppiumPageFactory to java-client ? Or to add it to Appium?