学习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邮箱代码:

 from selenium import webdriver
from time import sleep class QqLogin(): def user_login(dr,username,pwd): print(username,pwd) dr.switch_to.frame("login_frame")
idInput = dr.find_element_by_id('u')
pwdInput = dr.find_element_by_id('p')
idInput.clear()
idInput.send_keys(username)
pwdInput.clear()
pwdInput.send_keys(pwd) #登录
dr.find_element_by_id('login_button').click() #返回上级frame
#dr.switch_to.parent_frame() #返回主frame,此处两个方法都可以
dr.switch_to.default_content() #调用返回主frame需要等一下
sleep(1) switchs = dr.find_elements_by_css_selector('div')
print( len(switchs) ) #获取登录用户名、邮箱
name = dr.find_element_by_id('useralias')
email = dr.find_element_by_id('useraddr')
print('qq登录成功|',name.text,'---',email.text) #dr.quit()

登陆网易邮箱代码

 from time import sleep

 class WyLogin():

     #登录
def user_login(driver,username,pwd):
sleep(1)
print( driver.current_url )
driver.switch_to.frame('x-URS-iframe')
emailInput = driver.find_element_by_name("email")
emailInput.clear()
#emailInput.send_keys(username)#火狐执行无效
email_id = emailInput.get_attribute("id")
js = 'document.getElementById("'+email_id+'").value="'+username+'"'
print(js)
driver.execute_script(js)#执行js
pwdInput = driver.find_element_by_name("password")
pwdInput.clear()
pwdInput.send_keys(pwd)
dologin = driver.find_element_by_id("dologin")
dologin.click() print('网易邮箱登陆成功') driver.switch_to.default_content()

登陆方法:

 # coding=utf-8
from selenium import webdriver
from time import sleep
from loginQq import QqLogin
from loginWy import WyLogin class LoginSetup(): #初始化,两个下划横杠
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) #登录
def login(self):
user_file = open('user_info.txt','r')
lines = user_file.readlines()
user_file.close() try:
json_lines = [] for line in lines:
lineArr = line.split(',')
mail_type = lineArr[0]
mail = lineArr[1]
username = lineArr[2]
pwd = lineArr[3] # 打开浏览器窗口,定位当前窗口
url = 'http://'+mail
self.open_url(url) json_line = {}
json_line['username'] = username
json_line['pwd'] = pwd
json_line['mail'] = mail
json_line['mail_type'] = mail_type
json_lines.append(json_line)
#for end print(json_lines)
self.open_login(json_lines) #关闭浏览器
#self.driver.quit() except BaseException as error:
print('error:',error)
self.driver.quit()
#end login #打开新窗口
def open_url(self,url):
js = 'window.open("'+url+'")'
print(js)
self.driver.execute_script(js)
'''
win_handles = self.driver.window_handles
print( len(win_handles) )
for hand in win_handles:
print( hand ) cur_window = self.driver.current_window_handle
self.driver.switch_to.window(cur_window)
print('now url is ',self.driver.current_url)
'''
# win_handles = self.driver.window_handles
#end open_url #定位新打开窗口,登录
def open_login(self,json_lines):
dr = self.driver
cur_windows = dr.window_handles
print( len(cur_windows) )
username = ''
pwd = ''
mail_type = '' for cur_window in cur_windows:
dr.switch_to.window(cur_window)
cur_url = dr.current_url
print('cur_url 1 = ',cur_url) for line in json_lines:
mail = line['mail']
mail_in = mail.replace('www.','')
print(mail_in,cur_url)
if mail_in in cur_url:
print('username')
mail_type = line['mail_type']
username = line['username']
pwd = line['pwd'] print(mail_type,username) if username == '':
continue #调用登录方法
print('username is ',username)
if 'QQ' in mail_type:
QqLogin.user_login(dr,username,pwd)
if 'WY' in mail_type:
WyLogin.user_login(dr,username,pwd) # end open_login #调用登录方法
LoginSetup().login()

txt文件格式:

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

