Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add custom headers for my nightwatch.js tests?

So I been digging the web (google, stackoverflow, github) trying to find a simple way to add custom headers to my tests. So far nothing pop out. Maybe some thing for java or firefox, but nothing clear.

I'm using:

  • NodeJS: v6.11.3
  • Nightwatch: 0.9.16
  • ChromeDriver: 2.32
  • Selenium 3.5.3

So what i would like is that headers would propagate in all tests including 302 Redirects

before: {
  this.setHeaders = {
      "X-CUSTOM-HEADER-A": "Data-A",
      "X-CUSTOM-HEADER-B": "Data-B",
  }
}

Is there something like this avaliable? Any chromedriver args maybe?

like image 382
Ignus Avatar asked Oct 22 '25 05:10

Ignus


1 Answers

The new Nightwatch 2.x release supports talking to CDP. So for Chrome, you can use the following. You have to call 'Network.enable' first to enable the Network dev commands. (This took a little trial-and-error.)

await browser.chrome.sendDevToolsCommand('Network.enable')
await browser.chrome.sendDevToolsCommand('Network.setExtraHTTPHeaders', 
    { 
        headers:
            {
                "X-CUSTOM-HEADER-A": "Data-A",
                "X-CUSTOM-HEADER-B": "Data-B"
            }
    })

More info on the dev tool commands in Nightwatch: https://nightwatchjs.org/api/commands/#using-chrome-devtools-protocol

like image 169
Chris Seth Jackson Avatar answered Oct 25 '25 02:10

Chris Seth Jackson