【问题标题】:How to store PageObject content when doing UI testing with selenium in Java?在 Java 中使用 selenium 进行 UI 测试时如何存储 PageObject 内容?
【发布时间】:2016-05-30 18:21:17
【问题描述】:

我目前正在使用页面对象设计模型进行 UI 测试。目前,我在每个页面中都使用 hashmap 来存储内容。

我使用映射的原因是每当我有很多字段要填充时,我都会使用 fillData(映射数据)方法来匹配键。

例如,我的页面对象将具有:

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);
        });
    }

我的页面测试会:

generalAppearanceFieldsData = new HashMap();

        generalAppearanceFieldsData.put("backgroundColor", BLUE_HEX);
        generalAppearanceFieldsData.put("fontColor", ORANGE_HEX);
        generalAppearanceFieldsData.put("linksColor", PURPLE_HEX);
        generalAppearanceFieldsData.put("actionBarColor", RED_HEX);
        generalAppearanceFieldsData.put("actionBarActiveColor", ORANGE_HEX);
        generalAppearanceFieldsData.put("activeColor", GREEN_HEX);

我的测试会调用

brandingPage.fillDataFields(generalAppearanceFieldsData);

我遇到的问题是字符串键很难维护和验证,因为我可以调用一个指向无处的键。我不确定地图是否是存储页面内容的正确方法。有没有更好的使用 Java 的方法?

【问题讨论】:

    标签: java selenium pageobjects


    【解决方案1】:

    PageObjects 应该用于将测试代码与页面的内部工作隔离开来。您可能需要考虑向您的页面对象添加与操作和/或设置器相对应的方法,并从您的测试中调用它们。

    PageObject 知道如何在页面上做事。 Test 类与 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 2021-05-29
      相关资源
      最近更新 更多