Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automating Selenium tests in Python

I have a Django project for which I'm trying to write browser interaction tests with Selenium. My goal is to have the tests automated from Hudson/Jenkins. So far I'm able to get the test hitting the Django server, but from the server logs I see it's hitting the url /selenium-server/driver instead of the right path.

Here's my code (based on what was generated by the Selenium IDE plugin for Firefox:

from selenium import selenium


class AccountAdminPageTests(unittest.TestCase):
    def setUp(self):
        self.selenium = selenium("localhost", 
                                 8000, 
                                 "*chrome", 
                                 "http://localhost:8000/")
        self.selenium.start()
        self.selenium.open("/")

    def test_ok(self):
        self.assertTrue(self.selenium.is_text_present('OK'))

    def tearDown(self):
        self.selenium.stop()


if __name__ == "__main__":
    unittest.main()

Any clues?

like image 859
exupero Avatar asked Feb 07 '11 14:02

exupero


1 Answers

Never seen the exact error, but I think that Selenium is trying to connect to your app rather than the selenium Server ( a .jar file).

Port of the selenium server should be the first argument to selenium()

That should default to port 4444, you probably have to start it with

$ java -jar selenium-server.jar

FWIW here's how I got selenium tests running on a CI server...

from multiprocessing import Process
from django.test import TestCase
from selenium import selenium

class SeleniumFixtureCase(TestCase):
"""
Wrapper to multiprocess localhost server and selenium instance on one
test run.
"""

def setUp(self):
    "Make the selenium connection"
    TestCase.setUp(self)
    self.server = Process(target=serve)
    self.server.start()
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*firefox",
                             "http://localhost:8000/")
    self.selenium.start()

def tearDown(self):
    "Kill processes"
    TestCase.tearDown(self)
    self.server.terminate()
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)

def _login(self):
    "Login as Albert Camus"
    self.selenium.open("http://localhost:8000/admin/")
    self.selenium.wait_for_page_to_load("30000")
    self.selenium.type("id_username", "albert")
    self.selenium.type("id_password", "albert")
    self.selenium.click("//input[@value='Log in']")
    self.selenium.wait_for_page_to_load("30000")
like image 109
David Miller Avatar answered Sep 28 '22 08:09

David Miller