上善若水,不进则退

姚毛毛

左手代码,右手写诗

linuxido.com

selenium2自动化测试学习笔记(五)-参数化编程,自动登陆网易QQ邮箱

学习python下使用selenium2自动测试第6天,参数化编程这节课花了两天时间。

本次编程主要时间是花在熟悉python上

知识点or坑点:

1、读取txt、xml、csv等文件存储的账号、密码

txt文件格式,逗号分割(也可使用其他符号):

www.126.com,user1,pwd1

www.qq.com,user2,pwd2

www.163.com,user3,pwd3

user_file = open('user_info.txt','r')
lines = user_file.readlines()
user_file.close()

for line in lines:
    mail = line.split(',')[0]
    username = line.split(',')[1]
    pwd = line.split(',')[2]
    print(mail,username,pwd)

2、打开多窗口,定位新窗口

获取所有窗口句柄:cur_windows = dr.window_handles

定位窗口:

for cur_window in cur_windows:
  dr.switch_to.window(cur_window)

3、python编程中self的作用

在我的理解中,self是全局的this对象,定义一个class LoginSetup():

self就是指LoginSetup这个对象本身

在本class中定义多个对象时,可使用self.function( [param1,param2,...] )来调用,

被调用方法的参数self为默认参数,真实接收参数从第二个开始

被调用函数:

def open_url(self,url):
        js = 'window.open("'+url+'")'
        print(js)
        self.driver.execute_script(js)

调用函数:

def login(self):
    json_lines = []
    ……
    self.open_login(json_lines)

  

4、python的init初始化,是前后两个下划线横杠(坑点)

#初始化,两个下划横杠
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        url = 'http://www.baidu.com'
        self.init_url = url
        self.driver.get(url)

在类运行时,初始化一些参数

5、python的in

for 循环、比较部分字符串都可使用in

foreach:

for line in lines:

     print(line['username'])

  print(line['pwd'])

比较字符串:

if 'QQ' in url:

  print('true')

 

登陆QQ邮箱代码:

 1 from selenium import webdriver
 2 from time import sleep
 3 
 4 class QqLogin():
 5 
 6     def user_login(dr,username,pwd):
 7 
 8         print(username,pwd)
 9         
10         dr.switch_to.frame("login_frame")
11         idInput = dr.find_element_by_id('u')
12         pwdInput = dr.find_element_by_id('p')
13         idInput.clear()
14         idInput.send_keys(username)
15         pwdInput.clear()
16         pwdInput.send_keys(pwd)
17 
18         #登录
19         dr.find_element_by_id('login_button').click()
20 
21         #返回上级frame
22         #dr.switch_to.parent_frame()
23 
24         #返回主frame,此处两个方法都可以
25         dr.switch_to.default_content()
26 
27         #调用返回主frame需要等一下
28         sleep(1)
29 
30         switchs = dr.find_elements_by_css_selector('div')
31         print( len(switchs) )
32 
33         #获取登录用户名、邮箱
34         name = dr.find_element_by_id('useralias')
35         email = dr.find_element_by_id('useraddr')
36         print('qq登录成功|',name.text,'---',email.text)
37 
38         #dr.quit()
View Code

登陆网易邮箱代码

 1 from time import sleep
 2 
 3 class WyLogin():
 4     
 5     #登录
 6     def user_login(driver,username,pwd):
 7         sleep(1)
 8         print( driver.current_url )
 9         driver.switch_to.frame('x-URS-iframe')
10         emailInput = driver.find_element_by_name("email")
11         emailInput.clear()
12         #emailInput.send_keys(username)#火狐执行无效
13         email_id = emailInput.get_attribute("id")
14         js = 'document.getElementById("'+email_id+'").value="'+username+'"'
15         print(js)
16         driver.execute_script(js)#执行js
17         pwdInput = driver.find_element_by_name("password")
18         pwdInput.clear()
19         pwdInput.send_keys(pwd)
20         dologin = driver.find_element_by_id("dologin")
21         dologin.click()
22 
23         print('网易邮箱登陆成功')
24         
25         driver.switch_to.default_content()
26         
View Code

