SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

Having issue using PhantomJS with a proxy

Open kevtrn opened this issue 13 years ago • 7 comments

Hello,

I am behind a proxy and I have been trying many options to bypass the proxy so I can run my tests using the PhantomJS browser but haven't been able to do so successfully. Is there any convenient way to set the proxy when loading the PhantomJS webdriver?

Thanks, Kevin

kevtrn avatar Jun 14 '13 20:06 kevtrn

I posted this to the RF user group:

Supporting the proxy via DesiredCapabilities is marked TODO in PhantomJS's source code. Also setting the command line arguments via DesiredCapabilities (phantomjs.cli.args) is only implemented in the Java bindings. The only way currently to set the proxy AFAIK is via the command line. You can either launch phantomjs outside of Selenium2Library and use the remote_url to connect to it or you can get your hands a little dirty and make a custom library. Writing your own keyword allows you to expose some browser-specific arguments that are not exposed by Open Browser. The custom library approach is demonstrated below (it worked for me phantomJS 1.9.1/RF 2.8.1/Se 1.2/Py 2.7/Win7):

*** Settings ***
Library           Selenium2Library
Library           Se2LibExtensions.py

*** Test Cases ***
PhantomJS Proxy
    ${service args}=    Create List    --proxy=192.168.132.104:8888
    Open Phantomjs    http://www.google.com/    service_args=${service args}
    [Teardown]    Close All Browsers
# contents of Se2LibExtensions.py
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger

class Se2LibExtensions(object):

    def open_phantomjs(self, url, alias=None, service_args=None, desired_capabilities=DesiredCapabilities.PHANTOMJS, service_log_path=None, executable_path="phantomjs", port=0):
        """Open the PhantomJS browser to the given `url`."""
        s2l = BuiltIn().get_library_instance('Selenium2Library')
        browser = webdriver.PhantomJS(service_args=service_args, desired_capabilities=desired_capabilities, service_log_path=service_log_path, executable_path=executable_path, port=port)
        browser.get(url)
        logger.debug('Opened browser with session id %s'
                    % browser.session_id)
        return s2l._cache.register(browser, alias)

ombre42 avatar Jun 21 '13 20:06 ombre42

Is there any update on the support for proxy as part of DesiredCapabilities.

chethan avatar Jul 16 '13 14:07 chethan

2 months later ... any progress ?

a0s avatar Sep 10 '13 23:09 a0s

@ombre42: With your code above are you suggesting we add an 'Open PhantomJS' keyword? add it temporarily? Or are you suggesting we recommend this code as a work around till the issue is solved within the phantomjs codebase?

emanlove avatar Sep 14 '13 13:09 emanlove

It was intended to be a workaround. It is hard to commit to an API in a wrapper when the underlying API is somewhat unstable (referring to the WebDriver constructors as a whole). What I think should definitely be done is adding a documented, private method for adding a WebDriver instance to the cache. Perhaps _add_webdriver(url=None, alias=None). Using the term webdriver and having url default to None because they do not apply to the burgeoning use of Selenium for mobile automation.

Perhaps a middle ground between having lots of custom Open * keywords and forcing our users to instantiate the browser in Python would be to implement a keyword similar to Connect To Database Using Custom Params. It essentially is a workaround for RF for before it supported keywords using **kwargs and allows the user to avoid writing Python code but be generic enough to support dissimilar method signatures.

@emanlove what do you think of having something like Create Webdriver Using Custom Parameters?

ombre42 avatar Sep 16 '13 03:09 ombre42

@ombre42 : Sounds good to me.

emanlove avatar Oct 16 '13 11:10 emanlove

With newly implemented keyword 'Create WebDriver' you should be able to accomplish this with some lines like

*** Settings ***
Library  Collections
Library  Selenium2Library

*** Test Cases ***
Demonstrate Using Proxy With PhantomJS Via Create WebDriver Keyword
    [Tags]  phantomjs
    ${service args}=    Create List    --proxy=192.168.132.104:8888
    Create WebDriver  PhantomJS  service_args=${service args}
    Close Browser

emanlove avatar Dec 26 '13 19:12 emanlove