【Selenium2】【项目实战】

 

【public/login.py】

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#登录
def login(other,username,password):

driver = other.driver
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
driver.find_element(By.LINK_TEXT, '帐号密码登录').click()
driver.find_element(By.XPATH, ".//*[@id='u']").clear()
driver.find_element(By.XPATH, ".//*[@id='u']").send_keys(username)
driver.find_element(By.XPATH, ".//*[@id='p']").clear()
driver.find_element(By.XPATH, ".//*[@id='p']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, 'input#login_button').click()
driver.switch_to.default_content()


#退出
def logout(self):
driver = self.driver
driver.find_element(By.LINK_TEXT,'退出').click()

【report/all_test.py】
import unittest
import sys
#sys.path.append('C:/Users/wangxue1/PycharmProjects/selenium2TestOne/unittestTest')
#import unittestTest.public.login as login

#构造测试集
def creatsuite():
testunit = unittest.TestSuite()
#定义测试文件查找的目录
test_dir = 'C:/Users/wangxue1/PycharmProjects/selenium2TestOne/unittestTest'
#定义discover方法的参数
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py',top_level_dir=None)
#discover 方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
testunit.addTests(test_case)
print(testunit)
return testunit

if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(creatsuite())

【report/log.txt】的格式类似于下方:
....
----------------------------------------------------------------------
Ran 4 tests in 50.484s

OK
C:\Users\wangxue1\PycharmProjects\selenium2TestOne\unittestTest\login.xml
prompt_info: 你输入的帐号或密码不正确,请重新输入。
text: 你输入的帐号或密码不正确,请重新输入。
prompt_info: 你还没有输入帐号!
text: 你还没有输入帐号!
text: 你还没有输入密码!
prompt_info: 你还没有输入帐号!
text: 你还没有输入帐号!
Traceback (most recent call last):
File "report\all_test.py", line 4, in <module>
import unittestTest.public.login as login
ModuleNotFoundError: No module named 'unittestTest'
....
----------------------------------------------------------------------
Ran 4 tests in 52.457s

OK
C:\Users\wangxue1\PycharmProjects\selenium2TestOne\unittestTest\login.xml
<unittest.suite.TestSuite tests=[<test_login.TestLogin testMethod=test_error>, <test_login.TestLogin testMethod=test_null>, <test_login.TestLogin testMethod=test_pawd_null>, <test_login.TestLogin testMethod=test_user_null>]>
prompt_info: 你输入的帐号或密码不正确,请重新输入。
text: 你输入的帐号或密码不正确,请重新输入。
prompt_info: 你还没有输入帐号!
text: 你还没有输入帐号!
text: 你还没有输入密码!
prompt_info: 你还没有输入帐号!
text: 你还没有输入帐号!

【login.xml】
<?xml version='1.0' encoding='utf-8'?>
<info>
<explain>qq邮箱登录</explain>
<url>http://mail.qq.com</url>
<null username="" password="">你还没有输入帐号!</null>
<pawd_null username='###@qq.com' password=''>你还没有输入密码!</pawd_null>
<user_null username="" password="####">你还没有输入帐号!</user_null>
<error username="9######@qq.com" password="xxx">你输入的帐号或密码不正确,请重新输入。</error>

</info>

【number.py】


#栗子 登录126

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import unittest
import public.login as login


class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.base_url = 'http://mail.qq.com'
self.verificationErrors = []
self.accept_next_alert = True

def test_login(self):
driver = self.driver
driver.get(self.base_url)

#登录
login.login(self,'#########@qq.com','#########')
'''
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
driver.find_element(By.LINK_TEXT, '帐号密码登录').click()
driver.find_element(By.XPATH, ".//*[@id='u']").clear()
driver.find_element(By.XPATH, ".//*[@id='u']").send_keys('#########@qq.com')
driver.find_element(By.XPATH, ".//*[@id='p']").clear()
driver.find_element(By.XPATH, ".//*[@id='p']").send_keys('#############')
driver.find_element(By.CSS_SELECTOR, 'input#login_button').click()
# 获取断言信息进行断言
text = driver.find_element(By.ID, 'useraddr').text
self.assertEqual(text, '############@qq.com')
# 退出
time.sleep(2)
driver.find_element(By.LINK_TEXT, '退出').click()'''

def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)

if __name__ == '__main__':
unittest.main()