selenium2自动化测试学习笔记(五)-参数化编程,自动登陆网易QQ邮箱的更多相关文章

  1. CAS学习笔记五:SpringBoot自动/手动配置方式集成CAS单点登出

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式实现CAS客户端登出及单点登出. 本文基于<CAS学习笔记三:SpringBoot自动/手动配置方式集成C ...

  2. selenium2自动化测试学习笔记(四)

    今天是学习selenium2第四天.总结下今天的学习成果,自动登录网易邮箱并写信发送邮件. 知识点or坑点: 1.模块化编写测试模块(类似java里的抽象方法,js的函数编写) from 包名 imp ...

  3. selenium2自动化测试学习笔记(一)

    从这周开始学习自动化测试,采用selenium2,目标是在本月学习到appium,并测试公司的真实APP项目. 系统环境:win10 语言:python3.6.4 工具:selenium2 IDE:p ...

  4. python自动化测试学习笔记-unittest参数化

    做接口测试的时候,当一个参数需要输入多个值的时候,就可以使用参数来实现: python中unittest单元测试,可以使用nose_parameterized来实现: 首先需要安装:pip  inst ...

  5. python自动化测试学习笔记-7面向对象编程,类,继承,实例变量,邮件

    面向对象编程(OOP)术语: class TestClass(object):   val1 = 100       def __init__(self):     self.val2 = 200   ...

  6. selenium2自动化测试学习笔记(三)

    今天是学习selenium的第三天,今天的主题是自动登录126邮箱. 今天总结碰到的坑有三个: 1.frame内元素抓取,使用driver.switch_to.frame(frameId)方法切换锁定 ...

  7. selenium2自动化测试学习笔记(二)

    chromedriver报错问题解决了,真是无语 是因为chromedriver与浏览器版本不一致 http://chromedriver.storage.googleapis.com/index.h ...

  8. 孙鑫VC学习笔记:多线程编程

    孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modified ...

  9. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

随机推荐

  1. Flex中获取RadioButtonGroup中的RadioButton的值

    Flex中获取RadioButtonGroup中的RadioButton的值 1.设计源码 <?xml version="1.0" encoding="utf-8& ...

  2. [2015-06-10 20:53:50 - Android SDK] Error when loading the SDK:

    1.错误描述 [2015-06-10 20:53:50 - Android SDK] Error when loading the SDK: Error: Error parsing D:\Andro ...

  3. Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

    1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.tes ...

  4. Java考查“==”和equals

    /** * */ package com.you.demo; /** * @author YouHaiDong * @date 2015-04-02 */ public class Welcome { ...

  5. MFC中CFileDialog用法

    用CFileDialog选择了一个文件后,使用FILE::fopen打开文件错误,使用 的是相对地址,和王工调试了半天,怎么跟踪也没发现错误,原来如此......... CFileDialog文件选择 ...

  6. nodejs异步案例

    const fs = require('fs'); fs.readFile('./test.txt', 'utf-8', (err, data) => { err ? console.error ...

  7. SpringMVC拦截器(包括自定以拦截器--实现HandlerInterceptorAdapter)(资源和权限管理)

    一,springmvc的配置 <!-- 访问拦截 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping ...

  8. httppost的用法(NameValuePair(简单名称值对节点类型)核心对象)

    一,案例一 定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型),这个代码多处用于Java像url发送Post请求.在发送post请求时用该list来存放参数. ...

  9. 凯撒密码加密解密--JAVA实现(基础)

    凯撒密码一种代换密码,据说凯撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码.凯撒密码的基本思想是:通过把字母移动一定的位数来实现加密和解密.明文中的所有字母都在字母表上向后(或向前) ...

  10. Python Cookbook(第3版)中文版:15.18 传递已打开的文件给C扩展

    15.18 传递已打开的文件给C扩展¶ 问题¶ 你在Python中有一个打开的文件对象,但是需要将它传给要使用这个文件的C扩展. 解决方案¶ 要将一个文件转换为一个整型的文件描述符,使用 PyFile ...