簡體   English   中英

WebdriverIO - 如何向所有請求添加自定義標頭

[英]WebdriverIO - How to add custom headers to all requests

我需要向所有請求添加自定義標頭,以便訪問通常隱藏在公司代理和 vpn 后面的 Web 應用程序,並能夠運行 E2E 測試。 作為自動化工具,我們使用 WebdriverIO。 到目前為止,我嘗試按照官方 wdio 文檔( https://webdriver.io/docs/options/ )在 wdio.conf.js 文件中設置自定義標頭,並在我的配置文件中的功能中添加標頭選項。 不幸的是,這不起作用,我仍然無法連接到我們的網絡應用程序。 有沒有人嘗試在 wdio.config.js 文件中添加自定義標頭並運行 E2E 測試? 我的 wdio.config.js 文件的內容如下:

        exports.config = {
      runner: 'local',
    
      specs: [
        './src/specs/**/login.spec.ts',
      ],
      // Patterns to exclude.
      exclude: [
        // 'path/to/excluded/files'
      ],
    
      maxInstances: 10,
    
      capabilities: [
        {
          maxInstances: 5,
          browserName: 'chrome',
          acceptInsecureCerts: true,
    
          headers: {
            'REQUIRED-HEADER-KEY': 'HEADER-VALUE'
          },
    
          'goog:chromeOptions': {
            'excludeSwitches': [
              'enable-automation'
            ],
            prefs: {
              'profile.managed_default_content_settings.notifications': 1, 
              'plugins.always_open_pdf_externally': true, 
              'download': {
                'prompt_for_download': false, 
                'default_directory': process.cwd() + '/tmp'
              }
            },
            args: [
              '--disable-gpu',
              '--window-size=1920,1080',
              '--no-sandbox',
              '--disable-dev-shm-usage',
              '--start-maximized',
            ],
          },
        },
      ],
}

目前無法執行此操作,請參閱webdriverio/issues/6361

但是你可以用modheader_selenium做到這一點:

npm install chrome-modheader

const chromeModheader = require('chrome-modheader');
exports.config = {
...
    capabilities: [{
        browserName: 'chrome',
        "goog:chromeOptions": {
            extensions: [chromeModheader.getExtension()],
        }
    }],
...
    before: function (capabilities, specs) {
        browser.url(chromeModheader.getAddHeaderUrl('YOUR_HEADER', 'YOUR_HEADER_VALUE'));
    },
...
}

你能看看有什么問題嗎? 謝謝

const { config } = require('../wdio.conf');
const chromeModheader = require('chrome-modheader');
config.capabilities = [{
maxInstances: 1,
browserName: 'chrome',
'goog:chromeOptions': {
extensions: [chromeModheader.getExtension()],
enter code here
'excludeSwitches': [
  'enable-automation',
  '--disable-infobars',
  '--disable-blink-features=AutomationControlled',
  '--disable-dev-shm-usage',
  '--no-sandbox',
],

before: function (capabilities, specs) {
  browser.url(chromeModheader.getAddHeaderUrl('X-Captcha-Override', 'disable'));
},
}
}];
exports.config = config;

由於未創建會話,請求失敗,狀態為 500:無法啟動新會話。 創建與驅動程序服務的會話時出錯。 停止驅動程序服務:無法啟動新會話。 響應代碼 500。消息:未創建會話:無法處理擴展 #1 [0-0] 來自未知錯誤:無法 base64 解碼 [0-0] 構建信息:版本:'4.4.0',修訂:'e5c75ed026a'

暫無
暫無

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

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