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") driver.get("https://dev.infoheap.com/") title = driver.title print "title=%s" % (title) assert "InfoHeap" in title, "Tile should have InfoHeap" driver.close()
To run above code (-s switch will cause stdout to be printed on screen)
$ py.test -s withphantom.py withphantom.py
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 withphantom.py title=dev InfoHeap - Tech tutorials, tips, tools and more . ============================================================= 1 passed in 9.06 seconds ==============================================================