Mockito测试流利的等待硒

时间:2018-03-30 05:39:49

标签: java selenium mockito testng

public void waitForFileDownload(int totalTimeoutInMillis, String filePath) {
    logger.info(String.format("Driver will wait for %s to be available withing %s milliseconds",
            filePath, totalTimeoutInMillis));

    String browserName = Configuration.getOption(Constants.Browser);

    if(browserName.equalsIgnoreCase(BrowserName.FIREFOX.getValue())) {
        filePath = filePath + ".part";
    }

    File fileToCheck = new File(filePath);

    FluentWait<WebDriver> wait = getWebDriverFluentWait(totalTimeoutInMillis);

    if(browserName.equalsIgnoreCase(BrowserName.FIREFOX.getValue())) {
        FluentWait<WebDriver> waitForPartFile = getWebDriverFluentWait(5000);

        try {
            waitForPartFile.until((WebDriver wd) -> (fileToCheck.exists()));
        } catch (Exception exp) {
            logger.info(String.format("Could not find %s file in 5 seconds for firefox.", filePath));
        }
        wait.until((WebDriver wd) -> (!fileToCheck.exists()));
    } else {
        wait.until((WebDriver wd) -> fileToCheck.exists());
    }
}

我有以下单元测试但是因为文件不存在而在其他方面失败。我得到timeOutException。我使用testNg。

@Test
public void waitForFileDownloadChromeTest() {
    System.setProperty("browserName","Chrome");
    Mockito.when(mockFile.getPath()).thenReturn(fileName);

    Wait<WebDriver> wait = new FluentWait<>(mockDriver)
            .withTimeout(1, TimeUnit.MILLISECONDS)
            .pollingEvery(100, TimeUnit.MILLISECONDS);

    Mockito.when(wait.until((WebDriver wd) -> mockFile.exists())).thenReturn(true);
    Mockito.verify(new WebUtils(mockDriver).waitForFileDownload(1, mockFile.getPath());

}

如何模拟wait.until((WebDriver wd) - &gt; fileToCheck.exists());

1 个答案:

答案 0 :(得分:0)

尝试将waitForFileDownload get File的代码重构为参数。

然后模拟参数:

Mockito.when(mockFile.exists()).thenReturn(true);

也许还有其他必要的方法。