Use python for automated testing-automated testing on the server (3)-more http client instances and pythonclient

Source: Internet
Author: User
Tags keep alive

Use python for automated testing-automated testing on the server (3)-more http client instances and pythonclient

In the previous article, we only implemented a simple http client function. The request also provides various functions such as keep alive, SSL, multi-File Upload, cookie management, and http requests header management, all functions are supported in requests as long as your browser implements them.

#! /Usr/bin/env python # coding = utf-8import requestsdef login_douban (username, passwd): post_data = {'source': 'index _ nav', 'form _ e-mail ': username, 'form _ password': passwd} request_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv: 30.0) gecko/20100101 Firefox/30.0 "} response = requests. post ("http://www.douban.com/accounts/login", data = post_data, headers = request_headers) if u "" in response. text: print "Login successful" return response else: print "Login failed" print response. text return Falsedef say_something (login_cookie): post_data = {'ck ': 'ynnl', 'Rev _ title': u'payby', 'Rev _ text ': u'landlord is the title', 'Rev _ submit ': U' okay, speak'} response = requests. post ("http://www.douban.com/group/beijing/new_topic", data = post_data, cookies = login_cookie) if response. url = "http://www.douban.com/group/beijing/": print "post new content successfully" return True else: print "Post content fail" return success = login_douban (your_usename, your_passwd) say_something (login_response.cookies)

Request_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv: 30.0) Gecko/20100101 Firefox/30.0 "}, the purpose of this line is to simulate the request sent by FireFox. Many websites use the User-Agent field to block crawlers, of course, large websites should use more advanced methods to block crawlers. If this User-Agent is not set, requests will send a request, the User-Agent value is python-requests/2.3.0 CPython/2.7.3 Windows/7 \ r \ n.


The say_something function has not been tested. I have been debugging frequently just now. Douban asked me to enter the login verification code. If you have any questions, I will leave a message and I will debug it later.


For cookie and session management, if the request is in the same function, the session is automatically managed and no additional processing is required,

session = requests.Session()session.post("http://www.douban.com/accounts/login", data=post_data,headers=request_headers)session.post("http://www.douban.com/group/beijing/new_topic", data=post_data)

In this way, you can post successfully.


Here we will surely think of selenium, isn't it the same as requests? Requests is better at testing without UI interfaces, and selenium is better at web Testing with UI.


I am a beginner in Python and want to develop an automated mobile phone test script. I want to know a few questions.

1. Can Python be automatically tested on mobile phones? Yes.
2. Which tutorial will be better if you want to learn? First, learn automated testing, then learn python, and then learn it with instances. For more information, see wenku.baidu.com/...1.html.
3. What are the main modules to be learned?
In the world of Python, there is an open-source framework Splinter that can simulate the behavior of browsers very well (in a sense, it can also be said that it is a human access click behavior ). Splinter provides a variety of APIs to obtain page information to determine the results of the current behavior

4. if you understand this line, I will add additional points .. Multiple
Learn more about all aspects of testing. python is just a tool. The theoretical knowledge of testing is very important.

Want to learn web automation testing, ruby? Python? Javascript? The web automation language scripting language recommended by all heroes.

Automated web testing:
Commercial tools:
The QTP language is vbscript.
Open Source:
Selenium supports ruby and python
Ruby also has watir and python has PAMIE, but watir is better than PAMIE.
What to learn depends on the big environment:
What do companies learn when using tools for testing?
If you are looking for a job, search for "test ruby" and "test python" on the job-seeking website. I will learn more about this job. Haha
In addition, javascript is required for web Front-end testing, and more or less ~

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.