Snake's Home

用selenium实现的流程自动化

这是在某厂负责的第一个项目,是公司内部做的一个OA系统,主要是各种流程,请假,审批等等。
开发起来并不复杂,但是流程特别多,业务比较复杂,项目还没结束的时候,团队人都走没了。

当时测试时间被压缩得很紧,基本功能没啥问题后,就写了个脚本来覆盖各个流程。
这也是这个公司的第一个自动化测试项目和敏捷项目。
虽然功劳都被人拿走了,但是这个项目还是很成功的。

auto.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#encoding:utf-8
# coding:gb2312
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
import time,re,sys,os
import csv

class OAtest():
currentpath=os.getcwd()
logpath=currentpath+"\\Log"
casepath=currentpath+"\\Case"
tablepath=currentpath+"\\Table"
if not os.path.exists(logpath) :
os.mkdir("Log")
print logpath
logname = time.strftime(r"%Y-%m-%d_%H-%M",time.localtime())
print logname
filename=logpath + "\\" + logname+"log.txt"
print filename
logline=open(filename,"w+")
#logline.write('If you are happy')

def setUp(self):
self.dr = webdriver.Chrome()
#self.dr = webdriver.Ie()
self.url = 'http://10.21.168.170/index'
print u"现在开始登陆..."
self.logline.write("现在开始登陆...\n")
def login(self,name):
print "Now Logging is %s" %(name)
self.logline.write("现在登陆的账号是 %s \n" %(name))
dr = self.dr
dr.get(self.url)
time.sleep(1)
dr.find_element_by_id('UserForm_username').send_keys(name)
dr.find_element_by_id('UserForm_password').send_keys('1')
dr.find_element_by_name('yt0').click()
# errormsg = dr.find_element_by_class_name("errorSummary")
#if errormsg.is_displayed() == True:
# print u"用户名密码不匹配,请检查用户名或密码!\n"
time.sleep(1)
dr.maximize_window()


def creatleaveprogress(self,mingzi):
dr = self.dr
frame = dr.find_element_by_xpath("/html/frameset/frameset").find_element_by_tag_name("frame")
dr.switch_to_frame(frame)
print u"现在进入流程"
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[1]/a').click()
self.logline.write("现在进入新建流程...\n")
print u"现在进入新建流程"
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[3]/p[1]/a').click()
print u"点中了新建流程按钮"
time.sleep(1)
dr.switch_to_default_content()
# dr.switch_to_frame("Main")
frame1 = dr.find_element_by_xpath("/html/frameset/frameset/frameset").find_element_by_tag_name("frame")
dr.switch_to_frame(frame1)
print u"转入到流程页面"
self.logline.write("转入到流程页面...\n")
# element=dr.find_element_by_xpath('/html/body/div/div/div[2]/ul[3]/li[3]/p[1]/a')
#开始读table表了
filelist=os.listdir(self.tablepath)
print filelist
print mingzi
rule=re.compile(mingzi)
filelist1=",".join(filelist)
if rule.findall(filelist1):
print rule.findall(filelist1)
print u"有这张表"
else:
print u"找不到张表"
#my_file= 'C:\\Users\\Lenovo\\Desktop\\aa\\aa.csv'
myfile = self.tablepath + "\\"+mingzi + ".csv"
print myfile
data=csv.reader(file(myfile,'rb'))
print data
#for user in data:
# print user
# table2 = list(data)
i=0
for table1 in data:
if i==0:
element=dr.find_element_by_xpath(table1[0])
print u"打开创建流程页面"
element.click()
print u"现在申请的是:%s"%(element.text)
print "title of current page is %s" %(dr.title)
print "url of current page is %s" %(dr.current_url)
else:
dr.switch_to_default_content()
# now_handle = dr.current_window_handle
for handle in dr.window_handles:
dr.switch_to_window(handle)
print "title of current page is %s" %(dr.title)
print "url of current page is %s" %(dr.current_url)
# print "url of current page is %s" %(dr.current_window_handle)
#for table1 in data:
# print table1
print "The value is %s,%s"%(table1[0],table1[1])
dr.find_element_by_xpath(table1[0]).send_keys(table1[1])
i=i+1
title = dr.find_element_by_xpath('//table/tbody/tr[1]/td[2]/div').text
print "The title is %s \n" %title
dr.find_element_by_id('submit_request').click()
currenttime = time.strftime(r"%Y-%m-%d_%H:%M:%S",time.localtime())
print "Time is %s"%currenttime
# dr.get_screenshot_as_file('baidu.png')
time.sleep(1)
try:
alert = dr.switch_to_alert()
alert.accept()
except:
print 'no alerts display'

