簡體   English   中英

Serenity BDD:如何使用 Serenity + selenium 組合下載文件?

[英]Serenity BDD: How can I download a file using Serenity + selenium combination?

我是 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 文件中設置首選項嗎?

Serenity BDD 沒有下載文件的特定方式。 您將不得不使用驅動程序選項,這與您通常所做的類似。

一個關於如何在 Serenity BDD 中配置 ChromeDriver 的簡短指南 Firefox和其他瀏覽器也有類似的方法。

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>

以上將始終使用已知位置下載文件,無論您是在 Windows 或 Linux 上運行,還是在本地計算機或 Jenkins 上運行。 要進一步參數化它,您可以使用Maven Profiles

下一個問題是找到您剛剛下載的文件,以便您可以對其進行操作。 我嘗試了這里討論的解決方案,但無法讓它們在無頭模式下工作。

最終我想出了這個僅限 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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM