Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantomjs through selenium in python

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?

like image 725
Evan Avatar asked Jul 01 '14 17:07

Evan


People also ask

What is PhantomJS Python?

PhantomJS is a headless Webkit, which has a number of uses. In this example, we'll be using it, in conjunction with Selenium WebDriver, for conducting basic system tests directly from the command line. Since PhantomJS eliminates the need for a graphical browser, tests run much faster.

Is PhantomJS faster than Selenium?

Being a headless browser, the interactions are much faster than the real browser. So the performance time is smoother in PhantomJS than in Selenium.


1 Answers

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.

like image 93
Andrew Magee Avatar answered Nov 06 '22 07:11

Andrew Magee