【public/login.py】html
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 webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysimport unittest,time,osimport public.login as loginimport xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse(os.path.abspath('login.xml'))#獲得文檔元素對象root = dom.documentElementclass 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 ③ 隱式等待 '''