Before starting, make sure Splinter is :doc:`installed </install>`
This tutorial provides a simple example, teaching step by step how to:
- search for
splinter - python acceptance testing for web applications'in google.com, and - find if splinter official website is listed among the search results
First of all, import Browser class and instantiate it.
from splinter import Browser
browser = Browser()Note: if you don't provide any driver to Browser function, firefox will be used.
Visit any website using the browser.visit method. Let's go to Google search page:
browser.visit('http://google.com')After a page is loaded, you can perform actions, such as clicking, filling text input, checking radio and checkbox. Let's fill Google's search field with splinter - python acceptance testing for web applications:
browser.fill('q', 'splinter - python acceptance testing for web applications')Tell Splinter which button should be pressed. A button - or any other element - can be identified using its css, xpath, id, tag or name.
In order to find Google's search button, do:
button = browser.find_by_name('btnG')Note that this btnG was found looking at Google's page source code.
With the button in hands, we can then press it:
button.click()Note: Both steps presented above could be joined in a single line, such as:
browser.find_by_name('btnG').click()After pressing the button, you can check if Splinter official website is among the search responses. This can be done like this:
if browser.is_text_present('splinter.readthedocs.io'):
print "Yes, found it! :)"
else:
print "No, didn't find it :("In this case, we are just printing something. You might use assertions, if you're writing tests.
When you've finished testing, close your browser using browser.quit:
browser.quit()Finally, the source code will be:
from splinter import Browser
browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'splinter - python acceptance testing for web applications')
browser.find_by_name('btnG').click()
if browser.is_text_present('splinter.readthedocs.io'):
print "Yes, the official website was found!"
else:
print "No, it wasn't found... We need to improve our SEO techniques"
browser.quit()