Python – Headless Selenium Performance Tests with Google Chrome, Browsermob-Proxy and Xvfb


# -*- coding: utf-8 -*-
"""
# Install xvfb
sudo apt-get install -y xvfb

# create build directory
mkdir -p ~/build/selenium
cd ~/build/selenium

# Install API for browsermob-proxy and selenium
sudo pip install selenium browsermob-proxy --upgrade

# download browsermob proxy
wget https://github.com/downloads/webmetrics/browsermob-proxy/browsermob-proxy-2.0-beta-6-bin.zip
unzip browsermob-proxy-2.0-beta-6-bin.zip
# copy browsermob-proxy to /var/lib
sudo cp -r browsermob-proxy /var/lib/
sudo chown -R a:a /var/lib/browsermob-proxy

# create log directory
mkdir -p log

# download selenium-server
wget http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar

# start selenium-server
java /usr/bin/java -jar selenium-server-standalone-2.41.0.jar >> ./log/selenium.$(date +"%Y%d%m").log 2>&1&

# download chrome driver
wget http://chromedriver.storage.googleapis.com/2.9/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
# chromedriver

"""
import sys
import os
def main():
    from xvfbwrapper import Xvfb
    with Xvfb() as xvfb:
    #if True:
        open_page_in_selenium()

def open_page_in_selenium(browser='chrome'):
    import os
    from selenium import webdriver
    browsermob_proxy_filepath = "/var/lib/browsermob-proxy/bin/browsermob-proxy"
    # Create Proxy Server
    from browsermobproxy import Server
    server = Server(browsermob_proxy_filepath)
    server.start()
    proxy = server.create_proxy()
    # Create Webdriver
    #driver = webdriver.Firefox()
    # Create Chrome Driver - unused
    chromedriver = "./chromedriver"
    os.environ["webdriver.chrome.driver"] = chromedriver
    #driver = webdriver.Chrome(chromedriver)
    #chrome_options = webdriver.ChromeOptions()
    #chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
    #driver = webdriver.Chrome(chrome_options = chrome_options)
    # Create Profile using Proxy Server
    profile  = webdriver.FirefoxProfile()
    profile.set_proxy(proxy.selenium_proxy())
    driver = webdriver.Firefox(firefox_profile=profile)
    # Create HAR
    proxy.new_har("myhar")
    url = "http://www.python.org"
    try:
        from datetime import datetime
        print "%s: Go %s"%(datetime.now(), url)
        driver.get(url)
        print "%s: Finish %s"%(datetime.now(), url)
        #from selenium.webdriver.common.keys import Keys
        # submit query
        #elem = driver.find_element_by_name("q")
        #elem.send_keys("selenium")
        #elem.send_keys(Keys.RETURN)
        web_har = proxy.har # returns a HAR JSON blob
        print web_har
        # Get Additional Performance Data
        performance = driver.execute_script("return window.performance")
        print performance
        print "%s: Complete %s"%(datetime.now(), url)
    finally:
        driver.close()
    server.stop()

if __name__ == "__main__":
    main()
    pass


References

1. http://www.chrisle.me/2013/08/running-headless-selenium-with-chrome/

2. http://selenium-python.readthedocs.org/

3. http://stackoverflow.com/questions/8255929/running-webdriver-chrome-with-selenium