|
24 | 24 | Unit and acceptance tests for Selenium2Library |
25 | 25 |
|
26 | 26 |
|
| 27 | +Using virtualenv |
| 28 | +---------------- |
| 29 | +Background information about what virtualenv is, why one should be using virtualenv and how to use it |
| 30 | + |
27 | 31 | Unit and Acceptance Tests |
28 | 32 | ------------------------- |
29 | 33 |
|
@@ -63,6 +67,103 @@ To run just the unit tests, run:: |
63 | 67 |
|
64 | 68 | python test/run_unit_tests.py |
65 | 69 |
|
| 70 | +Debugging Selenium2Library |
| 71 | +-------------------------- |
| 72 | + |
| 73 | +Testing Third-Party Packages |
| 74 | +---------------------------- |
| 75 | + |
| 76 | +Sometimes in the process of developing and testing Selenium2Library |
| 77 | +one needs to determine whether or not an issue is within Selenium2Library |
| 78 | +or if it lies within a third-party package like Selenium or Robot Framework. |
| 79 | +Here are some hints for writing quick, short unit tests against Selenium |
| 80 | +and Robot Framework. |
| 81 | + |
| 82 | +Testing Selenium |
| 83 | +~~~~~~~~~~~~~~~~ |
| 84 | +First create a test directory and create an isolated Python environment |
| 85 | +using virtualenv:: |
| 86 | + |
| 87 | +~$ mkdir se-bug |
| 88 | +~$ cd se-bug |
| 89 | +~/se-bug$ virtualenv -p /usr/bin/python2.6 --no-site-packages clean-python26-env |
| 90 | + |
| 91 | +Activate the virtual environment:: |
| 92 | + |
| 93 | + ~/se-bug$ source clean-python26-env/bin/activate |
| 94 | + |
| 95 | +Install the version of Selenium for which you wish to test. In the following |
| 96 | +case we are going to check Selenium version 2.25.0. |
| 97 | + |
| 98 | +(clean-python26-env) ~/se-bug$ easy_install selenium==2.25.0 |
| 99 | + |
| 100 | +Create a test file, in this case ~/se-bug/testExeJS.py:: |
| 101 | + |
| 102 | +import unittest |
| 103 | +from selenium import webdriver |
| 104 | +
|
| 105 | +class ExecuteJavascriptTestCase(unittest.TestCase): |
| 106 | +
|
| 107 | + def setUp(self): |
| 108 | + self.driver = webdriver.Firefox() |
| 109 | +
|
| 110 | + def test_exe_javascript(self): |
| 111 | + driver = self.driver |
| 112 | + driver.get("http://www.google.com") |
| 113 | + url = driver.execute_script("return [ document.location ];") |
| 114 | + print('Finished') |
| 115 | +self.assertEqual(url[0]['href'], u"http://www.google.com/") |
| 116 | +
|
| 117 | + def tearDown(self): |
| 118 | + self.driver.close() |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + unittest.main() |
| 122 | + |
| 123 | +Breaking down this example test case we see in the setUp and tearDown |
| 124 | +methods we initiate and close the Firefox webdriver, respectively. |
| 125 | +In the one test, text_exe_javascript, we perform steps to verify or |
| 126 | +disprove the issue we are experiencing is with Selenium only. (In |
| 127 | +`this case`_ the browser was hanging after the execute_script call and |
| 128 | +not returning; thus I printed 'Finished' to help show where the test |
| 129 | +progressed to.) |
| 130 | + |
| 131 | +An important part of the above test case, and all unit tests, is the |
| 132 | +line "self.assertEqual(...". This is one example of the method's |
| 133 | +available to check for errors or failures. For example, you can check |
| 134 | +for trueness or falseness of a stament by using assertTrue() and |
| 135 | +assertFalse(). Or you can for inclusiveness and exclussiveness by using |
| 136 | +assertIn() and assertNotIn(), respectively. For more information about |
| 137 | +unittest see `Python's unittest documentation`_. The last two lines |
| 138 | +allow this test to be run from the command line. |
| 139 | + |
| 140 | +To run the unittest type:: |
| 141 | + |
| 142 | + (clean-python26-env) ~/se-bug$ python testExeJS.py |
| 143 | + |
| 144 | +In this example I removed the troubled selenium version and reinstalled a |
| 145 | +previous version, re-running the test case to verfiy selenium was the |
| 146 | +problem and not Selenium2Library:: |
| 147 | + |
| 148 | +(clean-python26-env) ~/se-bug$ rm -Rf clean-python26-env/lib/python2.6/site-packages/selenium-2.25.0-py2.6.egg |
| 149 | +(clean-python26-env) ~/se-bug$ easy_install selenium==2.24.0 |
| 150 | +(clean-python26-env) ~/se-bug$ python testExeJS.py |
| 151 | +Finished |
| 152 | +. |
| 153 | +---------------------------------------------------------------------- |
| 154 | +Ran 1 test in 6.198s |
| 155 | +
|
| 156 | +OK |
| 157 | +(clean-python26-env) ~/se-bug$ |
| 158 | + |
| 159 | +If you discover an issue with Selenium it is helpful to `report it`_ to |
| 160 | +the Selenium developers. |
| 161 | + |
| 162 | + |
| 163 | +Profiling |
| 164 | +--------- |
| 165 | +information on how to profile Selenium2Library code |
| 166 | + |
66 | 167 |
|
67 | 168 | Pushing Code to GitHub |
68 | 169 | ---------------------- |
@@ -187,3 +288,6 @@ are parsed by the reStructuredText parser. To build them, run:: |
187 | 288 | .. _downloads section on GitHub: https://github.com/rtomac/robotframework-selenium2library/downloads |
188 | 289 | .. _PyPI: http://pypi.python.org |
189 | 290 | .. _.pypirc file: http://docs.python.org/distutils/packageindex.html#the-pypirc-file |
| 291 | +.. _this case: http://code.google.com/p/selenium/issues/detail?id=4375 |
| 292 | +.. _report it: http://code.google.com/p/selenium/issues/list |
| 293 | +.. _Python's unittest documentation: http://docs.python.org/library/unittest.html |
0 commit comments