如何使用Mockito嘲笑org.openqa.selenium.support.ui.select



我希望能够为我的硒集成测试进行单元测试,并做到这一点,我需要能够模拟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);
}

我找到了解决方案:

我在这里查看Selenium官方文档

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

相关内容

  • 没有找到相关文章

最新更新