john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

selenium phantomjs headless browser python install

selenium: automation of browser actions (http://docs.seleniumhq.org/docs/02_selenium_ide.jsp#introduction)
phantomjs: headless browser (http://phantomjs.org/)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/bin/bash -x
# depends on curl, python, pip, tar or unzip
# not tested with Mac
# not tested with Windows

TARGETDIR='/tmp'
NAME="phantomjs-1.9.2"
SERVERURL="https://phantomjs.googlecode.com/files"
OS="$(uname)"
PORT=9134

if [[ $OS == "Linux" ]]; then
        INSTALLDIR="$NAME-linux-x86_64"
elif [[ $OS == "Darwin" ]]; then
        INSTALLDIR="$NAME-macosx"
else
        INSTALLDIR="$NAME-windows"
fi


if [[ $OS == "Linux" ]]; then
        PACKAGE="$INSTALLDIR.tar.bz2"
else
        PACKAGE="$INSTALLDIR.zip"
fi


if [ ! -d $TARGETDIR/$INSTALLDIR ]; then
        cd "$TARGETDIR"
        curl -OL "$SERVERURL/$PACKAGE"

        if [[ $OS == "Linux" ]]; then
                tar -xjvf "$PACKAGE"
        else
                unzip "$PACKAGE"
        fi
else
        echo "$TARGETDIR/$INSTALLDIR already exists"
fi

sudo apt-get install -y fontconfig
sudo pip install selenium
sudo pip install junit-xml

# reuse an existing running phantomjs (maybe better to kill and start again?)
phantomjs_pid=$(pidof phantomjs)
if [ ! "$phantomjs_pid" ]; then
        cd "$TARGETDIR/$INSTALLDIR/bin/"
        ./phantomjs --webdriver=$PORT --ignore-ssl-errors=true &
        phantomjs_pid=$(pidof phantomjs)
fi
echo "phantomjs running with pid: $phantomjs_pid"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# basic python script

from selenium import webdriver

driver = webdriver.PhantomJS( executable_path='/opt/phantomjs-1.9.2-linux-x86_64/bin/phantomjs', port=9134 )
driver.get( "http://127.0.0.1" )
print driver.current_url
driver.quit
print "done"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
phantomjs-1.9.2-linux-x86_64/bin/phantomjs --webdriver=9134 --ignore-ssl-errors=true

# more complete example with python unittest framework (used the Firefox Selenium IDE plugin -> Export)
# logs in, asserts there is an Admin tab which when clicked shows Group Info

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
import unittest, time, re

class SeleniumAdminLogin( unittest.TestCase ):
    def setUp( self ):
        self.driver = webdriver.PhantomJS( '/opt/phantomjs-1.9.2-linux-x86_64/bin/phantomjs', port=9134 )
        self.driver.implicitly_wait(30)
        self.base_url = "https://myexample.org"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_selenium_admin_login( self ):
        driver = self.driver
        driver.get( self.base_url + "/" )
        driver.find_element_by_id( "email" ).clear()
        driver.find_element_by_id( "email" ).send_keys( "admin@example.org" )
        driver.find_element_by_id( "password" ).clear()
        driver.find_element_by_id( "password" ).send_keys( "mypassword" )
        driver.find_element_by_id( "signin" ).click()
        self.assertEqual("https://myexample.org/home", driver.current_url)
        self.assertTrue(self.is_element_present(By.LINK_TEXT, "Launch the web app"))
        self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "a.admin > span"))
        driver.find_element_by_css_selector("a.admin > span").click()
        self.assertEqual("Group Info", driver.find_element_by_css_selector("h1").text)

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.delete_all_cookies()    # important to not persist sessions between tests
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REFERENCES:
http://phantomjs.org/release-1.8.html
https://github.com/ariya/phantomjs/wiki/API-Reference
http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/

  • « Continuous integration bamboo aws ec2
  • Chef install and knife »

Published

Apr 3, 2014

Category

python

~319 words

Tags

  • browser 4
  • headless 3
  • install 58
  • phantomjs 5
  • python 180
  • selenium 5