#filename = title + currenttime
return title

def banjie(self,biaoti,flag=1):
dr = self.dr
print u"传过来的参数是%s"%biaoti
print u"现在flag的值是%s"%flag
frame = dr.find_element_by_xpath("/html/frameset/frameset").find_element_by_tag_name("frame")
dr.switch_to_frame(frame)
print u"现在进入流程"
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[1]/a').click()
if flag==0:
print u"现在进入我的待办"
self.logline.write("现在进入我的待办...\n")
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[3]/p[2]/a').click()
else:
print u"现在进入我的办结"
self.logline.write("现在进入我的办结...\n")
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[3]/p[4]/a').click()
print u"点中"
time.sleep(1)
dr.switch_to_default_content()
# dr.switch_to_frame("Main")
frame1 = dr.find_element_by_xpath("/html/frameset/frameset/frameset").find_element_by_tag_name("frame")
dr.switch_to_frame(frame1)
print u"转入到我的办结页面"
self.logline.write("转入到我的办结页面...\n")
# dr.find_element_by_xpath('/html/body/div/div/div[2]/ul[3]/li[3]/p[1]/a').click()

xiaobiaoti = dr.find_element_by_xpath('/html/body/div/div/table/tbody/tr[1]/td[4]/a')
# assert(biaoti,xiaobiaoti.text)
pngname1=self.logpath+"\\"+self.logname+biaoti+"progress.png"
pngname2=self.logpath+"\\"+self.logname+biaoti+"detail.png"
if (biaoti == xiaobiaoti.text):
print u"找到了 %s" %biaoti
xiaobiaoti.click()
print u"打开请求 %s" %biaoti
for handle in dr.window_handles:
dr.switch_to_window(handle)
time.sleep(1)
dr.get_screenshot_as_file(pngname2)
dr.close()
else:
print u"没有找到 %s,请查看你的设置或流程,谢谢!\n" %biaoti
#xiaobiaoti.click()
for handle in dr.window_handles:
dr.switch_to_window(handle)
dr.get_screenshot_as_file(pngname1)
time.sleep(2)

def approveleaveprogress(self,biaoti,flag=1):
dr = self.dr
print u"传过来的参数是%s"%biaoti
print u"现在flag的值是%s"%flag
frame = dr.find_element_by_xpath("/html/frameset/frameset").find_element_by_tag_name("frame")
# driver.switch_to_frame(frame)
dr.switch_to_frame(frame)
# dr.switch_to_frame("Left")
print u"现在进入frame"
# add = driver.find_element_by_css_selector('input.add')
# SearchButton = driver.find_element_by_css_selector('input.add1')
# progressbutton = dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[2]/div[1]/a')
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[3]/div[1]/a').click()
time.sleep(1)
print u"现在进入事宜"
dr.find_element_by_xpath('/html/body/div[1]/div/ul/li[3]/div[3]/p[1]/a').click()
time.sleep(1)
print u"打开了待办事宜"
self.logline.write("打开了待办事宜...\n")
dr.switch_to_default_content()
frame1 = dr.find_element_by_xpath("/html/frameset/frameset/frameset").find_element_by_tag_name("frame")
# driver.switch_to_frame(frame)
dr.switch_to_frame(frame1)
print u"现在进入到 frame11"
self.logline.write("现在进入到 frame11...\n")
# inputs = dr.find_elements_by_tag_name('td')
# for input in inputs:
# if input.text == 'biaoti':
# print"Find the progress"
# input.click()
# break
#dr.find_element_by_xpath('/html/body/div/div/table/tbody/tr[1]/td[3]/a').click()
xiaobiaoti = dr.find_element_by_xpath('/html/body/div/div/table/tbody/tr[1]/td[3]/a')
# assert(biaoti,xiaobiaoti.text)
if (biaoti == xiaobiaoti.text):
print u"在待办事宜中找到了 %s" %biaoti
xiaobiaoti.click()
print u"打开请求 %s" %biaoti
else:
print u"在待办事宜中没找到 %s,请查看你的设置或流程,谢谢!\n" %biaoti
#xiaobiaoti.click()
time.sleep(3)
print "Now I open the page"
print "title of current page is %s" %(dr.title)
print "url of current page is %s" %(dr.current_url)
for handle in dr.window_handles:
dr.switch_to_window(handle)
print "title of current page is %s" %(dr.title)
print "url of current page is %s" %(dr.current_url)
print "url of current page is %s" %(dr.current_window_handle)
if flag==0:
dr.find_element_by_id('submit_back').click()
print u"请求%s被退回"%(biaoti)
else:
dr.find_element_by_id('submit_request').click()
print u"批准请求%s"%(biaoti)
time.sleep(1)
try:
alert = dr.switch_to_alert()
alert.accept()
except:
print 'no alerts display'

