6

I am trying to test a webpage's behaviour to requests from different referrers. I am doing the following so far

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.referer'] = referer

The problem is that the webpage has ajax requests which will change some things in the html, and those ajax requests should have as referer the webpage itself and not the referer i gave at the start. It seems that the referer is set once at the start and every subsequent request be it ajax or image or anchor takes that same referer and it never changes no matter how deep you browse, is there a solution to choocing the referer only for the first request and having it dynamic for the rest?

After some search i found this and i tried to achieve it through selenium, but i have not had any success yet with this:

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.onInitialized'] = """function() {page.customHeaders = {};};"""

Any ideas?

3
  • Any luck Evan? I have the same problem.
    – Sam R.
    Commented Aug 13, 2015 at 18:50
  • 1
    @norbertpy Hey, sorry for the delay. No i didnt manage this through python as i remember. I had to create a js script and while initiating the request with a set referrer i used an event called "oncomplete" (or something similar) to reset the referer to empty string and that worked. If you need the actual scripts let me know.
    – Evan
    Commented Aug 19, 2015 at 9:22
  • 1
    @norbertpy The reply below came too late for me, but it seems like it would work. You can try that if you want to keep everything(ish) within python.
    – Evan
    Commented Aug 19, 2015 at 9:27

1 Answer 1

2

From what I can tell you would need to patch PhantomJS to achieve this.

PhantomJS contains a module called GhostDriver which provides the HTTP API that WebDriver uses to communicate with the PhantomJS instance. So anything you want to do via WebDriver needs to be supported by GhostDriver, but it doesn't seem that onInitialized is supported by GhostDriver.

If you're feeling adventurous you could clone the PhantomJS repository and patch the src/ghostdriver/session.js file to do what you want.

The _init method looks like this:

_init = function() {
    var page;

    // Ensure a Current Window is available, if it's found to be `null`
    if (_currentWindowHandle === null) {
        // Create the first Window/Page
        page = require("webpage").create();
        // Decorate it with listeners and helpers
        page = _decorateNewWindow(page);
        // set session-specific CookieJar
        page.cookieJar = _cookieJar;
        // Make the new Window, the Current Window
        _currentWindowHandle = page.windowHandle;
        // Store by WindowHandle
        _windows[_currentWindowHandle] = page;
    }
},

You could try using the code you found:

page.onInitialized = function() {
  page.customHeaders = {};
};

on the page object created there.

Depending on what you test though you might be able to save a lot of effort and ditch the browser and just test HTTP requests directly using something like the requests module.

Not the answer you're looking for? Browse other questions tagged or ask your own question.