關(guān)于自動化Python+Selenium的自動化測試思路——之線性腳本篇
經(jīng)過最近的學(xué)習(xí)蟲師的Python的自動化測試,了解到了。入門學(xué)習(xí)中的一種方式,線性腳本篇
線性腳本的編寫,代表的是每個py都是一個test_case,各自獨立。如下面測試126的腳本為例
第一部分: 首先為了完成自動化測試,需要確定目錄結(jié)構(gòu):

image.png
如圖所示:
image文件夾: 是用于腳本運行中,對于一些地方的截圖
report文件夾: 是用于腳本運行后,生成的報告存儲
test_case文件夾: 顧名思義,就是存放每個測試用例py的腳本地方(?。。。。?!重要)
test_case_execl文件夾: 是用于存放項目中的execl用例,該用例對應(yīng)的是test_case中的腳本(1:1對應(yīng)關(guān)系)
test_project文件夾: 主要是用于項目最后集成運行的腳本,主要是查找運行的case腳本,輸出報告,并發(fā)送到指定郵箱(!?。。。?!重要)
了解了文件夾,目前下面只對test_case和test_project做處理介紹:
第二部分: test_case的結(jié)構(gòu)

image.png
如圖,每個test_*都是一個功能或者流程的case
已其中一個為例子:
1. __author__ = 'Administrator'
2. # coding=utf-8
3. from selenium import webdriver
4. import unittest
5. from selenium.webdriver.common.by import By
6. import time
7. class TestSwitchTabClass(unittest.TestCase):
8. def setUp(self):
9. self.browser = webdriver.Chrome()
10. self._126_url = "http://www.126.com/"
11. def test_126_switchtoshoujianxiang(self):
12. browser = self.browser
13. browser.get(self._126_url)
14. browser.maximize_window()
15. browser.implicitly_wait(10)
16. # driver.find_element(By.CSS_SELECTOR, "div.inputbox > div > input[name = 'email']").clear()
17. browser.switch_to.frame(browser.find_element_by_id("x-URS-iframe"))
18. browser.find_element_by_xpath(".//input[@name='email']").clear()
19. browser.find_element_by_xpath(".//input[@name='email']").send_keys("username")
20. #應(yīng)保密,請使用自己的126郵箱帳號
21. browser.find_element(By.NAME, "password").clear()
22. browser.find_element(By.NAME, "password").send_keys("pwd")
23. browser.find_element(By.ID, "dologin").click()
24. browser.implicitly_wait(10)
25. browser.find_element(By.ID, "_mail_tabitem_3_43text").click()
26. time.sleep(4)
27. # assert_text = driver.find_element(By.CSS_SELECTOR, "div a.gIbx-ext-txtLink").text
28. # print assert_text
29. def test_addemailtoshoujianxiang(self):
30. browser = self.browser
31. browser.get(self._126_url)
32. browser.maximize_window()
33. browser.implicitly_wait(5)
34. """登錄126"""
35. browser.switch_to.frame(browser.find_element_by_id("x-URS-iframe"))
36. browser.find_element_by_xpath(".//input[@name='email']").clear()
37. browser.find_element_by_xpath(".//input[@name='email']").send_keys("")
38. #應(yīng)保密,請使用自己的126郵箱帳號
39. browser.find_element(By.NAME, "password").clear()
40. browser.find_element(By.NAME, "password").send_keys("")
41. browser.implicitly_wait(3)
42. browser.find_element(By.ID, "dologin").click()
43. time.sleep(5)
44. """點擊寫信進入寫信界面"""
45. browser.find_element(By.XPATH, "http://span[text()='寫 信']").click()
46. browser.implicitly_wait(3)
47. """寫入郵件主題"""
48. write_subject = browser.find_element(By.XPATH, '//input[@class="nui-ipt-input" and @maxlength="256"]')
49. write_subject.click()
50. write_subject.send_keys(u"寫入主題")
51. """寫入郵件內(nèi)容"""
52. current_windowsname = browser.current_window_handle
53. browser.switch_to.frame(browser.find_element(By.CLASS_NAME, 'APP-editor-iframe'))
54. write_content = browser.find_element(By.CSS_SELECTOR, ".nui-scroll")
55. write_content.click()
56. write_content.send_keys(u"寫入郵件內(nèi)容寫入郵件內(nèi)容寫入郵件內(nèi)容寫入郵件內(nèi)容寫入郵件內(nèi)容寫入郵件內(nèi)容")
57. time.sleep(5)
58. """存入草稿箱"""
59. browser.switch_to.window(current_windowsname)
60. click_cungao = browser.find_element(By.XPATH, "http://span[text()='存草稿']")
61. click_cungao.click()
62. def tearDown(self):
63. self.browser.quit()
64. # if __name__ == "__main__":
65. # unittest.main()
第三部分: 集成所有的test_case,進行運行,并自動產(chǎn)生報告和發(fā)送到指定郵箱
test_project目錄結(jié)構(gòu)如下:

image.png
其中Test_all_case.py就是一個總的腳本
如下:
1. # coding=utf-8
2. __author__ = 'Administrator'
3. import time
4. import unittest
5. import smtplib
6. from email.mime.text import MIMEText
7. from email.header import Header
8. import HTMLTestRunner
9. import os
10. """找到所有的testcase并且加入到suite中"""
11. def find_all_case():
12. file_path = "E:\\study\\JC_autotest\\126_test_auto\\test_case"
13. testsuite_all = unittest.TestSuite()
14. discover_all_testcase = unittest.defaultTestLoader.discover(file_path, pattern="test*.py", top_level_dir=None)
15. for testsuite_small in discover_all_testcase:
16. for testcase in testsuite_small:
17. testsuite_all.addTest(testcase)
18. return testsuite_all
19. """從report中獲得最新的一個測試報告"""
20. def find_new_report():
21. report_file_path = "E:\\study\\JC_autotest\\126_test_auto\\report"
22. lists = os.listdir(report_file_path)
23. lists.sort(key=lambda fx: os.path.getctime(report_file_path+"\\"+fx))
24. report_file = lists[-1]
25. new_report_path = os.path.join(report_file_path, report_file)
26. return new_report_path
27. """對應(yīng)的report到指定的email"""
28. def send_email(receiver_user, receiver_pwd):
29. # 郵件內(nèi)容
30. email_content = find_new_report()
31. # 郵件主題
32. email_subject = "測試郵件"
33. # 發(fā)送者
34. sender = "wy@126.com"
35. # 接收者
36. receiver = receiver_user
37. # 確定使用何種郵箱服務(wù)器,此處是126郵箱
38. smtpserver = "smtp.126.com"
39. # 確定授權(quán)郵箱登錄
40. email_login_name = receiver_user
41. email_login_pwd = receiver_pwd
42. # 將如上內(nèi)容email_content經(jīng)過調(diào)整,變?yōu)猷]件可發(fā)送內(nèi)容
43. fp1 = file(email_content, "rb")
44. content = fp1.read()
45. msg = MIMEText(content, _subtype="html", _charset="utf-8")
46. msg["subject"] = Header(email_subject, "utf-8")
47. # 發(fā)送郵件
48. smtp = smtplib.SMTP()
49. smtp.connect(smtpserver)
50. smtp.login(email_login_name, email_login_pwd)
51. smtp.sendmail(sender, receiver, msg.as_string())
52. smtp.quit()
53. if __name__ == "__main__":
54. """生成動態(tài)時間報告"""
55. now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
56. report_path = "E:\\study\\JC_autotest\\126_test_auto\\report\\" + now + "report.html"
57. fp = file(report_path, 'wb')
58. runner = HTMLTestRunner.HTMLTestRunner(
59. stream=fp,
60. title=u"126測試報告",
61. description=u"測試報告主要看運行結(jié)果"
62. )
63. alltest = find_all_case()
64. runner.run(alltest)
65. fp.close()
66. """找到對應(yīng)的report并發(fā)送郵件到指定的郵件箱"""
67. find_new_report()
68. send_email("wy@126.com", "wy")
運行上面的代碼后會自動生產(chǎn)報告:
第四部分,報告

image.png