我想知道是否有可能获得位于DOM中的属性的内容,并将其存储为java类中的变量(对应于按照POM方法在网站中的页面的类)使用PageFactory from Selenium (@FindBy)。然后,我需要在另一个类中使用这个变量,对应于同一网站上的另一个页面(页面对象模型)。有人可以提供一些例子和方法吗?我没有任何代码可以展示。我只是想知道这在技术上是否可行,以及如何实现。
,编辑1----按照要求,一些代码需要解释。下面是DOM
中的属性
<label id="HouseNumberVersion">HOUSE N° x </label>
例如,我找到这个元素的DOM使用PageFactory从硒和获得其内容;遵循POM方法(这就是为什么它被称为Page1)
public class Page1 {
WebDriver driver;
@FindBy (xpath="//*[@id="HouseNumberVersion"]")
WebElement houseNumber;
public Page1(WebDriver driver){
PageFactory.initElements(driver, this);
this.driver = driver;
}
public Page1(){
}
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;
}
}
现在我想在我的其他类中调用和使用字符串actual
,它对应于网站中的另一个页面。
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 Page1();
String number = p.getHouse();
//i send this string recovered from the other
// to another string
houseInput.sendKeys(number);
}
}
和我最后调用方法sendActualToInput
从Page2在我的主测试脚本如下
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..
}
}
我试过了,但我得到了错误java.lang.NullPointerException
。Eclipse控制台指的是String actual = houseNumber.getAttribute("innerHTML");
行似乎没有任何东西返回到Page2中的sendActualToInput
方法。因为如果我在这个方法sendActualToInput
中放入一个sysout,它会在控制台中打印这个sysout的内容。
是可管理的吗?
,EDIT2--------
我已经在代码中初始化了我的字段。我只是忘了在代码片段中添加它们,但现在初始化函数在代码中。问题仍然在发生。
您将获得NullPointerException
,因为您的字段未初始化。
:
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);
}
当你做Page1 p = new Page();
时,它创建了一个对象,其中所有字段都是null
。为了初始化它们,你需要使用PageFactory.initElements(...)
在你的例子中,你需要这样写:
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)
}
}
你需要相应地更新你的第二页:
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..
}
}
所以现在你初始化你的page2针对你的驱动程序,并传递相同的驱动程序给page1构造器。