Using PageFactory and TestNG, calling PageFactory getting error

220 views
Skip to first unread message

Partho Dutta

unread,
Oct 3, 2016, 11:49:37 AM10/3/16
to Selenium Users
Here is my code
This the Driver class where i have initialized Chrome Driver
public class Driver {
   
   
    public static  WebDriver driver;
    }

This is my Login Class where i have accessed PageFactory elements

public class Login {
 
    //PageFactory Web elements Accessed
    LoginPage loginPage=PageFactory.initElements(Driver.driver,LoginPage.class);
    WaitLib waitLib=new WaitLib();
   
   
    public void LoginToApp(String userName, String password,String url) throws IOException
    {
        try
        {
                   
        Driver.driver.manage().window().maximize();
        Driver.driver.get(url);
         loginPage.getUsername().sendKeys(userName);
         loginPage.getPassword().sendKeys(password);
         loginPage.getLogIn().click();
          waitLib.implicitWait();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
       
    }

}

 This is my TestNG Code

public class CreateCompanyTest {
    ExcelLib excelLib;
    Login login;
   

    @BeforeClass
    public void configBeforeClass() throws Exception {

        // Object intialization
        excelLib = new ExcelLib();
        login = new Login();
        System.setProperty("webdriver.chrome.driver", "Driver/chromedriver.exe");
        Driver.driver=new ChromeDriver();
        
    }

    @BeforeMethod
    public void configBeforeMethod() throws Exception {
        
        
        
        String url = excelLib.getExcelData(1, 0, "Data1");
        System.out.println("------URL-----"+url);
        String userName = excelLib.getExcelData(1, 1, "Data1");
        System.out.println("------userName-----"+userName);
        String password = excelLib.getExcelData(1, 2, "Data1");
        System.out.println("------password-----"+password);
        login.LoginToApp(userName, password, url);
        Thread.sleep(3000);
        System.out.println("Hellol");

    }

    @Test
    public void createCompanyTest() {
        System.out.println("Hi");

    }

}

When i am running TestNG Code i am getting error like   
   java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy6.sendKeys(Unknown Source)
    at businesslogic.Login.LoginToApp(Login.java:37)
    at testcases.CreateCompanyTest.configBeforeMethod(CreateCompanyTest.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
Please help me out i have no clue.
   
   
   

 

⇜Krishnan Mahadevan⇝

unread,
Oct 4, 2016, 12:05:47 AM10/4/16
to Selenium Users
Partho,

Can you please try changing 

@BeforeClass
public void configBeforeClass() throws Exception {
    // Object intialization
    excelLib = new ExcelLib();
    login = new Login();
    System.setProperty("webdriver.chrome.driver", "Driver/chromedriver.exe");
    Driver.driver=new ChromeDriver();    
}

To

@BeforeClass
public void configBeforeClass() throws Exception {
    // Object intialization
    System.setProperty("webdriver.chrome.driver", "Driver/chromedriver.exe");    
    excelLib = new ExcelLib();
    Driver.driver=new ChromeDriver();    
    login = new Login();
}

and try again ?

Explanation : When you instantiate the Login() page it also ends up invoking the PageFactory.initElements() method [thats how you have written your code ] but with a null WebDriver object because Driver.driver is still not set a valid object.

I have just flipped the order of statements in your @BeforeClass annotated method such that first the driver gets instantiated and initialised in your Driver class and only then you instantiate the Login() object.

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/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/52947504-511d-4b02-83bb-bd1d4ed7ca49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages