【问题标题】:Serenity BDD: How can I download a file using Serenity + selenium combination?Serenity BDD:如何使用 Serenity + selenium 组合下载文件?
【发布时间】:2017-12-22 09:02:00
【问题描述】:

我是 Serenity BDD 的新手。通常我所做的是设置 Firefox 首选项。

        FirefoxProfile   profile = new FirefoxProfile();

            //Set Location to store files after downloading.
        profile.setPreference("browser.download.dir", "D:\\WebDriverDownloads");
        profile.setPreference("browser.download.folderList", 2);

        //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 

        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );

但是在 Serenity 中,我可以在哪里以及如何使用这些配置文件? 我有什么方法可以在 serenity.properties 文件中设置首选项吗?

【问题讨论】:

  • 查看有关 Firefox 配置文件的 Serenity 文档:thucydides.info/docs/serenity/…。有一个部分用于配置 webdriver.firefox.profile 和 firefox.preferences。
  • 我已经阅读了这些内容。我实际上是自动化和编码的新手。所以我所做的是使用 serenity jbehave archtype,添加了一个项目。它在 src/test/resources 文件夹下有故事。在那里添加了故事,然后使用 jbehave 将这些故事映射到 java 代码。我仍然很困惑在哪里为 firefox 配置文件添加这些代码。
  • 最后通过在 pom.xml 文件中添加首选项来完成
  • 前几天不得不这样做。这个问题是谷歌的第一个热门,但没有答案。在下面找到我的解决方案。 ;)

标签: java selenium firefox serenity-bdd downloadfile


【解决方案1】:

Serenity BDD 没有下载文件的特定方法。您将不得不使用驱动程序选项,类似于您通常所做的。

A short guide on how to configure ChromeDriver in Serenity BDDFirefox 和其他浏览器也有类似的做法。

serenity.properties 中设置这些选项可能并不理想。首先,此文件中的所有内容都必须是静态的 - 您不能根据操作系统或环境使用不同的值。其次,下载路径(例如在 Chrome 中的 chrome_preferences.download.default_directory 选项)必须是全局的——你可能不知道这个值在运行时会是什么。一个潜在的解决方案是在你的 pom 中设置这些;类似:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>${failsafe.plugin.version}</version>
  <configuration>
    <includes>
      <include>features.*.*Story</include>
    </includes>
    <systemPropertyVariables>
      <chrome_preferences.download.prompt_for_download>false</chrome_preferences.download.prompt_for_download>
      <chrome_preferences.download.default_directory>${project.build.directory}/downloads</chrome_preferences.download.default_directory>
    </systemPropertyVariables>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

以上内容将始终使用已知位置下载文件,无论您是在 Window 或 Linux 上运行,还是在本地计算机上或 Jenkins 上运行。要进一步参数化,您可以使用Maven Profiles

下一个问题将是找到您刚刚下载的文件,以便您可以对它进行一些操作。我尝试了solutions discussed here,但我无法让它们在headless mode 中工作。

最终我想出了这个仅限 Chrome 的解决方案,无论您是通过 Maven 运行还是仅在 IDE 中进行调试,它都应该可以工作。 Firefox 和其他浏览器将使用不同的位置 - 下面的 downloads 位置。

public class DownloadedFile implements Question<File> {

    private static final Logger log = LoggerFactory.getLogger(DownloadedFile.class);

    private DownloadedFile() { }

    @Override
    public File answeredBy(Actor actor) {

        File downloads;
        if (System.getProperty("chrome_preferences.download.default_directory") != null)
            downloads = new File(System.getProperty("chrome_preferences.download.default_directory"));
        else if (System.getProperty("headless.mode") != null && System.getProperty("headless.mode").contentEquals("true"))
            downloads = new File(System.getProperty("user.dir"));
        else
            downloads = new File(System.getProperty("user.home") + "/Downloads");
        log.debug("searching: " + downloads.getPath());
        
        // credit: https://stackoverflow.com/a/286001/3124333
        File[] files = downloads.listFiles(File::isFile);
        File chosenFile = null;
        long lastModifiedTime = Long.MIN_VALUE;
        if (files != null) {
            for (File file : files) {
                if (file.lastModified() > lastModifiedTime) {
                    chosenFile = file;
                    lastModifiedTime = file.lastModified();
                }
            }
        }
        log.debug("found: " + chosenFile.getAbsoluteFile());

        return chosenFile;
    }

    public static Question<File> filename() {

        return new DownloadedFile();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多