简体   繁体   中英

Katalon Studios - Verify if browser launch download

Is there a way in Katalon Studios to check if the browser launch a download? And/ or if the downloaded file is in the system afterwards?

Maybe it's not possible because it gets out of the browser's scope.

Many thanks

Here is an example of how to check file download from the docs :

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.testng.Assert as Assert
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
 
'Define Custom Path where file needs to be downloaded'
String downloadPath = 'D:\\FileDownloadChecking'
 
'Launch a browser and Navigate to URL'
WebUI.openBrowser(GlobalVariable.FileDownloadCheckingURL)
 
WebDriver driver = DriverFactory.getWebDriver()
 
'Clicking on a Link text to download a file'
driver.findElement(By.linkText('smilechart.xls')).click()
'Wait for Some time so that file gets downloaded and Stored in user defined path'
WebUI.delay(10)
 
'Verifying the file is download in the User defined Path'
Assert.assertTrue(isFileDownloaded(downloadPath, 'smilechart.xls'), 'Failed to download Expected document')
 
boolean isFileDownloaded(String downloadPath, String fileName) {
    long timeout = 5 * 60 * 1000
    long start = new Date().getTime()
    boolean downloaded = false
    File file = new File(downloadPath, fileName)
    while (!downloaded) {
        KeywordUtil.logInfo("Checking file exists ${file.absolutePath}")
        downloaded = file.exists()
        if (downloaded) {
            file.delete() // remove this line if you want to keep the file
        } else {
            long now = new Date().getTime()
            if (now - start > timeout) {
                break
            }
            Thread.sleep(3000)
        }
    }
    return downloaded
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM