跳到主要内容

95 篇博文 含有标签「python」

查看所有标签

在中文windows下使用pywinauto进行窗口操作(一)

· 阅读需 6 分钟

这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官方的文档,发现这个东西使用起来还真是非常方便,下面我也以notepad为例来说明一下它的简单操作。

pywinauto;

https://sourceforge.net/project/showfiles.php?group_id=157379

Sendkeys

http://www.rutherfurd.net/python/sendkeys/index.html

ctypes (如果你是python2.3或者2.4)

检测你是否安装正确

from pywinauto import application app = application.Application.start("notepad.exe") app.notepad.TypeKeys("%FX")

使用python进行windows自动化测试(1)

· 阅读需 6 分钟

最近开始学习整理python在windows自动化测试中的使用,觉得挺有意思的

主要思路,在windows下,主要通过启进程,然后查找进程的句柄,然后再操作这个句柄,包括点击,填写文字,关闭,获取文字等操作

下面以一个简单的校验文件md5值的操作来介绍一个python的应用,当然python中有校验md5的函数,不用非要使用工具来校验,这里只是练习使用python来自动化操作

所用的工具有SpyLite,用于查看窗口ID,句柄等信息

Python中的urllib2库的使用

· 阅读需 4 分钟

今天研究了下urllib2这个库的使用,才发现以前有很多不明白的东西,现在写下来也做个记录

import urllib2
url = r'http://www.baidu.com'
html = urllib2.urlopen(url).read()

客户端与服务器端通过request与response来沟通,客户端先向服务端发送request,然后接收服务端返回的response

urllib2提供了request的类,可以让用户在发送请求前先构造一个request的对象,然后通过urllib2.urlopen方法来发送请求

import urllib2
url = r'http://www.baidu.com'
req = urllib2.Request(url)
html = urllib2.urlopen(req).read()

python解析json

· 阅读需 5 分钟

相对于python解析XML来说,我还是比较喜欢json的格式返回,现在一般的api返回都会有json与XML格式的选择,json的解析起来个人觉得相对简单些

先看一个简单的豆瓣的图书查询的api返回

Python解析XML文档

· 阅读需 6 分钟

解析XML主要用到pytohn自带的XML库,其次还是lxml库

先以一个相对简单但功能比较全的XML文档为例

<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xml:lang='en'>
<title>dive into mark</title>
<subtitle>currently between addictions</subtitle>
<id>tag:diveintomark.org,2001-07-29:/</id>
<updated>2009-03-27T21:56:07Z</updated>
<link rel='alternate' type='text/html' href='http://diveintomark.org/'/>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Dive into history, 2009 edition</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'/>
<id>tag:diveintomark.org,2009-03-27:/archives/20090327172042</id>
<updated>2009-03-27T21:56:07Z</updated>
<published>2009-03-27T17:20:42Z</published>
<category scheme='http://diveintomark.org' term='diveintopython'/>
<category scheme='http://diveintomark.org' term='docbook'/>
<category scheme='http://diveintomark.org' term='html'/>
<summary type='html'>Putting an entire chapter on one page sounds
bloated, but consider this &mdash; my longest chapter so far
would be 75 printed pages, and it loads in under 5 seconds&hellip;
On dialup.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Accessibility is a harsh mistress</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/21/accessibility-is-a-harsh-mistress'/>
<id>tag:diveintomark.org,2009-03-21:/archives/20090321200928</id>
<updated>2009-03-22T01:05:37Z</updated>
<published>2009-03-21T20:09:28Z</published>
<category scheme='http://diveintomark.org' term='accessibility'/>
<summary type='html'>The accessibility orthodoxy does not permit people to
question the value of features that are rarely useful and rarely used.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
</author>
<title>A gentle introduction to video encoding, part 1: container formats</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2008/12/18/give-part-1-container-formats'/>
<id>tag:diveintomark.org,2008-12-18:/archives/20081218155422</id>
<updated>2009-01-11T19:39:22Z</updated>
<published>2008-12-18T15:54:22Z</published>
<category scheme='http://diveintomark.org' term='asf'/>
<category scheme='http://diveintomark.org' term='avi'/>
<category scheme='http://diveintomark.org' term='encoding'/>
<category scheme='http://diveintomark.org' term='flv'/>
<category scheme='http://diveintomark.org' term='GIVE'/>
<category scheme='http://diveintomark.org' term='mp4'/>
<category scheme='http://diveintomark.org' term='ogg'/>
<category scheme='http://diveintomark.org' term='video'/>
<summary type='html'>These notes will eventually become part of a
tech talk on video encoding.</summary>
</entry>
</feed>

