firefox-devtools / har-export-trigger

Trigger HAR export any time directly from within a page.
https://addons.mozilla.org/en-US/firefox/addon/har-export-trigger/
Mozilla Public License 2.0
55 stars 15 forks source link

Can it be used with Selenium 3? #44

Open dpburst opened 3 years ago

dpburst commented 3 years ago

Hi! I am just wondering whether this addon can be used together with Selenium 3? I just wrote a demo, trying to get HAR with python selenium 3. But it seems that it is not working at all (with no har file generated). Here is my profile setting. I am testing on MacOS, firefox 78.3.0esr and Selenium 3.141.0. ` profile.set_preference("extensions.netmonitor.har.enableAutomation", True); profile.set_preference("extensions.netmonitor.har.contentAPIToken", "test"); profile.set_preference("extensions.netmonitor.har.autoConnect", True);

profile.set_preference("devtools.netmonitor.enabled", True);
profile.set_preference("devtools.netmonitor.har.compress", False);
profile.set_preference("devtools.netmonitor.har.defaultFileName", "Autoexport_%y%m%d_%H%M%S");
profile.set_preference("devtools.netmonitor.har.defaultLogDir", HARDIR);
profile.set_preference("devtools.netmonitor.har.enableAutoExportToFile", True);
profile.set_preference("devtools.netmonitor.har.forceExport", True);   
profile.set_preference("devtools.netmonitor.har.includeResponseBodies", False);
profile.set_preference("devtools.netmonitor.har.jsonp", False);
profile.set_preference("devtools.netmonitor.har.jsonpCallback", False);
profile.set_preference("devtools.netmonitor.har.pageLoadedTimeout", "2500");
profile.update_preferences()`
janodvarko commented 3 years ago

@dpburst thanks for the report!

I don't have much experience with Selenium, can you provide some instructions to help me to it setup up on my machine and try the addon with it?

Thanks!

dpburst commented 3 years ago

@janodvarko Thank you so much for your reply. Here is my code (but it can not work):

import os
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# path to the firefox binary inside the Tor package
binary = '/Applications/firefox.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
    raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)
HARDIR = '/Users/aaron/Desktop/log'
AR_EXPORTTRIGGER_PATH = "/Users/aaron/Desktop/har_export_trigger-0.6.1-an+fx.xpi"

def get_profile():
    assert os.path.exists(AR_EXPORTTRIGGER_PATH)
    profile=webdriver.FirefoxProfile()
    profile.set_preference("devtools.netmonitor.enabled", True);
    # profile.add_extension(extension=AR_EXPORTTRIGGER_PATH)
    profile.set_preference("extensions.netmonitor.har.enableAutomation", True);
    profile.set_preference("extensions.netmonitor.har.contentAPIToken", "test");
    profile.set_preference("extensions.netmonitor.har.autoConnect", True);

    profile.set_preference("devtools.netmonitor.har.compress", False);
    profile.set_preference("devtools.netmonitor.har.defaultFileName", "Autoexport_%y%m%d_%H%M%S");
    profile.set_preference("devtools.netmonitor.har.defaultLogDir", HARDIR);
    profile.set_preference("devtools.netmonitor.har.enableAutoExportToFile", True);
    profile.set_preference("devtools.netmonitor.har.forceExport", True);   
    profile.set_preference("devtools.netmonitor.har.includeResponseBodies", False);
    profile.set_preference("devtools.netmonitor.har.jsonp", False);
    profile.set_preference("devtools.netmonitor.har.jsonpCallback", False);
    profile.set_preference("devtools.netmonitor.har.pageLoadedTimeout", "2500");
    profile.update_preferences()
    return profile

browser = None
def get_browser():
    global browser  
    # only one instance of a browser opens, remove global for multiple instances
    if not browser: 
        browser = webdriver.Firefox( firefox_profile = get_profile())
        browser.install_addon(AR_EXPORTTRIGGER_PATH, temporary=True)
    return browser

if __name__ == "__main__":
    browser = get_browser()
    urls = (
        ('tor browser check', 'https://check.torproject.org/'),
        # ('ip checker', 'http://icanhazip.com')
    )
    for url_name, url in urls:
        print("getting", url_name, "at", url)
        browser.get(url)
        # browser.quit()

I find that profile.add_extension(extension=AR_EXPORTTRIGGER_PATH) can not addon the extension to firefox. browser.install_addon(AR_EXPORTTRIGGER_PATH, temporary=True) can install the extension, but in both cases I can not get any HAR file saved in HARDIR.

My settings are: python 3.7 selenium 3.141.0 firefox 81.0.1 geckodriver 0.26.0 har_export_trigger-0.6.1-an+fx.xpi

Greatly appreciate it if you could make it work! Thanks!

janodvarko commented 3 years ago

I installed Selenium via npm install selenium-webdriver and tested HAR Trigger using the attached example test. It works for me (I can see HAR logs/files in the target directory), but how can I:

1) Get the Selenium version installed? 2) Ensure that Selenium 3 is installed?

const { Builder, By, Key, until } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

const HARDIR = "C:\\src\\www\\selenium\\first-test\\logs";

/**
 * Test entry point.
 */
async function runTest() {
  let driver;

  try {
    const options = new firefox.Options()
      .setPreference("extensions.firebug.allPagesActivation", "on")
      .setPreference("devtools.netmonitor.enabled", true)
      .setPreference("extensions.netmonitor.har.enableAutomation", true)
      .setPreference("extensions.netmonitor.har.contentAPIToken", "test")
      .setPreference("extensions.netmonitor.har.autoConnect", true)
      .setPreference("devtools.netmonitor.har.compress", false)
      .setPreference("devtools.netmonitor.har.defaultFileName", "Autoexport_%y%m%d_%H%M%S")
      .setPreference("devtools.netmonitor.har.defaultLogDir", HARDIR)
      .setPreference("devtools.netmonitor.har.enableAutoExportToFile", true)
      .setPreference("devtools.netmonitor.har.forceExport", true)
      .setPreference("devtools.netmonitor.har.includeResponseBodies", false)
      .setPreference("devtools.netmonitor.har.jsonp", false)
      .setPreference("devtools.netmonitor.har.jsonpCallback", false)
      .setPreference("devtools.netmonitor.har.pageLoadedTimeout", "2500")
      .addArguments("--devtools", "")
      .addExtensions('./har_export_trigger-0.6.1-an+fx.xpi');

    const driver = await new Builder()
        .forBrowser('firefox')
        .setFirefoxOptions(options)
        .build();

    //await driver.installAddon('./har_export_trigger-0.6.1-an+fx.xpi');
    await driver.get('http://janodvarko.cz/test/harexporttrigger');
    let title = await driver.getTitle();
    console.log("The title is: " + title);

    driver.findElement(By.id("xhr1")).click();
    driver.findElement(By.id("xhr1")).click();

    await driver.sleep(2000);
    await driver.quit();
  } catch (err) {
    console.log(err);
  }
};

runTest();