Selenium 1 to Selenium 2 Migration Selenium 1 to Selenium 2 Migration selenium selenium

Selenium 1 to Selenium 2 Migration


Going through the transition myself. If you had Selenium 1 experience, Selenium 2 feels quite different actually. Here is my Selenium 2 pros/cons vs. Selenium 1 I see so far (I use Python so some of them are Python specific):

Pros:

  • Much faster.
  • No need to run a separate server.
  • Gone are wait_for_page_to_load(), wait_for_element_present(), etc. All element interactions, clicks, etc. are blocking now, which is good. The only problem is with asynchronously loaded content (Ajax) though, see Con bellow.

Cons:

  • Loading/waiting for asynchronous content which used to be "free" with wait_for_page_to_load() requires coding now. These are the solutions I found so far:
    • use PageFactory/AjaxElementLocatorFactory like explained here, unfortunately I couldn't find the equivalent for Python.
    • use webdriver.implicitly_wait(N), this seems to be doing the trick with Python but using that seems to cause my script to miss changing elements which it used to detect before.
    • don't do sleep(T), loop until element appears, etc, that defeats the purpose of the whole thing (and makes wait_for_page_to_load look beautiful)...
  • The whole thing still feels a bit raw. Different drivers and bindings seem to miss different functionality. Not to say you can't use it but be ready to find 'alternate solutions' for certain problems.
  • The documentation is a bit dubious (related to the prev. point I guess). Especially for Python. Be ready to read code and experiment a lot (which luckily is easy with Python). Most of the 'tutorials' you'll find on the web (again, esp. Python, Java seems to be much better covered) are just to get you started with the plainest of web applications.
  • No PHP bindings, not a big one I prefer Python but our original suite was PHP so I noticed.
  • SeleniumIDE seems to be useless with Selenium 2.

Other differences:

  • The page elements you are accessing have to be 'visible' on the page at the moment when you ask selenium to find them. For example if you have a menu (containing a list of links) which opens when you hover your mouse over, you have to make sure it is open/visible before you click on a link inside (which wasn't the case in Selenium 1). This has it's uses since you'd be testing what an user would see on the page but requires the extra code. I found two solutions:
    • run a Javascript which would open your menu, in my case driver.execute_script("document.getElementById('dashboard_menu_navigation').show()") then click the menu item driver.find_element_by_link_text('Orders').click()
    • use the Mouse / Keyboard classes to simulate actual interaction, this seems to be broken in the Python bindings though (see Cons above):

Example (which throws 'WebElement' object has no attribute 'mouse_move_to' today):

element=driver.find_element_by_id('mn_dashboard')mouse=Mouse()mouse.move_to(element)

The Cons list seems longer but that is mostly if you are coming from Selenium 1. I do prefer the lightness and speed of Selenium 2 and despite the early code (using 2.0b4 at the time of writing) the whole thing is quite usable.

Hope to save someone some time...


Moving from Selenium 1 to Selenium 2 is as simple moving from

Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.example.com");selenium.open("/");

to

Webdriver driver = new FirefoxDriver();Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.example.com");selenium.open("/");

Since Selenium 2 is more tightly bound to the browser you will see huge difference. I have seen tests running at least 2x faster but in some cases I have seen it running 4x faster.

All the same best practises that you learnt during Selenium will be the translated across