Splinter the Python automation test tool introduces and USES examples

  • 2020-04-02 13:38:23
  • OfStack

Splinter quick introduction

The official website: http://splinter.cobrateam.info/

Official introduction:

Splinter is an open source tool for testingweb applications using Python. It lets you automate browser actions, to asvisiting URLs and interacting with their items

Features:

1. You can simulate browser behavior, access specific urls, and specify different browser types. Like firefox or chrome. Different browsers can be accessed by name in the code as long as the corresponding driver is installed locally.
2, support cookie operation, can be very convenient to add and delete cookies;
3, support simulation mouse action, such as sliding to a button, the focus away from a button and so on, for the page with dynamic prompt, such as search engine keyword input box dynamic prompt, can be very convenient to test.
4, support analog keyboard input operation, input to the input control can simulate the user's type process.
5, support to directly run js or call the page of js.
6. Support simulated file upload.
7. Convenient API support for radio and checkbox;
8, support to quickly get the page elements or determine whether there is text, for the development of page prompt information is accurate very convenient.
9. Most importantly, splinter's API is very simple. The cost of learning to match the official documentation is almost zero. If you know more about js and CSS, you might like it as much as you like jquery.

Function:

Splinter will automatically open the browser you specified and access the URL you specified.
And then any behavior that you develop in the simulation, it's automatically done, you just sit in front of the computer and watch the actions on the screen as if you were watching a movie and then collect the results.


For example, to return to the login function, we first develop the following script to simulate the login behavior:


#!/usr/bin/py2
# -*- coding: utf-8 -*-
#encoding=utf-8
import sys, re
from splinter.browser import Browser  
CLOASE_AFTER_TEST = False
reload(sys)
sys.setdefaultencoding('utf8')
encoding = lambda x:x.encode('gbk')  
def testLogin(desc, username, password, result):
    output(desc)      
    browser.fill('TPL_username',username.decode('utf8'))
    browser.fill('TPL_password',password.decode('utf8'))
    browser.find_by_value(' The login ').first.click()
    checkresult(result)  
def output(x):
    print encoding(x)  
def resultMsg(x):
    if x == True:
        print 'pass'
    else:
        print '[X]not pass'
def checkresult(x):
    """  check result message, x : the error message u want  """
    resultMsg(browser.is_text_present(x))  
__testUrl = 'http://waptest.taobao.com/login/login.htm?tpl_redirect_url=http%3A%2F%2Fm.taobao.com%2F'  
# chrome driver : http://code.google.com/p/selenium/wiki/ChromeDriver
browser = Browser()  # already support firefox
browser.visit(__testUrl)
output(" The test page :"+browser.title)  
try:
    # test login
    testLogin(' The test did not enter a user name ','','',' Please enter the member name ')
    testLogin(' Test no password entered ','qd_test_001','',' Please enter your password ')
    testLogin(' The test account does not exist ',' That's a name that doesn't exist ','xxxxxxx',' This account name does not exist ')
    testLogin(' Test successful login ','qd_test_001','taobao1234',' Continue the pre-login operation ')  
    # test find password
    output(" test [ Retrieve password ] link ")
    browser.visit(__testUrl)
    backPasswordLink = browser.find_link_by_text(' Retrieve the password ')
    if 1 == len(backPasswordLink):
        backPasswordLink.first.click()
        ru = re.findall(re.compile(".*(reg/gp.htm).*", re.IGNORECASE), browser.url)
        if ru is not None:
            checkresult(' Retrieve password ')
        else:
            output(" The test to retrieve the password link failed ")  
except Exception,x:
    print x  
if CLOASE_AFTER_TEST:
    browser.quit()



Related articles: