在Java中使用selenium进行UI测试时,如何存储PageObject内容?
我目前正在使用页面对象设计模型进行UI测试。目前,我在我的每个页面中使用hashmap来存储内容 我之所以使用映射,是因为每当我有许多字段要填充时,我都会使用fillData(映射数据)方法来匹配键 例如,我的页面对象将具有:在Java中使用selenium进行UI测试时,如何存储PageObject内容?,java,selenium,pageobjects,Java,Selenium,Pageobjects,我目前正在使用页面对象设计模型进行UI测试。目前,我在我的每个页面中使用hashmap来存储内容 我之所以使用映射,是因为每当我有许多字段要填充时,我都会使用fillData(映射数据)方法来匹配键 例如,我的页面对象将具有: Map<String, WebElement> content = new HashMap(); content.put("backgroundColor", WebElement a); content.put("fontColor", W
Map<String, WebElement> content = new HashMap();
content.put("backgroundColor", WebElement a);
content.put("fontColor", WebElement b);
content.put("linksColor", WebElement c);
content.put("actionBarActiveColor", WebElement d);
content.put("activeColor", WebElement e);
public void fillDataFields(Map<String, String> data){
data.forEach( (k,v) -> {
content.get(k).setValue(v);
});
}
我的测试会打电话给你
brandingPage.fillDataFields(generalAppearanceFieldsData);
我遇到的问题是字符串键很难维护和验证,因为我可以调用一个不指向任何地方的键。我不确定地图是否是存储页面内容的正确方法。使用Java有更好的方法吗?应该使用PageObjects将测试代码与页面的内部工作隔离开来。您可能想考虑将方法添加到与操作和/或设置程序相对应的页对象,并从测试中调用它们。 PageObject知道如何在页面上执行操作。 测试类与PageObject交互,要求它对页面执行操作,然后断言事情按预期发生 例如:
public class Test {
private final String BLUE_HEX="0000FF";
private final String RED_HEX="FF0000";
private WebDriver driver;
@Test
public void test() {
PageObject page = new PageObject(driver);
page.setBackgroundColor(BLUE_HEX);
page.setActionBarColor(RED_HEX);
// do stuff
assertTrue(page.getSomeValue());
}
}
public class PageObject {
private WebDriver driver;
public PageObject( WebDriver driver) {
this.driver = driver;
}
private void setText(String id, String val) {
driver.findElement(By.id(id)).sendKeys(val);
}
public void setBackgroundColor(String hex) {
setText("backgroundColor", hex);
}
public void setActionBarColor(String hex) {
setText("actionBarColor", hex);
}
public boolean getSomeValue() {
// Do some checks on the values etc
return true;
}
}
public class Test {
private final String BLUE_HEX="0000FF";
private final String RED_HEX="FF0000";
private WebDriver driver;
@Test
public void test() {
PageObject page = new PageObject(driver);
page.setBackgroundColor(BLUE_HEX);
page.setActionBarColor(RED_HEX);
// do stuff
assertTrue(page.getSomeValue());
}
}
public class PageObject {
private WebDriver driver;
public PageObject( WebDriver driver) {
this.driver = driver;
}
private void setText(String id, String val) {
driver.findElement(By.id(id)).sendKeys(val);
}
public void setBackgroundColor(String hex) {
setText("backgroundColor", hex);
}
public void setActionBarColor(String hex) {
setText("actionBarColor", hex);
}
public boolean getSomeValue() {
// Do some checks on the values etc
return true;
}
}