Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

java - Selenium Page Model - How to get an attribute, extract its content and use it in another class

I would like to know if it's possible to get the content of an attribute located in the DOM and store it as a variable in a java class (a class that corresponds to a page in a website as per the POM approach) using PageFactory from Selenium (@FindBy). Then, I would need to use this variable in another class that corresponds to another page on the same website (Page Object Model). Are there some examples and method to do someone can provide? I don't have any code to present. I was just wondering if it's technically possible and how.

----EDIT 1---- As requested, some code to explain. Below is the attribute in the DOM


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...