5

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?

1
  • you need to have selenium run on on a different port ( the default is 4444). That it is the reason why you are hitting the wrong url Commented Feb 17, 2011 at 1:58

5 Answers 5

9

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")
Sign up to request clarification or add additional context in comments.

1 Comment

for what self.server = Process(target=serve) stands for ? there's not mention of serve...
4

a co-worker and myself created some automated selenium tests using django and selenium 2. It works without having to use the jar files. Here's a link to the code that shows our test cases.

Comments

3

We currently run django tests successfully from Jenkins using django-jenkins: https://github.com/kmmbvnr/django-jenkins

FWIW nowadays django provides support for Selenium in the form of LiveServerTestCase: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.LiveServerTestCase

LiveServerTestCase launches a django server which allows clients like Selenium to connect to it.

Furthermore, you can now use PhantomJs as a web driver for headless testing. This makes CI integration way easier.

2 Comments

With LiveServerTestCase you don't have to have a live server running in parallel, and it will use the test database (including honoring the fixtures variable!). This is definitly the way to go for Continuous Integration. The staticfiles module also has a subclass for LiveServerTestCase to handle static files in that case correctly.
The subclass @Risadinha mentions is StaticLiveServerTestCase: django.readthedocs.org/en/latest/ref/contrib/…
1

The second argument of the selenium() call is supposed to be the Selenium server port number (as written in David's answer), not the tested application's port number. The default port number is 4444. I would replace the call with :

  self.selenium = selenium("localhost", 4444, ....

Comments

0

For automating Selenium tests I would definitely use a CI solution like Jenkins. You can configure Jenkins to pull your code repository and trigger the Selenium tests from your server. I have been using Pytest for doing so from Jenkins.

You can find a step by step tutorial of configuring Jenkins with Github and Selenium here: http://www.6020peaks.com/2015/12/how-to-build-a-test-automation-solution-for-your-web-projects/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.