先简单的看一下这个XML的结构

python发邮件是件很简单的事情

· 阅读需 1 分钟

利用python的smtplib,发邮件将是一件非常简单的事情,下文以用163邮箱来发邮件为例,说明smtplib的应用

#coding:utf-8
import smtplib

def sendMail(mail_to):
mail_server = 'smtp.163.com'
mail_port = '25'
username = 'soar_1987@163.com'
password = 'XXXXXXXX'
mail_title = 'python Test'
mail_content = 'This is a test from python for sending email'
if type(mail_to) == str:#之所以要有这样的判断是为了收件人是多个人或者传入的的收件人列表是以list的方式
mail_list = mail_to.split(';') #将str类型的数据转换为list
elif type(mail_to) == list:
mail_list = mail_to
else:
print "你输入的收件人格式有误"

try:
handle = smtplib.SMTP(mail_server,mail_port)
handle.login(username,password)
msg = "From:%srn To:%srnContent-Type: text/html;charset=gb2312rnSubject:%srnrn %s"%("杨彦星",";".join(mail_list),mail_title,mail_content) #这里的msg其实就是一种固定的格式,From:To:Subject
handle.sendmail(username,mail_list,msg)
print "Send email sucess"
except Exception,e:
print "Send email failed because %s" % e

if __name__ =="__main__":
sendMail('yanxingyang@gmail.com')

可爱的python中的一个与递归相关的小问题

· 阅读需 2 分钟

可爱的python一书中有一个练习题,是在一个目录中查找特定扩展名的文件,并且读取里面的内容,然后用户输入一个关键词,在这些文件中进行搜索,如果找到后就把这一行内容打印出来,他提出的问题是如果里面还有目录,目录里面还有更深的目录,解决这个问题,我想到的只能是递归。。。

#coding:utf-8
import os,sys

def cdGrep(keyword,filepath):
filelist = os.listdir(filepath)

for cdc in filelist:
if os.path.isdir(cdc):
filepath2 = os.path.join(filepath,cdc)
cdGrep(keyword,filepath2)

elif '.txt' in cdc:
# print filepath+cdc
f = open(filepath+''+ cdc)
for line in f.readlines():
linelower = line.lower()
if keyword in linelower:

print '您查找的关键词在%s中找到'% (filepath+''+cdc)
print line
f.close()

if __name__ == '__main__':
keword = raw_input('请输入想要查询的关键字')
pw = os.getcwd()
cdGrep(keword,pw)

通过一个python小脚本来下载最新的360杀毒安装程序

· 阅读需 1 分钟

小脚本很简单,主要是练习一下正则匹配,与简单的urllib库的应用

#coding: utf8
import urllib,re
import os

def getLastinstall():

page = urllib.urlopen(r'http://sd.360.cn/download_center.html')
html = page.read()
page.close()
rematch = r'http://down.360.cn/360sd/360sd_se_(.*?)exe'
replusmatch = r'http://down.360.cn/360sd/360sd_plus_(.*?)exe'
stdname = '360sd_'+(str(re.compile(rematch).findall(html)[0]))+'exe'
downurlstd = r'http://down.360.cn/360sd/360sd_'+(str(re.compile(rematch).findall(html)[0]))+'exe'
powname = '360sd_plus_'+(str(re.compile(replusmatch).findall(html)[0]))+'exe'
downurlpow = r'http://down.360.cn/360sd/360sd_plus_'+(str(re.compile(replusmatch).findall(html)[0]))+'exe'
localpath = os.getcwd()+r'/installpack'
if not (os.path.isfile(stdname) and os.path.isfile(powname)):
if not os.path.isdir('installpack'):
os.makedirs('installpack')
urllib.urlretrieve(downurlstd,localpath+'/'+stdname)
urllib.urlretrieve(downurlpow,localpath+'/'+powname)
else:
print '目录中已经是线上最新版杀毒安装程序'

if __name__ =='__main__':
getLastinstall()