error with pagefactory

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

error with pagefactory

Illusion0Reality
PageFactory - The code below throws - java.lang.NullPointerException.

Do I need to specifically initialize PageFactory, like shown below:
        GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);

If so, how, please let me know how to do that.


public class Test1Class
{
        @FindBy(name="q")
        private WebElement searchBox;
@Test
public void CreateDriverAndSetup()
throws Exception
{
        try{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
Thread.sleep(11);
searchBox.sendKeys("we");

Reply | Threaded
Open this post in threaded view
|

Re: error with pagefactory

softwaretestingforum
Administrator
Yes you are right. But since your class name is 'Test1Class' hence use it. Just add it before sendKeys, that is -
Illusion0Reality wrote
searchBox.sendKeys("we");

PageFactory.initElements(driver, Test1class.class);
searchBox.sendKeys("we");

It should work now.


~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: error with pagefactory

Illusion0Reality
Thanks for the reply Tarun !

I understood the issue. All the fields and methods should be in a separate class or, even in the same class, the method should be a different one.

This static method (PageFactory.initElements)  always returns the initialized class's object. With this object only we have to achieve the task.

---- this code should be in another class ----------- E.g. TestClass2

temp1 tco= PageFactory.initElements(driver, temp1.class);
temp1.searchAll("test text");
------------------------------------------------


public class temp1 {
        @FindBy(name="p")
        public WebElement searchBox;

        public void searchAll(String te)throws Exception
        {
      searchBox.sendKeys(te);
       }
}
Reply | Threaded
Open this post in threaded view
|

Re: error with pagefactory

softwaretestingforum
Administrator
This is where PageObject comes in to existence.

Your page object class would have methods like - searchAll, and your test class would use them and eventually carry out required assertion.
The only thing you need to make sure is that you use init method of PageFactory to initialize the WebElements of PageObject class before you use any method of PageObject class.

Does it make sense? Or did I confuse you?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: error with pagefactory

Illusion0Reality
It's clear, no problem at all, you did not confuse me !

However, PageObject  refers to the word mentioned by Selenium guys, but your intention might be just the page object class (the class which is purely related to a page in the application)
Reply | Threaded
Open this post in threaded view
|

Re: error with pagefactory

softwaretestingforum
Administrator
Na, I also mean same when I used word "PageObject"
~ seleniumtests.com