To create a WebDriver browser instance:
from webdriverplus import WebDriver
browser = WebDriver('firefox')
Currently supported browsers are firefox, chrome, ie, htmlunit, remote and phantomjs.
The default browser is firefox. If called without any arguments, WebDriver() will create a firefox instance.
browser = WebDriver()
Because starting up a web browser instance on every test run can be a significant performance hit, WebDriverPlus provides an easy way to allows instances to be reused between test sessions.
Setting the reuse_browser flag ensures that when you call driver.quit() the browser will be returned to a browser pool, and reused when you create a new WebBrowserInstance:
browser = WebDriver('firefox', reuse_browser=True)
There are some important aspects to bear in mind about this behaviour:
By default WebDriverPlus will ensure that when a python process quits it will attempt to quit any remaining open WebDriver instances.
If you do not want this behaviour set quit_on_exit to False:
browser = WebDriver('firefox', quit_on_exit=False)
By default WebDriverPlus will not wait for elements to become available. You can pass a wait argument to specify the number of seconds that find() should wait for elements to become available before timing out.
browser = WebDriver('firefox', wait=10)
This uses WebDriverWait under the covers, but is much less verbose. The idea behind setting a per-browser wait argument instead of forcing the programmer to use WebDriverWait around each piece of code that needs to wait for an element is to free the programmer from having to think about waiting, which we consider a low-level detail that the framework should deal with.
By default WebDriverPlus highlights elements it found. Setting highlight to falsy will disable this
To switch to a specific iframe, call switch_to_frame()
Right now webdriverplus supports expected condition style waiting with wait_for(selector, displayed=True. By default it will wait until the element with selector to be present AND visible. If displayed is set to False, it will only wait until element is present.
browser.wait_for('div')
To quit a WebDriver browser instance, call quit():
browser.quit()
Setting the force flag causes the browser instance to quit and ignore the value of the reuse_browser flag. The instance will be terminated and will not be returned to the browser pool:
browser.quit(force=True)
Using pyvirtualdisplay, you can run real browser instances in a virtual X framebuffer or VNC session. This enables you to run Firefox or Chrome tests in headless mode, without having to install HTMLUnit.
$ pip install pyvirtualdisplay
You need to install either Xvfb or Xvnc as a backend for pyvirtualdisplay.
To run the headless tests, use the --headless argument:
$ python runtests.py --headless
Running tests in headless mode.
.........................................................
----------------------------------------------------------------------
Ran 57 tests in 7.715s
OK