一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python分類測試代碼實例匯總

Python分類測試代碼實例匯總

2020-07-23 23:03bashliuhe Python

這篇文章主要介紹了Python分類測試代碼實例匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1.自動化測試里面的測試用例設計的一些方法

解耦、可以獨立運行、需要靈活切換

設計思路: 腳本功能分析(分步驟)和模塊化分層(拆分為多模塊)

project

login_order.py #登錄下單測試用例
category.py #菜單分類測試用例

all_test.py #主入口

login_order.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: UTF-8 -*-
import unittest
import time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
 
 
class LoginOrderTestCase(unittest.TestCase):
  def setUp(self):
    print("測試開始")
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(20)
    self.base_url = "https://xdclass.net"
    self.driver.get(self.base_url)
 
  def tearDown(self):
    print("單個測試用例結束")
    pass
    #單個測試用例結束
  
  def test_login_order(self):
    u"登錄測試用例"
    driver = self.driver
    #登錄框
    login_ele = driver.find_element_by_css_selector("#login")
    ActionChains(driver).click(login_ele).perform()
 
    sleep(2)
    #查找輸入框,輸入賬號,輸入框要提前清理里面的數據
    driver.find_element_by_id("phone").clear()
    driver.find_element_by_id("phone").send_keys("13113777338")
    #查找密碼輸入框,輸入密碼
    driver.find_element_by_id("pwd").clear()
    driver.find_element_by_id("pwd").send_keys("123456789")
 
    #拿到登錄按鈕
    login_btn_ele = driver.find_element_by_css_selector("button.login")
    #觸發點擊事件,登錄
    login_btn_ele.click()
    #判斷登陸是否成功,邏輯-》鼠標移到上面,判斷彈窗字符
    #獲取鼠標上移的元素
    user_info_ele = driver.find_element_by_css_selector(".user_head_portrait")
    sleep(1)
    #hover觸發
    ActionChains(driver).move_to_element(user_info_ele).perform()
    sleep(1)
    #獲取用戶名稱元素
    user_name_ele = driver.find_element_by_css_selector(".img_name > span:nth-child(2)")
    print("===測試結果==")
    print(user_name_ele.text)
 
    name = user_name_ele.text
    #self.assertEqual(name, u"二當家小D",msg="登錄失敗")
 
    video_ele = driver.find_element_by_css_selector("div.hotcourses:nth-child(3) > div:nth-child(2) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1) > div:nth-child(1) > img:nth-child(1)")
    video_ele.click()
    sleep(2)
 
    buy_btn_ele = driver.find_element_by_css_selector(".learn_btn > a:nth-child(1)")
 
    buy_btn_ele.click()
    print("進入下單頁面")
    
if __name__ == '__main__':
    unittest.main()

category.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: UTF-8 -*-
import unittest
import time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
 
class CategoryTestCase(unittest.TestCase):
  def setUp(self):
    print("測試開始")
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(20)
    self.base_url = "https://xdclass.net"
    self.driver.get(self.base_url)
 
 
  def tearDown(self):
    print("測試結束")
    #單個測試用例結束
    self.driver.quit()
 
  def test_menu(self):
    u"彈出菜單測試用例"
    driver = self.driver
    #跳轉網頁
    sleep(1)
 
    #定位到鼠標移動到上面的元素
    menu_ele = driver.find_element_by_css_selector("#banner_left_ul > a:nth-child(1) > li:nth-child(1) > span:nth-child(1)")
 
    #對定位到的元素執行鼠標移動到上面的操作
    ActionChains(driver).move_to_element(menu_ele).perform()
    sleep(2)
    #選中子菜單
    sub_meun_ele = driver.find_element_by_css_selector("#des > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > a:nth-child(1)")
 
    sub_meun_ele.click()
    sleep(2)
 
 
if __name__ == '__main__':
  unittest.main()

all_test.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: UTF-8 -*-
import unittest
import HTMLTestRunner
import login_order ,category
import time
 
#創建測試集合 
def create_suite():
  print("測試開始")
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(login_order.LoginOrderTestCase))
  suite.addTest(unittest.makeSuite(category.CategoryTestCase))
  return suite
   
 
if __name__ == '__main__':
  suite = create_suite()
 
  #文件名中加了當前時間,為了每次生成不同的測試報告
  file_prefix = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
 
  #創建測試報告,此時這個文件還是空文件 wb 以二進制格式打開一個文件,只用于寫入,如果文件存在則覆蓋,不存在則創建
  fp = open("./"+file_prefix+"_result.html","wb")
  
  # stream定義一個測試報告寫入的文件,title就是標題,description就是描述
  runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"小D課堂 測試報告",description=u"測試用例執行情況",verbosity=2)
  runner.run(suite)
  fp.close()

 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/bashliuhe/p/13284063.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎综合九九色九九综合色 | 爆操美女在线观看 | 日韩福利一区 | 国产精品66福利在线观看 | 免费免费啪视频在线观播放 | 男人狂躁女人下面狂叫图片 | 蜜桃视频一区二区 | 日韩免费视频播播 | 娇妻被健身教练挺进小说阅读 | 国产成人激情 | 十大免费b2b网站 | 91系列在线观看免费 | 国产精品免费久久久久影院 | 奇米影视一区 | 波多野结衣在线中文字幕 | 国产欧美va欧美va香蕉在线观 | 欧美一区二区三区四区在线观看 | 国产在线观看福利 | 亚洲欧美久久久久久久久久爽网站 | 国产91短视频 | 国产在线视频在线观看 | pregnantsexxx临盆孕妇 | 精品久久久久久 | 久久成人国产精品一区二区 | 美女日b视频 | 4虎影视国产在线观看精品 4s4s4s4s色大众影视 | 国产成人精品视频一区二区不卡 | 国产欧美日韩专区 | 脱jk裙的美女露小内内无遮挡 | 欧美人人干| 国内精品久久久久久野外 | 91大神大战高跟丝袜美女 | 深夜在线小视频 | 日本aa大片在线播放免费看 | 视频在线观看国产 | 99久女女精品视频在线观看 | 三级黄色图片 | 国产福利在线免费观看 | 97色综合 | 男人的天堂视频 | 草莓香蕉榴莲丝瓜秋葵绿巨人在线看 |