java - I get NullPointerException when trying to click button that exists and initialized with PageFactory.initElements() -
say, have 2 little tests within 1 class.
public class loginandlogout extends basetest { homepage kashome = new homepage(); @test(testname = "login_as") public void login() { loginpage loginkas = loginpage.open(); //open login page kashome = loginkas.login(name, pwd); } @test public void logout() { kashome.logout(); } }
homepage class:
public class homepage extends basepage { public homepage() { pagefactory.initelements(driver.get(), this); } }
basepage class:
public class basepage { @findby(xpath="//img[@title='Выход']") webelement exitbutton; @findby (xpath="//a[text()='Выход']") webelement exitlink; public basepage() { pagefactory.initelements(driver.get(), this); } public boolean isloggedin(string usr) { if (this.usernametext.gettext().startswith(usr)) return true; else return false; } public void logout() { try { exitlink.click(); alert alert = driver.get().switchto().alert(); reporter.log(alert.gettext(), true); reporter.log("Отвечаем ОК", true); alert.accept(); } catch (unhandledalertexception e) { e.printstacktrace(); reporter.log(e.getmessage(), true); } catch (exception e) { e.printstacktrace(); reporter.log(e.getmessage(), true); } } }
1st test runs ok, on second test npe when trying execute exitlink.click(), seems elements in kashome not initialized, are! there no @aftertest methods affect tests' behavior. checked button's xpath, it's ok. however, if add kashome.logout() 1st test , delete 2nd one, works ok.
why npe?
if pagefactory
doesn't process annotations on exitlink
, exitbuttons
, because aren't initialized explicitly, default null
when homepage
instance created.
i think assuming
@findby(xpath="//img[@title='Выход']") webelement exitbutton; @findby (xpath="//a[text()='Выход']") webelement exitlink;
because of @findby
annotations, have values when run tests. annotations on own don't anything. need processor read them and, through reflection, set field annotating. if pagefactory
doesn't that, fields remain null until set them yourself.
Comments
Post a Comment