selenium 启动浏览器时,Serenity未使用自定义Firefox配置文件

rsaldnfx  于 2023-05-29  发布在  其他
关注(0)|答案(2)|浏览(557)

bounty还有4天到期。此问题的答案有资格获得+50声望奖励。ASM正在寻找一个规范答案

我使用的是Serenity BDD,我需要启动Firefox浏览器与自定义配置文件,因为我想存储证书到该配置文件。所以,我不会有任何问题与Auth。不过,我已经添加了下面的代码来使用自定义Firefox配置文件。

  1. String filePath = System.getProperty("user.dir")+"/firefoxprofile";
  2. Log.info("Firefox profile Path:"+ filePath);
  3. File firefoxProfileFolder = new File(filePath);
  4. FirefoxProfile firefoxProfile = new FirefoxProfile(firefoxProfileFolder);
  5. firefoxProfile.setAcceptUntrustedCertificates(true);
  6. Serenity.useFirefoxProfile(firefoxProfile);
  7. Log.info("Using User profile: " + Serenity.getFirefoxProfile().getClass().getSimpleName());
  8. loginPage.open();

我在下面添加了Serenity conf文件:

  1. webdriver.capabilities.acceptInsecureCerts=true

我还创建了一个Firefox配置文件,在那里我将根目录添加到自动化存储库“firefoxprofile”文件夹中。
当我使用maven命令执行测试时。实际上,Firefox并没有使用自定义配置文件。当它启动时,我转到帮助>疑难解答>验证了与我提供的路径不匹配的配置文件路径。如何解决此问题?宁静需要使用自定义配置文件,我已经创建。

fafcakar

fafcakar1#

主要是在创建Firefox WebDriver示例时缺少用于配置浏览器的FirefoxOptions。在这种情况下,需要配置示例以使用自定义配置文件。
见以下修订:

  1. // Set the path to your custom Firefox profile directory
  2. // Also note that it is using the File.separator to ensure
  3. // cross-platform compatibility.
  4. String profilePath = System.getProperty("user.dir") + File.separator + "firefoxprofile";
  5. // Use the setProfileDirectory() method to set the directory
  6. // of the custom profile for the FirefoxProfile class
  7. FirefoxProfile firefoxProfile = new FirefoxProfile();
  8. firefoxProfile.setProfileDirectory(new File(profilePath));
  9. firefoxProfile.setAcceptUntrustedCertificates(true);
  10. // Apply the custom profile on FirefoxOptions when creating
  11. // the Firefox WebDriver instance
  12. FirefoxOptions firefoxOptions = new FirefoxOptions();
  13. firefoxOptions.setProfile(firefoxprofile);
  14. // Inform Serenity to use the provided (custom) firefoxProfile
  15. // as the default Firefox profile to ensure that it is used by
  16. // Serenity's WebDriver instance
  17. Serenity.useFirefoxProfile(firefoxProfile);

从Serenity配置文件中删除webdriver.capabilities.acceptInsecureCerts=true行,因为使用自定义配置文件不需要它。
代码中的firefoxProfile.setAcceptUntrustedCertificates(true)行已经处理了这个问题。

展开查看全部
iezvtpos

iezvtpos2#

要确保Serenity BDD使用您创建的自定义Firefox配置文件,您应该尝试以下步骤:

  • 更新配置文件:
  • 在Serenity配置文件(通常为serenity.propertiesserenity.conf)中,添加以下行:
  1. webdriver.driver = firefox
  • 修改代码以设置Firefox配置文件:
  • 在您的代码中,创建Firefox配置文件后,将系统属性“webdriver.firefox.profile”设置为配置文件的名称:
  1. String profileName = "your_profile_name"; // Replace with the name of your profile
  2. System.setProperty("webdriver.firefox.profile", profileName);
  • 使用修改后的代码启动Firefox浏览器:
  • 使用以下代码而不是Serenity.useFirefoxProfile(firefoxProfile)来启动具有自定义配置文件的浏览器:
  1. WebDriver driver = new FirefoxDriver(firefoxProfile);
  2. Serenity.setWebDriver(driver);

确保您创建的Firefox配置文件已正确配置所需的设置和证书。

展开查看全部

相关问题