我正在尝试是否有一种方法可以将变量从一个页面对象传递给另一个页面对象?我在Java项目中使用Serenity BDD框架。在一个页面上,我正在创建和命名一个项目,然后转到另一个页面,在该页面中,我试图在表格中找到该项目。我遇到了一个问题,我在第二页上调用的变量是null。
每个页面都有自己的页面类,这是示例代码:
public class Page1 extends PageObject{
String itemName;
private String savedItemName;
public String getSavedItemName(){
return savedItemName;
}
public String createItemAndNameIt(){
//some action to create an item
//some action to save the item with a given name
//assigned from itemName which is fed from environment variable
savedItemName = itemName;
return itemName;
}
}
下一步是我的代码进入下一页并调用savedItemName变量。
public class Page2 extends PageObject{
Page1 page1Steps = New Page1();
String recoveredItemName;
public void searchForItem{
//perform some action to get a list of items
//calling a function that searches page for item
//which accepts element to search and string to search for
searchForElement(webElement,page1Steps.getSavedItemName());
}
}
当我调用page1Steps.getSavedItemName()时返回null。我怀疑在页面之间切换时,罪魁祸首可能是webdriver,新页面无法访问上一页中保存的值?寻找有关如何成功执行此操作的建议。
public **String** createItemAndNameIt(){
//some action to create an item
//some action to save the item with a given name
//assigned from itemName which is fed from environment variable
savedItemName = itemName;
return itemName;
}
您的setter方法没有返回类型。
同样在Page2类中,您的方法没有正确的签名和返回类型。不确定您没有得到编译错误。
[在约翰·弗格森(John Ferguson)的帮助下,可以通过以下方法轻松解决:
Serenity.setSessionVariable("customerName").to("Jim”);
String customerName = sessionVariableCalled("customerName”);
使用编剧模式时,您可以记住/回忆这些事情:
actor.remember("itemName", "someItem")
actor.recall("itemName"
)