【test_login.py】
from selenium import webdriver
from selenium.webdriver.common.by import By
import time,os,sys
import unittest
import public.login as login
import xml.dom.minidom




#打开xml文档
path = os.path.abspath('login.xml')
print(path)
dom = xml.dom.minidom.parse(path)
#得到文档元素对象
root = dom.documentElement

class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
logins = root.getElementsByTagName('url')
self.base_url = logins[0].firstChild.data
self.verificationErrors = []

def test_null(self):
driver = self.driver
driver.get(self.base_url)
logins = root.getElementsByTagName('null')
# 获得null标签的username password属性值
username = logins[0].getAttribute('username')
password = logins[0].getAttribute('password')
prompt_info = logins[0].firstChild.data
print("prompt_info: ", prompt_info)
# 登录
login.login(self, username, password)
# 获得断言信息进行断言
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
text = driver.find_element(By.XPATH, ".//*[@id='err_m']").text
print("text: ", text)
self.assertEqual(text, prompt_info)

# 输入用户名、密码为空
def test_pawd_null(self):
driver = self.driver
driver.get(self.base_url)
logins = root.getElementsByTagName('pawd_null')
# 获得pawd_null标签的username password 属性值
username = logins[0].getAttribute('username')
password = logins[0].getAttribute('password')
prompt_info = logins[0].firstChild.data
# 登录
login.login(self, username, password)
# 获得断言信息进行断言
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
text = driver.find_element(By.XPATH, ".//*[@id='err_m']").text
print("text: ", text)
self.assertEqual(text, prompt_info)

# 用户名密码错误
def test_error(self):
driver = self.driver
driver.get(self.base_url)
logins = root.getElementsByTagName('error')
# 获得error标签的username password属性值
username = logins[0].getAttribute('username')
password = logins[0].getAttribute('password')
prompt_info = logins[0].firstChild.data
print("prompt_info: ", prompt_info)
# 登录
login.login(self, username, password)
# 获得断言信息进行断言
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
text = driver.find_element(By.XPATH, ".//*[@id='err_m']").text
print("text: ", text)
self.assertEqual(text, prompt_info)
# 用户名为空,输入密码
def test_user_null(self):
driver = self.driver
driver.get(self.base_url)
logins = root.getElementsByTagName('user_null')
# 获得user_null标签的username password 属性值
username = logins[0].getAttribute('username')
password = logins[0].getAttribute('password')
prompt_info = logins[0].firstChild.data
print("prompt_info: ", prompt_info)
login.login(self, username, password)
# 获得断言信息进行断言
if driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#login_frame'))
text = driver.find_element(By.XPATH, ".//*[@id='err_m']").text
print("text: ", text)
self.assertEqual(text, prompt_info)

def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)

if __name__ == '__main__':
unittest.main()



【test_send.py】
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import unittest,time,os
import public.login as login
import xml.dom.minidom


#打开xml文档
dom = xml.dom.minidom.parse(os.path.abspath('login.xml'))
#得到文档元素对象
root = dom.documentElement

