Page factory object with nested class

1,234 views
Skip to first unread message

AjayB

unread,
Mar 5, 2013, 7:47:13 AM3/5/13
to webd...@googlegroups.com
I am getting an exception when trying to use page factory object with nested class

I have a page factory class  and a test case as

public class pages {

    public class loginpage {

        @FindBy(how = How.ID, id = "username")
        private WebElement login_username;
        @FindBy(how = How.NAME, name = "password")
        private WebElement login_password;
        @FindBy(name = "button")
        private WebElement login_button;

        public void login(String uname, String pass) {

            login_username.sendKeys(uname);
            login_password.sendKeys(pass);
            login_button.click();
        }
    }

    public class logout {

        @FindBy(linkText = "Logout")
        WebElement logout;

        public void logout() {
            logout.click();
        }
    }
}

@Test
public void test() {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();

        driver.get("http://www.xyz.com");

        pages.loginpage lg = PageFactory.initElements(driver, pages.loginpage.class);
        lg.login("user", "pass");
       

        pages.logout lo = PageFactory.initElements(driver, pages.logout.class);
        lo.logout();

    }

Is this the correct way?

AjayB

unread,
Mar 5, 2013, 7:54:40 AM3/5/13
to webd...@googlegroups.com
The exception thrown is


Exception in thread "main" java.lang.RuntimeException: java.lang.InstantiationException: selenium.pages$loginpage
    at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:136)
    at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:66)
    at selenium.testPage.main(testPage.java:24)
Caused by: java.lang.InstantiationException: selenium.pages$loginpage
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:133)
    ... 2 more

Krishnan Mahadevan

unread,
Mar 6, 2013, 12:41:17 AM3/6/13
to webd...@googlegroups.com
Ajay,
The problem in your case is because you ended up defining LoginPage and Logout as "inner classes" instead of defining them as "static inner classes".

Inner classes cannot be instantiated without the instance of the outer class in which they reside.

Using PageFactory you tried instantiating the inner class directly without the outer class instance viz., Page.

To fix the problem, add the "static" modifier to loginpage class as well as Logout class.

That should help you get past the InstantiationException

PageFactory uses reflection to instantiate objects.
so the following links should add more context to what am talking about:



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ajay Bachkar

unread,
Mar 6, 2013, 12:44:56 AM3/6/13
to webd...@googlegroups.com
Thank you very much.

That solved the issue.

once again thanks.

Pushpraj Singh

unread,
Apr 26, 2013, 1:07:35 AM4/26/13
to webd...@googlegroups.com
@AjayB, could you please share the solution. How creating nested helped in solving the problem.
If I am not wrong, I am experiencing the same issue. Here is what I am trying to achieve.

I have two classes ;
1. Base Class , here I have instantiated all the stuff,
example WebDriver driver = new FirefoxDriver();
Similarly I have instantiated Page Factory elements , this looks like this...
public static Define page = PageFactory.initElements(driver,Define.class);
    @CacheLookup
    @FindBy(id = "user_name")
    public static WebElement name;

    @CacheLookup
    @FindBy(name = "user_email")
    public static WebElement lastNameTextbox;
All this inside one class which BaseClass

2. CreateUser Class extends BaseClass
Test Method create{
name.clear();      =======>> doesn't work
name.sendKeys("xyz"); ===========> doesn't work
}

Error that I get on running CreateUser is :
java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:34)
I don't understand this error. I have already initialized everything in Base Class, and used it extended in CreateUser class. So why null pointer exception.

@All, Ajay , Krishnanan, Please let me know what I am missing.

Ajay Bachkar

unread,
Apr 26, 2013, 1:50:12 AM4/26/13
to webd...@googlegroups.com
Pushpraj Singh,

The problem got solved just by declaring inner classes as static class.

Looking at your code and error, the issue is different.

-AjayB





Reply all
Reply to author
Forward
0 new messages