selenium09-自动化练习案例
为了练习测试用例模块化,与数据分离,这次以qq邮箱为例,进行练习:
先将6个qq账号和密码放在一个单独的文件A中,然后在另一个文件B中编写自动化的代码,调用文件A中的账号和密码来实现自动登录并验证,验证之后,退出浏览器,三秒后再开始下一个账号的登录。
以下是源码,具体的qq账号和密码我隐去了
#user_list.py #存放账号和密码的文件 user_list.py
def get_user_list():
user_list = {
'account01': {'username':'********', 'password':'********'},
'account02': {'username':'********', 'password':'********'},
'account03': {'username':'********', 'password':'********'},
'account04': {'username':'********', 'password':'********'},
'account05': {'username':'********', 'password':'********'},
'account06': {'username':'********', 'password':'********'}
}
return user_list
#qq_mail_login.py #主要代码模块的文件名 qq_mail_login.py
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
import user_list
def get_web():
driver = webdriver.Firefox()
driver.get('https://mail.qq.com/')
return driver
def get_login(driver):
WebDriverWait(driver, 10).until(lambda driver:driver.find_element_by_xpath('/html/body/div/div[1]/a'))
driver.switch_to.frame('login_frame')
driver.find_element_by_id('switcher_plogin').click()
def input_login(driver, account, password):
ele_account = driver.find_element_by_id('u')
ele_account.clear()
ele_account.send_keys(account)
ele_account.send_keys('@qq.com')
ele_password = driver.find_element_by_id('p')
ele_password.clear()
ele_password.send_keys(password)
driver.find_element_by_xpath('//*[@id="login_button"]').click()
time.sleep(10)
def check_login(driver, account):
try:
driver.switch_to.default_content()
driver.switch_to.frame('mainFrame')
ele = driver.find_element_by_xpath('/html/body/div[2]/div/ul/li[1]/div[2]/a')
print('登录账号',account,'成功!')
except:
print(account,'登录失败!')
if __name__ == "__main__":
users = user_list.get_user_list()
for key, value in users.items():
account = value['username']
password = value['password']
print('账号', key, '正在登录中……')
driver = get_web()
get_login(driver)
input_login(driver, account, password)
check_login(driver, account)
driver.quit()
time.sleep(3)
凌晨一点,困得眼睛睁不开,最近上班是996,也许以上代码中有错,但是我在geany中运行是正常的,不知道刚刚犯困有没有改错