0

I'm trying to export a Selenium script to Python from the Selenium IDE. I am using a few user-extension.js functions though (which are working in Selenium IDE). After exporting to Python, the generated script looks like this:

from selenium import selenium
import unittest, time, re

class new_selenium_test(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://localhost/")
        self.selenium.start()

    def test_selenium_assert_something(self):
        sel = self.selenium
        # sel.assert_something("abc=1", "x=126")

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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

Note that the most interesting line, where I call my user extension code (function "assert_something", which maps on function "assertSomething" in my user-extensions.js file), is commented out. When I activate that line and run the script against Selenium server like this:

py.test new-selenium-test.py

I get an error like this:

AttributeError: 'selenium' object has no attribute 'assert_something'

Any idea why Selenium IDE comments out my custom call, and why it does not execute it from Python?

Note that I have started the Selenium server like this:

java -jar selenium-server-standalone-2.0rc2.jar -userExtensions /path/user-extensions.js

Thanks for your help!

nxhtltzw
  • 3
  • 1

1 Answers1

0

You need to rewrite your custom JavaScript functions in Python, as described here:

http://groups.google.com/group/selenium-users/browse_thread/thread/e927dad7e6cb2944/1712b997934cece5

It can't connect the Python object to your custom JS, so it leaves that comment there to remind you to implement it in Python.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Does my "assert" function then require a "do_command" call on the low level, or something else? – nxhtltzw Jul 19 '11 at 13:37
  • I don't know what you're asserting, but if the necessary information is accessible from this scope, you can probably just write something like `def assert_something(self, abc, x): return self.assert_equal(abc < x, True)` then call it with `self.assert_equal(abc=1, x=126)` from where your JavaScript function is commented out. Otherwise, if it really needs to be on Selenium, you'll have to subclass as described in my link. Unless you're changing state and testing an assert before and after, you shouldn't need a `do_command` I don't think. – agf Jul 19 '11 at 13:52
  • Thanks for the info.Since my assert_something function also uses a custom locator, I assume I have to make Python aware of this custom locator as well. How do I accomplish that? In the Selenium IDE this works out of the box because I provide a Target like this: "myprefix=blabla". I have implemented a PageBot.prototype.locateElementByMyprefix function that does the lookup. How do I call this particular locator from Python then, in combination with my assert_something function? Can't seem to find anything on the internet about this. – nxhtltzw Jul 20 '11 at 07:31