Firefox 特定命令

FirefoxDriver 公開了一些特定的命令,例如用於設定環境以執行「特權」JavaScript 程式碼,或用於處理附加元件。這些現在可直接在 Nightwatch 中的 firefox 命名空間上使用。

browser.firefox

更多資訊

自訂 Firefox 設定檔

每個 Firefox WebDriver 實例都會使用匿名設定檔建立,以確保瀏覽器歷程記錄不會共用會話資料(Cookie、歷程記錄、快取、離線儲存等)。

每個 WebDriver 會話使用的設定檔可以使用 Selenium 中的 Options 類別進行設定。Nightwatch 2 完全支援使用 selenium-webdriver 程式庫建立的選項物件。

不會修改預先存在的 Firefox 設定檔;相反地,WebDriver 會建立副本進行修改。WebDriver 需要某些瀏覽器偏好設定才能正常運作,並且這些偏好設定將永遠被覆寫。

安裝 Firefox 擴充功能

假設您需要安裝一個名為 Firebug 的擴充功能。在您的 nightwatch.conf.js 中,您可以使用 Options 類別來設定 WebDriver 會話,如下所示

const firefox = require('selenium-webdriver/firefox');

const options = new firefox.Options() .addExtensions('../../../path/to/firebug.xpi') .setPreference('extensions.firebug.showChromeErrors', true);

module.exports = { src_folders: ['tests'], test_settings: { default: { browserName: 'firefox', desiredCapabilities: options } } };

或作為一個函數

module.exports = {
  src_folders: ['tests'],
  test_settings: {
    default: {
      browserName: 'firefox',
      desiredCapabilities() {
        const firefox = require('selenium-webdriver/firefox');

    const options = new firefox.Options()
      .addExtensions('../../../path/to/firebug.xpi')
      .setPreference('extensions.firebug.showChromeErrors', true);
    
    return options;
  }
}

} };