Calling a page factory webelement, is giving error.

4,893 views
Skip to first unread message

Pushpraj Singh

unread,
Apr 26, 2013, 2:34:53 AM4/26/13
to webd...@googlegroups.com

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...


package tests;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Base {

              public static WebDriver driver = new FirefoxDriver();
              public static Base page = PageFactory.initElements(driver,Base.class);  
         
              @CacheLookup
              @FindBy(id = "user_name")
              public static WebElement name;
    @CacheLookup
    @FindBy(name = "user_email")
    public static WebElement lastNameTextbox;
}// class ends
All this inside one class which is Base Class
 
2.Another Test class
package tests;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TestPagef extends Base{
   
    @Test
    public void testpage() {
       
        driver = new FirefoxDriver();

        driver.get(baseUrl);   
       
        name.clear();
        name.sendKeys("user1");
       
    }

}
         After invoking and invoking the browser and getting the baseUrl, it simply gives error
Error that I get on running Test class 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 Test class. So why null pointer exception.

Smita Sinha

unread,
Apr 26, 2013, 3:09:57 AM4/26/13
to webd...@googlegroups.com
Pushpraj,

I do not think Page factory will work this way.

The driver that is finding the element and the driver that is calling the methods are different. Thats the reason its throwing this null pointer.

You have to instantiate the base class from the test class and pass the driver reference of test class.
Also, it is not really necessary to extend the class just for page Factory unless you have something else to do with that inheritance.

Your code should look something like this.
package tests;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Base {

              //public static WebDriver driver = new FirefoxDriver(); 

                
          
              @CacheLookup
              @FindBy(id = "user_name")
              public static WebElement name;
    @CacheLookup
    @FindBy(name = "user_email")
    public static WebElement lastNameTextbox;
              public void sendingKeys(String text) {
                name.clear();
                name.sendKeys(text);
                
               }
}// class ends 
All this inside one class which is Base Class
 
2.Another Test class 
package tests;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TestPagef extends Base{
    
    @Test
    public void testpage() {
        
        driver = new FirefoxDriver();
                  driver.get(baseUrl);  
                  public static Base_page = PageFactory.initElements(driver,Base.class);          
                  Base_page.sendingKeys("User1");        
    }

}

-Smita


--
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.
 
 

Pushpraj Singh

unread,
Apr 26, 2013, 3:20:50 AM4/26/13
to webd...@googlegroups.com
Ohhh..I didn't know that, that it doesn't work like I was expecting.
What I wanted was, to declare all WebElements at one place and use them in different classes. Plus I would short statements like;
I could use this name.sendKeys("xysz"); instead of driver.findElement(By.id("user_name")).sendKeys("user1");
So if Page Factory can't solve the problem, what do you suggest I should use ..., I want to skip too much typing, if I want to change something in my scripts.

Smita Sinha

unread,
Apr 26, 2013, 3:24:14 AM4/26/13
to webd...@googlegroups.com
Page factory will certainly solve the problem you are talking about.
You should use page factory the way it is meant to use.
Have u tried the modified code I have given? If not, please try.

Thanks,
Smita


--

Pushpraj Singh

unread,
Apr 26, 2013, 4:52:44 AM4/26/13
to webd...@googlegroups.com
I think, I understood . You have created a method inside BaseClass and calling that method within a method of test class.
But if I do this way, it would too much typing, it would be very hectic.
Suppose I have many @FindBy elements, like id, name, className, xpath...then it will become very hectic, for creating so many methods.
Please correct me if I am wrong.

Smita Sinha

unread,
Apr 26, 2013, 5:32:27 AM4/26/13
to webd...@googlegroups.com
@FindBy is used for each element on the page that you want to test.Which has to be written once
The methods are action you can perform on those elements.You may re-use the actions in various tests.

The tests to be performed can be written in the test class.

-Smita

Bill Ross

unread,
Apr 26, 2013, 12:13:37 PM4/26/13
to webd...@googlegroups.com
You are initializing an instance of the Base page, not all
instances. Especially not classes that happen to extend Base.

Bill


> public class Base {
>
> public static WebDriver driver = new FirefoxDriver();
> public static Base page =
> PageFactory.initElements(driver,Base.class); ******!!!!
>
> @CacheLookup
> @FindBy(id = "user_name")
> public static WebElement name;
>
> > @CacheLookup
> > @FindBy(name = "user_email")
> > public static WebElement lastNameTextbox;
> >
> }// class ends
>
> > All this inside one class which is Base Class
>
>
>
> > *2.Another Test class *
> >
> package tests;
> >
> > import org.openqa.selenium.firefox.FirefoxDriver;
> > import org.testng.annotations.Test;
> >
> > public class TestPagef extends Base{
> >
> > @Test
> > public void testpage() {
> >
> > driver = new FirefoxDriver();
> >
> > driver.get(baseUrl);
> >
> > name.clear();
> > name.sendKeys("user1");
> >
> > }
> >
> > }
> >
> After invoking and invoking the browser and getting the baseUrl,
> it simply gives error
>
> > Error that I get on running Test class 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 Test class. So why null pointer
> exception.
>

mmmuku...@gmail.com

unread,
Apr 27, 2016, 7:31:29 AM4/27/16
to webdriver
Hi Smita!
Im using page factory..got 2 page classes..in both of them classes I got 

WebDriver driver;

public ArHomeFactory(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver, this);
}

and @FindBy and apis to perfrom the actions.
Similary I got two test classes:
1) That perfroms the login action and directs to other page.
2) That perfrom the action on the redirected page.
I get the "null pointer exception" when I access the api in second page class.
What is it I need to do? Please help. Thanks!
Reply all
Reply to author
Forward
0 new messages