Wednesday, June 1, 2011

Using Selenium 2 with Python and web2py

I've used Selenium some time ago to create automated tests for a few Web2py applications I've written. On FLOSS weekly I heard of the new version which no longer needs the separate Java server I used in Selenium RC. I've got no problems using the Java server, in my testsuite I test if the server is available and then start it with

selserver = subprocess.Popen(["java", '-jar', r"C:\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar"])

 and then connect to it. Works fine, but less is more...

Installing Selenium in Python

I installed Selenium using pip install selenium. I'm using Python 2.7.1. There where two syntax errors while installing in webdriver\mouse.py.  and keyboard.py I guess this needs repairing at some point...

But first let's see if it works. In Python I can now

 from selenium import webdriver
 driver = webdriver.Chrome()

This gives me an exception, apparently I need an executable, to replace the former selenium server, but now a specialized browserspecific one. You can find it at http://code.google.com/p/selenium/downloads/list I downloaded chromedriver_win32_12.0.727.0.zip since I'm on Windows 7, unzipped it and placed the executable in a folder and made it reachable with my PATH by changing this environment variable.

Now my code - after starting a server in the background - opens a Chrome browser instance, which is good news. Let's see if we can control it by sending the browser to my app url at 127.0.0.1:8000/dibsa. This is the Web2py application I want to test. And let's say we want to login, and the menu item, the link to login is 'Login'

from selenium import webdrive
driver = webdriver.Chrome()
driver.get("http://127.0.0.1:8000/dibsa")
link=driver.find_element_by_partial_link_text("Login")
link.click()
driver.quit()

This code, in the opened browser now redirects to the login page. We can now continue to fill in the inputs, and then clicking the submit button. I could have used other methods like find_element_by_xpath() to select the link.
When ready you can close the browser with the close() method, but in that case the chromedriver binary stays in memory. If you use quit() it ends. If you first call close() and then quit() the selenium module crashes.

Conclusion
Selenium 2 seems to work fine, at least with Chrome. The server-executables are an improvement. No configuring needed, no fiddling with profiles and no java. Don't forget to end the chromedriver process by calling the quit() method.

7 comments:

  1. I am seeing an crash or exception even with just close. Did you find any solution for fixing this issue? Please do let me know.

    ReplyDelete
  2. I'm not having an issue, it's working for me on Windows 7 and on Mac. What's your setup? Mine is Python 2.7, current selenium module installed with pip, and the binary/executable chromedriver installed in my path.

    ReplyDelete
  3. Excellent incredible blog layout! How long have you been blogging for? you make running a blog look easy. The overall glance of your website is magnificent, let alone the content!

    Selenium Training in Chennai

    ReplyDelete
  4. Really it was an awesome article about JAVA, very interesting to read.You have provided an nice article,Thanks for sharing.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete