You're getting NullPointerException
because your fields are not itintialized.
Here:
public void sendActualToInput(){
// i created an object to recover the string within the return method in
// Page1
Page1 p = new Page();
String number = p.getHouse();
//i send this string recovered from the other
// to another string
houseInput.sendKeys(number);
}
When you're doing Page1 p = new Page();
it creates an object where all fields are null
. In order to initialize them you need to use PageFactory.initElements(...)
So in your case you need something like:
public class Page1 {
@FindBy (xpath="//*[@id="HouseNumberVersion"]")
WebElement houseNumber;
public void assertEquals(){
String actual = houseNumber.getAttribute("innerHTML");
//some code here to assert this is equal
// to another string but not this method is not needed it's just to show that
// i can use this string in this page properly using this method
}
public String getHouse(){
String actual = houseNumber.getAttribute("innerHTML");
return actual;
}
public Page1(WebDriver driver){
PageFactory.init(driver, this)
}
}
You need to update your second page correspondingly:
public class Page2 {
WebDriver driver;
//i search for an element in this second page which is an input
@FindBy (xpath="//*[@id="HouseInput"]")
WebElement houseInput;
public Page2(WebDriver driver){
PageFactory.initElements(driver, this);
this.driver = driver;
}
public void sendActualToInput(){
// i created an object to recover the string within the return method in
// Page1
Page1 p = new Page(driver);
String number = p.getHouse();
//i send this string recovered from the other
// to another string
houseInput.sendKeys(number);
}
}
public class MyTest (){
public WebDriver driver;
// some Before Method here ..
@Test
Public void CheckHouse(){
Page2 p2Object = new Page2(driver);
p2Object.sendActualToInput();
//some others actions here..
}
}
So that now you initialize your page2 against your driver and pass the same driver to page1 constructor.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…