preface

Splinter is an open source web test automation toolkit developed in Python. It helps you automate browser behavior, such as browsing URLs and interacting with pages.

Quick learning

The installation

Running from a terminal:

pip install splinterCopy the code

Install the appropriate browser driver

Chrome driver: https://sites.google.com/a/chromium.org/chromedriver/downloads Firefox browser drive: https://github.com/mozilla/geckodriver/releases according to the corresponding browser to download the corresponding driver version, and add the driver’s path environment variable.

Create a Browser instance

From splinter import Browser Browser = Browser() # driver_name= Browser(driver_name='chrome')Copy the code

Tip: If you do not specify driver for Browser, firefox will be used by default.

Visit the Baidu search page

Use the browser.visit method to access any website. Let’s visit baidu search page:

browser.visit('http://baidu.com')Copy the code

Enter search terms

Once the page is loaded, you can perform a series of interactions, such as clicking, filling fields with input fields, and selecting radio buttons and check boxes. Let’s fill splinter-Python acceptance testing for Web Applications in baidu search box.

browser.fill('wd', 'splinter - python acceptance testing for web applications')Copy the code

Click the search button

Tell Splinter which button to click. This button – or any other element – can be identified by its CSS, xpath, ID, tag, or name.

Find the Baidu search button by doing the following:

button = browser.find_by_xpath('//input[@type="submit"]')Copy the code

Tip: the button with this xpath syntax will be found in the source code of the Baidu search page.

After finding the button, we can click on it:

button.click()Copy the code

Tip: The two steps shown above can be combined into one line of code, as follows:

browser.find_by_xpath('//input[@type="submit"]').click()Copy the code

Check to see if Splinter’s official website is in the search results

After clicking the search button, you can follow these steps to check if the official Splinter website is in your search results.

if browser.is_text_present('splinter.readthedocs'):
    print "Yes, found it! :)"
else:
    print "No, didn't find it :("Copy the code

In this little example, we just printed out the result. When writing tests, you need to use assertions.

Close the browser

After finishing the test, we need to close the browser with browser.quit:

browser.quit()Copy the code

conclusion

The final complete code looks like this:

from splinter import Browser

browser = Browser()
browser.visit('http://baidu.com')
browser.fill('wd', 'splinter - python acceptance testing for web applications')
button = browser.find_by_xpath('//input[@type="submit"]').click()

if browser.is_text_present('splinter.readthedocs'):
    print "Yes, the official website was found!"
else:
    print "No, it wasn't found... We need to improve our SEO techniques"

browser.quit()Copy the code

Translation program

The above introduction is from the quick start part of Splinter Chinese document I translated recently. Thank you very much

@ a leaf dyed autumn

Splinter Chinese document project

eggs

People used Phantomjs to implement headless in the early days, but there were some drawbacks. Both Chrome and Firefox now offer headless modes, and Splinter is integrated.

from splinter import Browser

browser = Browser(driver_name='chrome', headless=True)
# browser = Browser(driver_name='firefox', headless=True)Copy the code

Headless mode can be enabled with a simple parameter, so go for it! (Ps: I did a little work for this feature)

reference

  1. Splinter official document
  2. Splinter Chinese document