class TestSendMail(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
logins = root.getElementsByTagName('url')
self.base_url = logins[0].firstChild.data
self.verificationErrors = []
#只填写收件人发送邮件
def qtest_send_mail(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'9##########@qq.com','############')
#写信
driver.find_element(By.LINK_TEXT,'写信').click()
#填写收件人

if driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame'))
driver.find_element(By.XPATH, ".//*[@id='toAreaCtrl']/div[2]/input").send_keys('#########@qq.com')
#发送邮件
list1 = driver.find_elements(By.XPATH,".//*[@id='toolbar']/div/a[1]")
list1[0].click()
driver.switch_to.default_content()
if driver.find_element(By.XPATH,".//*[@id='QMconfirm_QMDialog_confirm']").is_displayed():
driver.find_element(By.XPATH, ".//*[@id='QMconfirm_QMDialog_confirm']").click()
#断言发送结果
if driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame'))
if driver.find_element(By.XPATH, ".//*[@id='sendinfomsg_span']").is_displayed():
text = driver.find_element(By.XPATH, ".//*[@id='sendinfomsg_span']").text
self.assertEqual(text, '此邮件发送成功')
#填写收件人、主题、附件,发送邮件
def qtest_send_mail3(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self, '#######@qq.com', '#############')
#写信
driver.find_element(By.LINK_TEXT,'写信').click()
#填写收件人和主题
if driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame'))
driver.find_element(By.XPATH,".//*[@id='toAreaCtrl']/div[2]/input").send_keys('#############@qq.com')
driver.find_element(By.XPATH,".//*[@id='subject']").send_keys('给妹妹的信')
#上传附件
driver.find_element(By.XPATH,".//*[@id='AttachFrame']/span/input").send_keys('E:\wangxue1\Desktop\dog.PNG')
#发送邮件
list2 = driver.find_elements(By.XPATH,".//*[@id='toolbar']/div/a[1]")
list2[0].click()
driver.switch_to.default_content()
# 断言发送结果
if driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame'))
if driver.find_element(By.XPATH, ".//*[@id='sendinfomsg_span']").is_displayed():
text = driver.find_element(By.XPATH, ".//*[@id='sendinfomsg_span']").text
self.assertEqual(text, '此邮件发送成功')

#填写收件人、主题、正文,发送邮件
def qtest_send_mail4(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self, '#####1@qq.com', '#################')
#写信
driver.find_element(By.LINK_TEXT,'写信').click()
#填写收件人和主题
if driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame').is_displayed():
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame'))
driver.find_element(By.XPATH,".//*[@id='toAreaCtrl']/div[2]/input").send_keys('#########@qq.com')
driver.find_element(By.XPATH,".//*[@id='subject']").send_keys('给妹妹的信')
#定位正文编辑框textarea
f = driver.find_element(By.CLASS_NAME, "qmEditorIfrmEditArea")
driver.switch_to.frame(f)
#其实 send_key()同样可以向 body 标签之间输入内容
#driver.find_element(By.XPATH, "//body[@contenteditable='true']").send_keys('你好,阿妹,好久不见')
driver.find_element(By.XPATH, "//body[@accesskey='q']").send_keys('你好,阿妹,好久不见')
driver.switch_to.parent_frame()
list2 = driver.find_elements(By.XPATH, ".//*[@id='toolbar']/div/a[1]")
list2[0].click()
driver.switch_to.default_content()
if driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame').is_displayed():
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'iframe#mainFrame'))
if driver.find_element(By.XPATH,".//*[@id='sendinfomsg_span']").is_displayed():
text = driver.find_element(By.XPATH, ".//*[@id='sendinfomsg_span']").text
self.assertEqual(text,'此邮件发送成功')

#搜索邮件
def qtest_send_mail5(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self, '#########@qq.com', '############')
#定位搜索框,输入信息
driver.find_element(By.XPATH,"//input[@id='subject']").send_keys('9543')
driver.find_element(By.XPATH,"//input[@id='subject']").send_keys(Keys.ENTER)
#断言搜索邮件标签页面
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame'))
text = driver.find_element(By.XPATH,".//*[@id='frm']/table/tbody/tr/td[7]").text
self.assertEqual(text,'所在文件夹')

#删除邮件 html body#list.tbody div#qqmail_mailcontainer.mailcontainer form#frm div.toolbg.toolbgline.toolheight.nowrap div.nowrap.left a#quick_del.btn_gray.btn_space .//a[@id='quick_del' and @class='btn_gray btn_space']
def qtest_delete_mail6(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'#########@qq.com', '############')
#打开收件箱
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe#mainFrame'))
driver.find_element(By.XPATH,"//a[@id='folder_1']").click()
#选中“更早”中的第一封邮件
driver.find_elements(By.XPATH,"//*[@id='div_showbefore']/table[@class='i F']/tbody/tr/td[@class='cx']/input").pop(0).click()
driver.find_elements(By.XPATH,"//a[@id='quick_del' and @class='btn_gray btn_space']").pop(0).click()








def tearDown(self):
pass
# self.driver.quit()
#self.assertEqual([],self.verificationErrors)





if __name__ == '__main__':
unittest.main()



#【备注】
'''
参考:http://blog.csdn.net/huilan_same/article/details/52200586
注意:① 只有在父级,才能定位到子级 ② frameset不用切,frame需层层切!③ 三大法 (1) driver.switch_to.frame(reference) (2)driver.switch_to.parent_frame() (3) driver.switch_to.default_content()
定位元素困难,解决思路: ① 显式等待:常用办法我一般行不通,我基本用 .is_display() ② iframe ③ 隐式等待
'''



































































































posted @ 2018-01-09 12:05  素人渔芙2017  阅读(259)  评论(0编辑  收藏  举报