我是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文件中设置首选项吗?
答案 0 :(得分:0)
Serenity BDD没有下载文件的特定方法。您将不得不使用驱动程序选项,类似于您通常所做的事情。
有 A short guide on how to configure ChromeDriver in Serenity BDD 。 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>
无论您是在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();
}
}