Splinter is an open source tool for testing web applications using Python. Splinter provides easy to use functions to access the elements in a web page. We use Splinter for testing web applications involving user flows.
Splinter’s Easy to use APIs
Simple APIs – Splinter provides simple APIs to visit URLs and access elements in the web page. browser.visit(“https://saisyam.com”) will load the web page in the default driver, Firefox. Below are some of the functions that will help in accessing elements in the page:
browser.find_by_css('h1') browser.find_by_xpath('//h1') browser.find_by_tag('h1') browser.find_by_name('name') browser.find_by_text('Hello World!') browser.find_by_id('firstheader') browser.find_by_value('query')
Multiple Driver Support – Supports multiple web drivers, along with Flask and Django. We can test Flask or Django applications using Splinter. Below is a simple example of using Flask driver:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello, World!' @app.errorhandler(404) def page_not_found(e): return 'Oops! 404', 404 from splinter import Browser browser = Browser("flask", app=app) browser.visit("http://localhost:5000") assert "Hello, World!" == browser.html browser.visit("http://localhost:5000/users") assert "Oops! 404" == browser.html
Filling forms – filling forms made easy in Splinter with browser.fill() API.
Simple example using Splinter
from splinter import Browser url = "https://saisyam.com" browser = Browser() browser.visit(url) assert browser.title == "Saisyam – Personal Blog" browser.quit()
The above example simply loads my website and check the title. The default web driver is Firefox. If we want to use other drivers like, chrome, we will pass it as an argument to the Browser class as Browser(“chrome”). We can pass chrome options to the Browser class with options variable as Browser(“chrome”, options=chrome_options) where chrome_options is webdriver.ChromeOptions().
Accessing Web driver object
Splinter’s Browser class wraps the web driver object. You can access the driver object through browser.driver. We saw few issues with Splinter screenshot APIs (browser.screenshot) like, Splinter was adding a random string at the end of the provided file name and the screenshot captured was not complete. We used driver’s default screenshot API (driver.save_screenshot) as a work around. Our tests mostly run on Servers deployed on cloud services like AWS. It seems Splinter screenshot API has some issues with driver’s headless mode and not working on server environments. My Github repo web-automation-testing provides examples on using Splinter with various drivers and options.
Splinter makes testing web applications easier with least learning curve and less code. With few work arounds and tweaks as mentioned above, we were able to get the best out of it. Try it and provide your comments. Thanks for reading.