示例#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_jpm():
    firefox_binary_path = get_firefox_binary_path()
    cmd_jpm_run = "jpm run --binary-args 'url %s' -b %s" \
                  % (BASE_TEST_URL, firefox_binary_path)
    server, thread = start_server()
    try:
        # http://stackoverflow.com/a/4417735/3104416
        for line in get_command_output(cmd_jpm_run, cwd=EXT_PATH):
            print(colorize(line), bcolors.ENDC, end=' ')
    except KeyboardInterrupt:
        print("Keyboard Interrupt detected, shutting down...")
    print("\nClosing server thread...")
    server.shutdown()
    thread.join()
示例#3
0
def start_webext():
    firefox_binary_path = get_firefox_binary_path()
    cmd_webext_run = "npm start -- --start-url '%s' --firefox '%s'" \
        % (BASE_TEST_URL, firefox_binary_path)
    server, thread = start_server()
    try:
        # http://stackoverflow.com/a/4417735/3104416
        for line in get_command_output(cmd_webext_run, cwd=EXT_PATH):
            print(colorize(line.decode("utf-8")), bcolors.ENDC, end=' ')
    except KeyboardInterrupt:
        print("Keyboard Interrupt detected, shutting down...")
    print("\nClosing server thread...")
    server.shutdown()
    thread.join()
示例#4
0
def start_webext():
    firefox_binary_path = get_firefox_binary_path()
    cmd_webext_run = "npm start -- --start-url '%s' --firefox '%s'" \
        % (BASE_TEST_URL, firefox_binary_path)
    server, thread = start_server()
    try:
        # http://stackoverflow.com/a/4417735/3104416
        for line in get_command_output(cmd_webext_run, cwd=EXT_PATH):
            print(colorize(line), bcolors.ENDC, end=' ')
    except KeyboardInterrupt:
        print("Keyboard Interrupt detected, shutting down...")
    print("\nClosing server thread...")
    server.shutdown()
    thread.join()
示例#5
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)