繁体   English   中英

Selenium - 从 UI 启动下载时文件未下载到 Jenkins 目录

[英]Selenium - Files not downloading to Jenkins directory when the download is initiated from UI

希望有人能在这里帮助我,

场景:我,

  1. 导航到有下载图标的页面
  2. 点击下载图标
  3. .pdf 文件开始下载到我的项目目录

注意:下载过程中不会显示确认消息,一旦单击下载图标,文件就会被下载

观察到:在本地运行测试用例时(来自 mvn 命令和 testng)它似乎正在工作并将文件下载到我的目录。

然而,同样的情况似乎并不适用于詹金斯。 该文件不会下载到 Jenkins 目录。

本地:Windows 机器 Jenkins:Linux 浏览器:Chrome

配置的路径:

String parentDirectoryPath = System.getProperty("user.dir");

String testExecutionDownloadFilesFolderPath = parentDirectoryPath + File.separator + "TestingExecutionResult" +File.separator+"DownloadedFiles"+ File.separator;

所以,基本上文件被下载到我的项目目录中的“ DownloadedFiles ”文件夹中。

我正在打印应该发生下载的 Jenkins 路径,它似乎指向正确:

2021-07-15 08:02:13 INFO UI_PAGE - FILEPATH===> /var/jenkins_home/workspace/PROJECT_UI/TestingExecutionResult/DownloadedFiles/

同样的案例运行成功,但是文件没有下载到Jenkins目录下。

如果我在这里遗漏了什么,请指导我。

文件下载片段:

 int beforeDownload = FilesAndDirectoriesHelper.getNoOfFilesInDirectory(
            downloadPath);
    log.info("Total Files before Download: " + beforeDownload);
    JSExecutorHelper.clickOnWebElement(driver, downloadIcon);
    log.info("Clicked download icon for entry....");
    //Sleep is being used to wait for the download to complete
    TimeWaitHandling.threadSleepDelay(10000);

    int afterDownload = FilesAndDirectoriesHelper.getNoOfFilesInDirectory(
            downloadPath);
    log.info("Total Files after Download Start: " + afterDownload);

downloadPath - 上面提供的路径 (testExecutionDownloadFilesFolderPath)。

//This method is used to return the no. of files present in given folder
public static int getNoOfFilesInDirectory(String directoryFilePath) {

    try {
        File directory = new File(directoryFilePath);
        if(directory!=null) {

            File[] files = directory.listFiles();
            return files.length;
        }
    }catch (Exception e) {
        // TODO: handle exception
    }
    return 0;
}

要解决此问题,您需要在浏览器配置中设置下载路径。

在 Chrome 中:

String parentDirectoryPath = System.getProperty("user.dir");
String downloadFilepath = parentDirectoryPath+"/downloads/";
APP_LOGS.debug("Chrome Download path set to: "+downloadFilepath);
File downloadFolder = new File(downloadFilepath);
if (!downloadFolder.exists()){
      downloadFolder.mkdir();
 }
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadFilepath);
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

在 Firefox 中:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2)
profile.setPreference("browser.download.manager.showWhenStarting", False)
profile.setPreference("browser.download.dir", downloadFilepath)
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

FirefoxOptions option = new FirefoxOptions();
option.setProfile(profile);
WebDriver driver = new FirefoxDriver(option);

注意:请提供有关该问题的更多信息。 就像您使用的浏览器一样,您已经实现了逻辑。

如果 Chrome 在 Docker 容器中运行,您需要阅读 Docker 文档中的 Bind Mounts。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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