Python pytest – using selenium with PhantomJS

Here is quick tutorial to use python selenium with PhantomJS as driver. We will fetch a page and assert that page title has certain keyword. Here is sample code for it. Note that you must have PhantomJS, python selenium and pytest binding installed for this tutorial. We’ll use Mac for this tutorial but it should work on Linux also.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def test_title():
  driver = webdriver.PhantomJS("/usr/local/bin/phantomjs")
  title = driver.title
  print "title=%s" % (title)
  assert "InfoHeap" in title, "Tile should have InfoHeap"

To run above code (-s switch will cause stdout to be printed on screen)

$ py.test -s

If everything is fine, you will get an output like this.

================================================================ test session starts ================================================================
platform darwin -- Python 2.7.10, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /Users/user1/tmp/qa, inifile: 
collected 1 items title=dev InfoHeap - Tech tutorials, tips, tools and more

============================================================= 1 passed in 9.06 seconds ==============================================================
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments