【问题标题】:How to use mockito to mock org.openqa.selenium.support.ui.Select?如何使用mockito来模拟org.openqa.selenium.support.ui.Select?
【发布时间】:2016-11-24 22:30:55
【问题描述】:

我希望能够为我的 Selenium 集成测试进行单元测试,为此我需要能够模拟 selenium 选择对象。

我测试的方法是:

protected int findOptionByIgnoreCaseText(String value, Select dropDown) {
...
}

我的测试方法是:

@Test
public void testFindOptionByIgnoreCaseText() {
    WebElement mockElement = Mockito.mock(WebElement.class,....??? "Peter","Stéphane","Katy","Brad"
    Select dropDown = new Select(mockElement);
    Assert.assertEquals("OK", 1, step.findOptionByIgnoreCaseText("Peter", dropDown);
    Assert.assertEquals("OK", 2, step.findOptionByIgnoreCaseText("Stephane", dropDown);
    Assert.assertEquals("OK", 2, step.findOptionByIgnoreCaseText("Stéphane", dropDown);
    Assert.assertEquals("OK", 3, step.findOptionByIgnoreCaseText("Katy", dropDown);
    Assert.assertEquals("OK", 4, step.findOptionByIgnoreCaseText("Brad", dropDown);
}

【问题讨论】:

    标签: java unit-testing selenium junit mockito


    【解决方案1】:

    我找到了解决办法:

    我看selenium官方文档here

    @Test
    public void testFindOptionByIgnoreCaseText() {
        final WebElement peterOption = mockOption("Peter");
        final WebElement stephaneOption = mockOption("Stephane");
        final WebElement noelOption = mockOption("Noël");
        final WebElement katyOption = mockOption("Céline");
        final WebElement bradOption = mockOption("Brad ");
        final WebElement pierreOption = mockOption(" Pierre");
        final List<WebElement> options = Arrays.asList(peterOption, stephaneOption, noelOption, katyOption, bradOption, pierreOption);
    
        final WebElement element = Mockito.mock(WebElement.class);
        Mockito.when(element.getTagName()).thenReturn("select");
        Mockito.when(element.findElements(By.tagName("option"))).thenReturn(options);
        Select select = new Select(element);
    
        Assert.assertEquals("OK", 0, step.findOptionByIgnoreCaseText("Peter", select));
    
        Assert.assertEquals("OK", 1, step.findOptionByIgnoreCaseText("Stephane", select));
        Assert.assertEquals("OK", 1, step.findOptionByIgnoreCaseText(" Stephane", select));
        Assert.assertEquals("OK", 1, step.findOptionByIgnoreCaseText("Stephane ", select));
        Assert.assertEquals("OK", 1, step.findOptionByIgnoreCaseText("Stéphane", select));
    
        Assert.assertEquals("OK", 2, step.findOptionByIgnoreCaseText("Noël", select));
        Assert.assertEquals("OK", 2, step.findOptionByIgnoreCaseText("Noel", select));
    
        Assert.assertEquals("OK", 3, step.findOptionByIgnoreCaseText("Celine", select));
        Assert.assertEquals("OK", 3, step.findOptionByIgnoreCaseText("Céline", select));
    
        Assert.assertEquals("OK", 4, step.findOptionByIgnoreCaseText("Brad", select));
        Assert.assertEquals("OK", 4, step.findOptionByIgnoreCaseText(" Brad", select));
        Assert.assertEquals("OK", 4, step.findOptionByIgnoreCaseText("Brad ", select));
        Assert.assertEquals("OK", 4, step.findOptionByIgnoreCaseText(" Brad ", select));
    
        Assert.assertEquals("OK", 5, step.findOptionByIgnoreCaseText("Pierre", select));
        Assert.assertEquals("OK", 5, step.findOptionByIgnoreCaseText(" Pierre", select));
        Assert.assertEquals("OK", 5, step.findOptionByIgnoreCaseText("Pierre ", select));
        Assert.assertEquals("OK", 5, step.findOptionByIgnoreCaseText(" Pierre ", select));
    }
    
    private WebElement mockOption(String name) {
        final WebElement option = Mockito.mock(WebElement.class, name);
        Mockito.when(option.getText()).thenReturn(name);
        return option;
    }
    

    我测试的方法是:

    protected int findOptionByIgnoreCaseText(String value, Select dropDown) {
        int index = 0;
        for (WebElement option : dropDown.getOptions()) {
            if (Normalizer.normalize(option.getText(), Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").trim()
                    .equalsIgnoreCase(Normalizer.normalize(value, Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").trim())) {
                return index;
            }
            index++;
        }
        return -1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-08
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多