|
| 1 | +Getting Started |
| 2 | +--------------- |
| 3 | + |
| 4 | +Simple Usage |
| 5 | +~~~~~~~~~~~~ |
| 6 | + |
| 7 | +If you have installed Selenium server and Python bindings and able to |
| 8 | +run the server, you can start using it from Python like this. |
| 9 | + |
| 10 | +:: |
| 11 | + |
| 12 | + from selenium import webdriver |
| 13 | + from selenium.webdriver.common.keys import Keys |
| 14 | + |
| 15 | + driver = webdriver.Firefox() |
| 16 | + driver.get("http://www.python.org") |
| 17 | + assert "Python" in driver.title |
| 18 | + elem = driver.find_element_by_name("q") |
| 19 | + elem.send_keys("selenium") |
| 20 | + elem.send_keys(Keys.RETURN) |
| 21 | + assert "Google" in driver.title |
| 22 | + driver.close() |
| 23 | + |
| 24 | +The above script can be saved into a file (eg:- |
| 25 | +`python_org_search.py`), then it can be run like this:: |
| 26 | + |
| 27 | + python python_org_search.py |
| 28 | + |
| 29 | +The `python` which you are running should have the `selenium` module |
| 30 | +installed. |
| 31 | + |
| 32 | +Walk through of the example |
| 33 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 34 | + |
| 35 | +The `selenium.webdriver` module provides all the WebDriver |
| 36 | +implementations. Currently supported WebDriver implementations are |
| 37 | +Firefox, Chrome, Ie and Remote. The `Keys` class provide keys in the |
| 38 | +keyboard like RETURN, F1, ALT etc. |
| 39 | + |
| 40 | +:: |
| 41 | + |
| 42 | + from selenium import webdriver |
| 43 | + from selenium.webdriver.common.keys import Keys |
| 44 | + |
| 45 | +Next, the instance of Firefox WebDriver is created. |
| 46 | + |
| 47 | +:: |
| 48 | + |
| 49 | + driver = webdriver.Firefox() |
| 50 | + |
| 51 | +The `driver.get` method will navigate to a page given by the URL. |
| 52 | +WebDriver will wait until the page has fully loaded (that is, the |
| 53 | +"onload" event has fired) before returning control to your test or |
| 54 | +script. It's worth noting that if your page uses a lot of AJAX on |
| 55 | +load then WebDriver may not know when it has completely loaded.:: |
| 56 | + |
| 57 | + driver.get("http://www.python.org") |
| 58 | + |
| 59 | +The next line is an assertion to confirm that title has "Python" word |
| 60 | +in it:: |
| 61 | + |
| 62 | + assert "Python" in driver.title |
| 63 | + |
| 64 | +WebDriver offers a number of ways to find elements using one of the |
| 65 | +`find_element_by_*` methods. For example, the input text element can |
| 66 | +be located by its `name` attribute using `find_element_by_name` |
| 67 | +method. Detailed explanation of findind elements is available in the |
| 68 | +:ref:`locating-elements` chapter:: |
| 69 | + |
| 70 | + elem = driver.find_element_by_name("q") |
| 71 | + |
| 72 | +Next we are sending keys, this is similar to entering keys using your |
| 73 | +keyboard. Special keys can be send using `Keys` class imported from |
| 74 | +`selenium.webdriver.common.keys`:: |
| 75 | + |
| 76 | + elem.send_keys("selenium") |
| 77 | + elem.send_keys(Keys.RETURN) |
| 78 | + |
| 79 | +After submission of the page, you should be reached in the Google |
| 80 | +site:: |
| 81 | + |
| 82 | + assert "Google" in driver.title |
| 83 | + |
| 84 | +Finally, the browser window is closed. You can also call `quit` |
| 85 | +method instead of `close`. The `quit` will exit entire browser where |
| 86 | +as `close` will close one tab, but if it just one tab, by default most |
| 87 | +browser will exit entirely.:: |
| 88 | + |
| 89 | + driver.close() |
| 90 | + |
| 91 | + |
| 92 | +Using Selenium to write tests |
| 93 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +Selenium will be used mostly for writing test cases. You can write |
| 96 | +test cases using Python's unittest module. Here is the modified |
| 97 | +example which uses unittest module. This is a test for python.org |
| 98 | +search functionality:: |
| 99 | + |
| 100 | + import unittest |
| 101 | + from selenium import webdriver |
| 102 | + from selenium.webdriver.common.keys import Keys |
| 103 | + |
| 104 | + class PythonOrgSearch(unittest.TestCase): |
| 105 | + |
| 106 | + def setUp(self): |
| 107 | + self.driver = webdriver.Firefox() |
| 108 | + |
| 109 | + def test_search_in_python_org(self): |
| 110 | + driver = self.driver |
| 111 | + driver.get("http://www.python.org") |
| 112 | + self.assertIn("Python", driver.title) |
| 113 | + elem = driver.find_element_by_name("q") |
| 114 | + elem.send_keys("selenium") |
| 115 | + elem.send_keys(Keys.RETURN) |
| 116 | + self.assertIn("Google", driver.title) |
| 117 | + |
| 118 | + def tearDown(self): |
| 119 | + self.driver.close() |
| 120 | + |
| 121 | + if __name__ == "__main__": |
| 122 | + unittest.main() |
| 123 | + |
| 124 | + |
| 125 | +You can run the above test case from a shell like this:: |
| 126 | + |
| 127 | + python test_python_org_search.py |
| 128 | + . |
| 129 | + ---------------------------------------------------------------------- |
| 130 | + Ran 1 test in 15.566s |
| 131 | + |
| 132 | + OK |
| 133 | + |
| 134 | + |
| 135 | +Walk through of the example |
| 136 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 137 | + |
| 138 | +Initially, all the basic modules required are imported. The `unittest |
| 139 | +<http://docs.python.org/library/unittest.html>`_ module is a built-in |
| 140 | +Python based on Java's JUnit. This module provides the framework for |
| 141 | +organizing the test cases. The `selenium.webdriver` module provides |
| 142 | +all the WebDriver implementations. Currently supported WebDriver |
| 143 | +implementations are Firefox, Chrome, Ie and Remote. The `Keys` class |
| 144 | +provide keys in the keyboard like RETURN, F1, ALT etc. |
| 145 | + |
| 146 | +:: |
| 147 | + |
| 148 | + import unittest |
| 149 | + from selenium import webdriver |
| 150 | + from selenium.webdriver.common.keys import Keys |
| 151 | + |
| 152 | +The test case class is inherited from `unittest.TestCase`. |
| 153 | +Inheriting from `TestCase` class is the way to tell `unittest` module |
| 154 | +that, this is a test case:: |
| 155 | + |
| 156 | + class PythonOrgSearch(unittest.TestCase): |
| 157 | + |
| 158 | + |
| 159 | +The `setUp` is part of initialization, this method will get called |
| 160 | +before every test function which you are going to write in this test |
| 161 | +case class. Here you are creating the instance of Firefox WebDriver. |
| 162 | + |
| 163 | +:: |
| 164 | + |
| 165 | + def setUp(self): |
| 166 | + self.driver = webdriver.Firefox() |
| 167 | + |
| 168 | +This is the test case method. The first line inside this method |
| 169 | +create a local reference to the driver object created in `setUp` |
| 170 | +method. |
| 171 | + |
| 172 | +:: |
| 173 | + |
| 174 | + def test_search_in_python_org(self): |
| 175 | + driver = self.driver |
| 176 | + |
| 177 | +The `driver.get` method will navigate to a page given by the URL. |
| 178 | +WebDriver will wait until the page has fully loaded (that is, the |
| 179 | +"onload" event has fired) before returning control to your test or |
| 180 | +script. It's worth noting that if your page uses a lot of AJAX on |
| 181 | +load then WebDriver may not know when it has completely loaded.:: |
| 182 | + |
| 183 | + driver.get("http://www.python.org") |
| 184 | + |
| 185 | +The next line is an assertion to confirm that title has "Python" word |
| 186 | +in it:: |
| 187 | + |
| 188 | + self.assertIn("Python", driver.title) |
| 189 | + |
| 190 | +.. note:: |
| 191 | + |
| 192 | + The `assertIn` API is only available in Python 2.7 unittest module. |
| 193 | + |
| 194 | +WebDriver offers a number of ways to find elements using one of the |
| 195 | +`find_element_by_*` methods. For example, the input text element can |
| 196 | +be located by its `name` attribute using `find_element_by_name` |
| 197 | +method. Detailed explanation of findind elements is available in the |
| 198 | +:ref:`locating-elements` chapter:: |
| 199 | + |
| 200 | + elem = driver.find_element_by_name("q") |
| 201 | + |
| 202 | +Next we are sending keys, this is similar to entering keys using your |
| 203 | +keyboard. Special keys can be send using `Keys` class imported from |
| 204 | +`selenium.webdriver.common.keys`:: |
| 205 | + |
| 206 | + elem.send_keys("selenium") |
| 207 | + elem.send_keys(Keys.RETURN) |
| 208 | + |
| 209 | +After submission of the page, you should be reached in the Google |
| 210 | +site. You can confirm it by asserting "Google" in the title:: |
| 211 | + |
| 212 | + self.assertIn("Google", driver.title) |
| 213 | + |
| 214 | +The `tearDown` method will get called after every test method. This |
| 215 | +is a place to do all cleanup actions. In the current method, the |
| 216 | +browser window is closed. You can also call `quit` method instead of |
| 217 | +`close`. The `quit` will exit all entire browser where as `close` |
| 218 | +will close one tab, but if it just one tab, by default most browser |
| 219 | +will exit entirely.:: |
| 220 | + |
| 221 | + def tearDown(self): |
| 222 | + self.driver.close() |
| 223 | + |
| 224 | +Final lines are some boiler plate code to run the test suite:: |
| 225 | + |
| 226 | + if __name__ == "__main__": |
| 227 | + unittest.main() |
| 228 | + |
| 229 | + |
| 230 | +Using Selenium with remote WebDriver |
| 231 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 232 | + |
| 233 | +To use the remote WebDriver, you should have Selenium server running. |
| 234 | +While running the Selenium server, you could see a message looks like |
| 235 | +this:: |
| 236 | + |
| 237 | + 15:43:07.541 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub |
| 238 | + |
| 239 | +The above line says that, you can use this URL for connecting to |
| 240 | +remote WebDriver. Here are some examples:: |
| 241 | + |
| 242 | + from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 243 | + |
| 244 | + driver = webdriver.Remote( |
| 245 | + command_executor='http://127.0.0.1:4444/wd/hub', |
| 246 | + desired_capabilities=DesiredCapabilities.CHROME) |
| 247 | + |
| 248 | + driver = webdriver.Remote( |
| 249 | + command_executor='http://127.0.0.1:4444/wd/hub', |
| 250 | + desired_capabilities=DesiredCapabilities.OPERA) |
| 251 | + |
| 252 | + driver = webdriver.Remote( |
| 253 | + command_executor='http://127.0.0.1:4444/wd/hub', |
| 254 | + desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS) |
| 255 | + |
| 256 | +The desired capabilities is a dictionary, so instead of using the |
| 257 | +default dictionaries, you can specifies the values explicitly:: |
| 258 | + |
| 259 | + driver = webdriver.Remote( |
| 260 | + command_executor='http://127.0.0.1:4444/wd/hub', |
| 261 | + desired_capabilities={'browserName': 'htmlunit', |
| 262 | + 'version': '2', |
| 263 | + 'javascriptEnabled': True}) |
| 264 | + |
0 commit comments