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

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

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

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

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

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

webdriver.capabilities.acceptInsecureCerts=true

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

fafcakar

fafcakar1#

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

// Set the path to your custom Firefox profile directory
// Also note that it is using the File.separator to ensure 
// cross-platform compatibility.
String profilePath = System.getProperty("user.dir") + File.separator + "firefoxprofile";

// Use the setProfileDirectory() method to set the directory 
// of the custom profile for the FirefoxProfile class
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setProfileDirectory(new File(profilePath));
firefoxProfile.setAcceptUntrustedCertificates(true);

// Apply the custom profile on FirefoxOptions when creating 
// the Firefox WebDriver instance
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(firefoxprofile);

// Inform Serenity to use the provided (custom) firefoxProfile
// as the default Firefox profile to ensure that it is used by
// Serenity's WebDriver instance
Serenity.useFirefoxProfile(firefoxProfile);

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

iezvtpos

iezvtpos2#

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

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

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

相关问题