Python Testing - PhantomJS with Selenium WebDriver
PhantomJS has a lot of different uses. The interesting bit for me is to use PhantomJS as a lighter-weight replacement for a browser when running web acceptance tests. This enables faster testing, without a display or the overhead of full-browser startup/shutdown.
I write my web automation using Selenium WebDriver, in Python.
In future versions of PhantomJS, the GhostDriver component will be included.
So, Ghostdriver is the bridge we need to use Selenium WebDriver with Phantom.JS.
Since it is not available in the current PhantomJS release, you can try it yourself by compiling a special version of PhantomJS:
It wes pretty trvial to setup on Ubuntu (12.04):$ sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev $ git clone git://github.com/ariya/phantomjs.git $ cd phantomjs $ git checkout 1.8 $ ./build.sh $ git remote add detro https://github.com/detro/phantomjs.git $ git fetch detro && git checkout -b detro-ghostdriver-dev remotes/detro/ghostdriver-dev $ ./build.sh
Then grab the `phantomjs` binary it produced (look inside `phantomjs/bin`). This is a self-contained executable, it can be moved to a different directory or another machine. Make sure it is located somewhere on your PATH, or declare it's location when creating your PhantomJS driver like the example below.
for these examples, `phantomjs` binary is located in same directory as test script.
Example: Python Using PhantomJS and Selenium WebDriver.#!/usr/bin/env python driver = webdriver.PhantomJS('./phantomjs') # do webdriver stuff here driver.quit()
Example: Python Unit Test Using PhantomJS and Selenium WebDriver.#!/usr/bin/env python import unittest from selenium import webdriver class TestUbuntuHomepage(unittest.TestCase): def setUp(self): self.driver = webdriver.PhantomJS('./phantomjs') def testTitle(self): self.driver.get('http://www.ubuntu.com/') self.assertIn('Ubuntu', self.driver.title) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)
- Selenium WebDriver Python API Documentation
What is the Best Way To Learn Selenium?
(a small rant on asking answerable questions)
At a certain online QA/Testing Forum I regularly visit, the Selenium forum is one of the most active.
I continuously see newbie questions like: "How I do I learn Selenium?", with little or no other context given.
Then, helpful responders chime in with tangentially related answers. That's about as good as you can do, based on the lack of information given in the original post. This happens often and wastes significant brain activity in the responders, while generally giving no value to the OP or forum community.
So first... a clarification:
Selenium WebDriver is a system for automating browser interaction. It is accessed via programming library/API. If you want to use it without touching code, it is not possible (record/replay/export-madness via IDE aside). You can abstract away most of the programming aspects with higher level frameworks, but at that point you are no longer really dealing with Selenium anymore.
Here was my response to a recent forum example:
"Best way to start learning Selenium - I would like to know what is the best practice to start with Selenium for non programmers. Is there any books or ebooks?"
The responses compelled me to chime in with a rant:
To anyone on this thread looking for the "Best way to start learning Selenium":
Think of it this way:
Selenium (WebDriver) provides language bindings and a similar API for several programming languages. It gives you a programming library that can manipulate a browser and web elements/controls.
You can't "learn" a library for a programming language if you don't know the language itself (syntax, idioms, etc).
So the question shouldn't be: "How do I learn Selenium?", with no other information or context given.
Selenium should be used with either: the supported language that you are most comfortable with, or the language of the system under test you are working against.
If you are looking for information, be specific about what you want to learn. General understanding of the components that make up Selenium is great, but really, using the API's from any language is the best way to learn. If you specify a programming language along with your question, at least answers can be directed towards frameworks, code samples, documentation, tutorials, etc, that are *relevant* to you.
Diving into the main Selenium docs can be confusing at first, as it covers things from a general level... mixing in code samples from various languages.
So, my question to you: Which programming language are you going to use Selenium with? If you define that, someone can surely help point you in the right direction.
How are your programming skills? Which language do you want to learn in? If you know several, switching is trivial down the line, as the API's are similar between languages.
If you don't have skills in any Selenium supported programming language (java, c#, python, ruby, php, etc), then learn one of those before you even touch Selenium. If you have a language in mind, please phrase your question using that in context.
p.s. now go RTFM: http://seleniumhq.org/docs/rant over.