예제 #1
0
def start_webdriver(with_extension=False):
    """ Open a webdriver instance and a server for the test pages

    This is meant to be imported and run manually from a python or
    ipython shell. A webdriver instance is returned and both the webdriver
    and server will automatically clean up when the shell is exited.

    Parameters
    ----------
    with_extension : boolean
        Set to True to also load OpenWPM extension instrumentation

    Returns
    -------
    webdriver
        A selenium webdriver instance.
    """
    firefox_binary_path = get_firefox_binary_path()
    geckodriver_executable_path = get_geckodriver_exec_path()

    fb = FirefoxBinary(firefox_path=firefox_binary_path)
    server, thread = start_server()

    def register_cleanup(driver):
        driver.get(BASE_TEST_URL)

        def cleanup_server():
            print("Cleanup before shutdown...")
            server.shutdown()
            thread.join()
            print("...sever shutdown")
            driver.quit()
            print("...webdriver closed")

        atexit.register(cleanup_server)
        return driver

    fp = webdriver.FirefoxProfile()
    if with_extension:
        # TODO: Restore preference for log level in a way that works in Fx 57+
        # fp.set_preference("*****@*****.**", "all")
        configure_firefox.optimize_prefs(fp)
    driver = webdriver.Firefox(
        firefox_binary=fb, firefox_profile=fp,
        executable_path=geckodriver_executable_path
    )
    if with_extension:
        # add openwpm extension to profile
        create_xpi()
        ext_xpi = join(EXT_PATH, 'openwpm.xpi')
        driver.install_addon(ext_xpi, temporary=True)

    return register_cleanup(driver)
예제 #2
0
def start_webdriver(with_extension=False):
    """ Open a webdriver instance and a server for the test pages

    This is meant to be imported and run manually from a python or
    ipython shell. A webdriver instance is returned and both the webdriver
    and server will automatically clean up when the shell is exited.

    Parameters
    ----------
    with_extension : boolean
        Set to True to also load OpenWPM extension instrumentation

    Returns
    -------
    webdriver
        A selenium webdriver instance.
    """
    path = os.environ['PATH']
    if FF_BIN_PATH not in path:
        os.environ['PATH'] = FF_BIN_PATH + os.pathsep + path

    fb = FirefoxBinary()
    server, thread = start_server()

    def register_cleanup(driver):
        driver.get(BASE_TEST_URL)

        def cleanup_server():
            print("Cleanup before shutdown...")
            server.shutdown()
            thread.join()
            print("...sever shutdown")
            driver.quit()
            print("...webdriver closed")

        atexit.register(cleanup_server)
        return driver

    fp = webdriver.FirefoxProfile()
    if with_extension:
        # add openwpm extension to profile
        create_xpi()
        ext_xpi = join(EXT_PATH, 'openwpm.xpi')
        fp.add_extension(extension=ext_xpi)
        fp.set_preference("*****@*****.**", "all")

        configure_firefox.optimize_prefs(fp)

    return register_cleanup(
        webdriver.Firefox(firefox_binary=fb, firefox_profile=fp))
예제 #3
0
def start_webdriver(
    with_extension=True, load_browser_params=True, browser_params_file=None
):
    """Open a webdriver instance and a server for the test pages

    This is meant to be imported and run manually from a python or
    ipython shell. A webdriver instance is returned and both the webdriver
    and server will automatically clean up when the shell is exited.

    Parameters
    ----------
    with_extension : boolean
        Set to True to also load OpenWPM extension instrumentation
    load_browser_params : boolean
        Set to True to load browser_params
    browser_params_file : string
        Specify the browser_params.json to load.
        If None, default_params will be loaded.

    Returns
    -------
    webdriver
        A selenium webdriver instance.
    """
    firefox_binary_path = get_firefox_binary_path()

    fb = FirefoxBinary(firefox_path=firefox_binary_path)
    server, thread = start_server()

    def register_cleanup(driver):
        driver.get(BASE_TEST_URL)

        def cleanup_server():
            print("Cleanup before shutdown...")
            server.shutdown()
            thread.join()
            print("...sever shutdown")
            driver.quit()
            print("...webdriver closed")

        atexit.register(cleanup_server)
        return driver

    fp = webdriver.FirefoxProfile()
    if with_extension:
        # TODO: Restore preference for log level in a way that works in Fx 57+
        # fp.set_preference("*****@*****.**", "all")
        configure_firefox.optimize_prefs(fp)
    driver = webdriver.Firefox(firefox_binary=fb, firefox_profile=fp)
    if load_browser_params is True:
        # There's probably more we could do here
        # to set more preferences and better emulate
        # what happens in TaskManager. But this lets
        # us pass some basic things.

        browser_params = load_default_params()[1][0]
        if browser_params_file is not None:
            with open(browser_params_file, "r") as f:
                browser_params.update(json.loads(f.read()))
        js_request = browser_params["js_instrument_settings"]
        js_request_as_string = jsi.convert_browser_params_to_js_string(js_request)
        browser_params["js_instrument_settings"] = js_request_as_string

        profile_dir = driver.capabilities["moz:profile"]
        with open(join(profile_dir, "browser_params.json"), "w") as f:
            f.write(json.dumps(browser_params))

    if with_extension:
        # add openwpm extension to profile
        create_xpi()
        ext_xpi = join(EXT_PATH, "dist", "openwpm-1.0.zip")
        driver.install_addon(ext_xpi, temporary=True)

    return register_cleanup(driver)