#def rejectprogress(self):


def tearDown(self):
self.dr.quit()



#if __name__ == "__main__":
# unittest.main()

t = OAtest()

print 'now start to create a request'
t.setUp()

t.login(10000386)
ss=t.creatleaveprogress()
print "return value is %s" %ss
t.tearDown()

time.sleep(5)

print 'now start to approve a request'
t.setUp()
t.login(10000410)
t.approveleaveprogress(ss)
t.tearDown()

leavetime = dr.find_element_by_xpath('//table/tbody/tr[6]/td[2]/div/input')
backtime = dr.find_element_by_xpath('//table/tbody/tr[7]/td[2]/div/input')
leaveday = dr.find_element_by_xpath('//table/tbody/tr[11]/td[2]/div/input')
leavereason = dr.find_element_by_xpath('//table/tbody/tr[12]/td[2]/div/textarea')



leavetime.send_keys("2014-07-09 00:00:00")
backtime.send_keys("2014-07-10 00:00:00")
leaveday.send_keys("1")
leavereason.send_keys(u"回家")

path.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#encode=utf-8
import os,sys,re

a=os.getcwd()+"\\Table"
print a
list=os.listdir(a)
print list
print type(list)
arry=",".join(list)
print arry
print type(arry)
r=r'\w+\.csv'
a=re.compile(r)
print a
list=a.findall(arry)
print list

for i in list:
print i
i=i.split(".")
print i[0]

把case写到一个文件里面,维护起来方便。

reader.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#coding=utf-8
import csv
import re,sys,os

my_file= 'C:\\Users\\Lenovo\\Desktop\\aa\\dd.csv'
typelist =[]
data=csv.reader(file(my_file,'rb'))
i = 0
for user in data:
print user
typelist.append(user[0])
i=i+1
print typelist
typeli="".join(typelist)
print i+1

#yourfile='C:\\Users\\Lenovo\\Desktop\\aa\\dd.txt'
#data=csv.reader(file(yourfile,'rb'))
source=open(r"C:\\Users\\Lenovo\\Desktop\\aa\\dd.txt","r")
data=source.readlines()
source.close()
j=0
k=0
for pro in data:
print "the list is %s" %pro
# pro="".join(pro)
#print "the list change to %s" %pro
pro=pro.strip("\n")
index=pro.find(':')
print index
pro=pro.split(':')
head=pro[0]
print head
tail=pro[1]
print tail
#r=r'\d'
rule=re.compile(r'^\D')
#print rule.findall(head)
if rule.findall(head):
if re.findall(head,typeli):
print u"在csv文件中有匹配"
print u"行号是"
j=j+1
#登陆等操作
#创建新表
else:
print u"请新建流程的表路径,或者检查你的类型"
else:
print "The case %s is skip " %head
k=k+1
print u"跑过的case %d" %j
print u"跳过的case %d" %k

弄成一个可执行的工具,这样就可以脱离python环境。

setup.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#-*-coding: UTF-8-*-
from distutils.core import setup
import py2exe
# Powered by ***
INCLUDES = []
options = {"py2exe" :
{"compressed" : 1,
"optimize" : 2,
"includes" : INCLUDES,
"dll_excludes": ["mswsock.dll", "powrprof.dll","w9xpopen.exe"] }}
setup(
name = "OA Helper",
version = '1.0',
options = options,
description = "Please contact with Anderson if you have any problems,Q:117732865",
zipfile=None,
console=[{"script": "file.py", "icon_resources": [(1, "logo.ico")] }],
)

因为时间很紧,脚本没有做太多的优化,也没有结果报表啥的,主要是给自己看的,就随意了很多。