selenium - Null Pointer exception using Page object model with Cucumber -


i have used page object model design pattern along cucumber. have created 2 pages named abstractpage , loginpage on running script null pointer exception when have initialize webelements using pagefactory , please have @ below code:

abstractpage:

public class abstractpage {      protected webdriver driver;     public static loginpage lpobj;      public void openbrowsernurlhit() {         driver=new firefoxdriver();         driver.get("http://www.facebook.com");         pagefactory.initelements(driver, loginpage.class);     } } 

loginpage:

public class loginpage extends abstractpage {      @findby(name = "email")     public webelement username;      @findby(name = "pass")     public webelement password;      @findby(id = "u_0_2")     public webelement loginbuton;      public void loginintoapp() {          string url=driver.getcurrenturl();         system.out.println("the url is::::::::::::::::"+url);         username.sendkeys("testuser");         password.sendkeys("123");     }      public void clicklogn() {         loginbuton.click();     } } 

and have class stepdefination calling above methods of login class. browser opened fine , url hit when goes inside loginintoapp() method throws exception on first line itself.

public class smoketest {      @given("^open firefox , start application$")     public void open_firefox_and_start_application() throws throwable {          abstractpage obj = new abstractpage();         obj.openbrowsernurlhit();     }      @when("^i enter valid \"([^\"]*)\" , \"([^\"]*)\"$")     public void i_enter_valid_username_and_password(string arg1, string arg2) throws throwable {          loginpage lpobj = new loginpage();         lpobj.loginintoapp();     }      @then("^user should able login successfully$")     public void user_should_be_able_to_login_successfully() throws throwable {          loginpage lpobj = new loginpage();         lpobj.clicklogn();     } } 

i have testrunner class have glued stepdefination:

@runwith(cucumber.class) @cucumberoptions(         features= "features",          glue= {"stepdefination"},         plugin= {"html:target/cucumber-html-report"}         )  public class testrunner {  } 

but on running above script nullpointerexception in first line ofloginintoapp() method.i have used pagefactory initialize webelements guess "driver" variable not getting initialized in login class though have inherited asbtract class m instantiating driver due throwing null pointer.please see doing wrong here.error follows:

1 scenarios ([31m1 failed[0m) 3 steps ([31m1 failed[0m, [36m1 skipped[0m, [32m1 passed[0m) 0m18.149s

java.lang.nullpointerexception     @ pages.loginpage.loginintoapp(loginpage.java:22)     @ stepdefination.smoketest.i_enter_valid_username_and_password(smoketest.java:33)     @ ?.when enter valid "t1@gmail.com" , "pass"(myapp.feature:5) 

thanks

the simplest way solve problem make driver object static.

 protected static webdriver driver; 

as using page factory, need initiazlize page object, otherwise the webelement null

  public loginpage(){         pagefactory.initelements(driver, this);     } 

or this, don't think practice , people should avoid this.

public void loginintoapp() {

string url=driver.getcurrenturl(); system.out.println("the url is::::::::::::::::"+url); pagefactory.initelements(driver, this); username.sendkeys("testuser"); password.sendkeys("123"); 

}


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -