Introduction and use of Python automated testing tool Splinter

Source: Internet
Author: User
This article describes the introduction and use of the Python automated testing tool Splinter. Splinter can simulate browser behavior very well. Splinter provides a wide range of APIs, get the page information to determine the results of the current behavior Splinter quick introduction

Http://splinter.cobrateam.info/

Official introduction:

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

Features:

1. Simulate browser behavior, access the specified URL, and specify different browser types. For example, firefox or chrome. Different browsers can be accessed by specifying the name in the Code by installing the corresponding driver locally.
2. Supports cookie operations to conveniently add and delete cookies;
3. Supports simulating mouse actions, such as moving to a button and leaving the focus of a button. For pages with dynamic prompts, such as dynamic prompts In the keyword input box of the search engine, it can be easily tested.
4. Supports simulating input operations on the keyboard. Inputting input controls such as input can simulate the user's type process.
5. support directly running js or calling js on the page.
6. Supports file upload simulation.
7. dedicated api support for radio and checkbox, which is very convenient;
8. quick retrieval of page elements or determination of existence of text are supported. It is very convenient for developers to determine whether the page prompt information is accurate.
9. Most importantly, splinter's API is very simple. The cost of learning with official documents is almost zero. Of course, you must understand some python syntax. If you are familiar with js and css, you may like jquery;

Function:

When Splinter is executed, the browser you specified is automatically opened to access the specified URL.
Then, any simulated behavior you develop will be automatically completed. You just need to sit in front of your computer and watch the various actions on the screen like watching a movie and then collect the results.


For example, to return to the logon function, we must first develop the script to simulate logon behavior as follows:

The Code is as follows:


#! /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 ('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 ("test page:" + browser. title)

Try:
# Test login
TestLogin ('test User Name Not entered ', '','', 'enter member name ')
TestLogin ('test without password entered ', 'qd _ test_001', '', 'enter password ')
TestLogin ('test account does not exist', 'This is a nonexistent name', 'xxxxxxx', 'this account name does not exist ')
TestLogin ('logon successful test', 'qd _ test_001 ', 'taobao1234', 'Operation before logon ')

# Test find password
Output ("test [retrieve Password] Link ")
Browser. visit (_ testUrl)
BackPasswordLink = browser. find_link_by_text ('retrieve 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 ("failed to Test password retrieval ")

Except t Exception, x:
Print x

If CLOASE_AFTER_TEST:
Browser. quit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.