登陆方法:

  1 # coding=utf-8
  2 from selenium import webdriver
  3 from time import sleep
  4 from loginQq import QqLogin
  5 from loginWy import WyLogin
  6 
  7 class LoginSetup():
  8     
  9     #初始化,两个下划横杠
 10     def __init__(self):
 11         self.driver = webdriver.Chrome()
 12         self.driver.implicitly_wait(10)
 13         url = 'http://www.baidu.com'
 14         self.init_url = url
 15         self.driver.get(url)
 16 
 17     #登录
 18     def login(self):
 19         user_file = open('user_info.txt','r')
 20         lines = user_file.readlines()
 21         user_file.close()
 22 
 23         try:
 24             json_lines = []
 25             
 26             for line in lines:
 27                 lineArr = line.split(',')
 28                 mail_type = lineArr[0]
 29                 mail = lineArr[1]
 30                 username = lineArr[2]
 31                 pwd = lineArr[3]
 32 
 33                 # 打开浏览器窗口,定位当前窗口
 34                 url = 'http://'+mail
 35                 self.open_url(url)
 36 
 37                 json_line = {}
 38                 json_line['username'] = username
 39                 json_line['pwd'] = pwd
 40                 json_line['mail'] = mail
 41                 json_line['mail_type'] = mail_type
 42                 json_lines.append(json_line)
 43                 #for end
 44 
 45             print(json_lines)
 46             self.open_login(json_lines)
 47 
 48             #关闭浏览器
 49             #self.driver.quit()
 50 
 51         except BaseException as error:
 52             print('error:',error)
 53             self.driver.quit()
 54         #end login
 55 
 56     #打开新窗口
 57     def open_url(self,url):
 58         js = 'window.open("'+url+'")'
 59         print(js)
 60         self.driver.execute_script(js)
 61         '''
 62         win_handles = self.driver.window_handles
 63         print( len(win_handles) )
 64         for hand in win_handles:
 65             print( hand )
 66         
 67         cur_window = self.driver.current_window_handle
 68         self.driver.switch_to.window(cur_window)
 69         print('now url is ',self.driver.current_url)
 70         '''
 71         # win_handles = self.driver.window_handles
 72         #end open_url
 73 
 74     #定位新打开窗口,登录
 75     def open_login(self,json_lines):
 76         dr = self.driver
 77         cur_windows = dr.window_handles
 78         print( len(cur_windows) )
 79         username = ''
 80         pwd = ''
 81         mail_type = ''
 82     
 83         for cur_window in cur_windows:
 84             dr.switch_to.window(cur_window)
 85             cur_url = dr.current_url
 86             print('cur_url 1 = ',cur_url)
 87 
 88             for line in json_lines:
 89                 mail = line['mail']
 90                 mail_in = mail.replace('www.','')
 91                 print(mail_in,cur_url)
 92                 if mail_in in cur_url:
 93                     print('username')
 94                     mail_type = line['mail_type']
 95                     username = line['username']
 96                     pwd = line['pwd']
 97 
 98             
 99             print(mail_type,username)
100                 
101             if username == '':
102                 continue
103 
104             #调用登录方法
105             print('username is ',username)
106             if 'QQ' in mail_type:
107                 QqLogin.user_login(dr,username,pwd)                    
108             if 'WY' in mail_type:
109                 WyLogin.user_login(dr,username,pwd)     
110             
111         # end open_login
112 
113     
114                 
115 #调用登录方法
116 LoginSetup().login()
117 
118    
View Code

txt文件格式:

WY,www.126.com,user1,pwd1
WY,mail.163.com,user2,pwd2
QQ,mail.qq.com,user3,pwd3

posted @ 2018-03-19 17:21  姚毛毛  阅读(786)  评论(0编辑  收藏  举报