只開啟一次瀏覽器,產生html測試報告<小緊張中......>

來源:互聯網
上載者:User

標籤:ret   except   box   報告   函數   cli   UI   return   add   


from selenium import webdriver

import unittest
import time

class Blog(unittest.TestCase):
"""登入部落格園"""

# 添加@classmethod裝飾器,只開啟瀏覽器一次
@classmethod
def setUpClass(cls):
"""初始化"""
cls.browser = webdriver.Chrome()
cls.url = ‘https://passport.cnblogs.com/user/signin‘
cls.browser.get(cls.url)
cls.browser.implicitly_wait(30)
cls.browser.maximize_window()

def login(self, username, password):
"""登入帳號密碼參數化"""
self.browser.find_element_by_id(‘input1‘).send_keys(username)
self.browser.find_element_by_id(‘input2‘).send_keys(password)
self.browser.find_element_by_id(‘signin‘).click()
time.sleep(3)

def is_login_sucess(self):
"""判斷是否擷取登入後賬戶名稱"""
time.sleep(2)
try:
text = self.browser.find_element_by_id(‘lnk_current_user‘).text
print(text)
return True
except:
return False

def back_out(self):
# 退出返回登入頁面
time.sleep(2)
self.browser.find_element_by_xpath(‘//*[@id="header_user_right"]/a[5]‘).click()
time.sleep(2)
# alert方法去掉退出彈框
s = self.browser.switch_to_alert()
s.accept()
time.sleep(2)
self.browser.find_element_by_xpath(‘//*[@id="header_user_left"]/a[1]‘).click()
time.sleep(2)


def clear_box(self):
"""清空輸入框"""
self.browser.find_element_by_id(‘input1‘).clear()
self.browser.find_element_by_id(‘input2‘).clear()
time.sleep(1)

def test_01(self):
"""調用login函數登入"""
self.login(‘uesrname‘, ‘password‘)
result = self.is_login_sucess()
self.assertTrue(result)

def test_02(self):
"""錯誤的示範"""
self.back_out()
self.clear_box()
self.login(‘1‘, ‘2‘)
result = self.is_login_sucess()
self.assertTrue(result, msg=‘失敗了呀,沒有擷取登入後的賬戶名稱呢!‘)

@classmethod
def tearDownClass(cls):
cls.browser.quit()

if __name__ == ‘__main__‘:
"""產生html報告"""
import HTMLTestRunner
# now = time.s
now = time.strftime(‘%Y-%m-%d %H-%M-%S‘)
report_title = ‘部落格園登入自動化測試‘
# report_path = r‘D:\PythonProject\com\report.html‘
report_path = ‘D:\\PythonProject\\com\\‘+ now + ‘report.html‘
explain = ‘部落格園登入測試報告‘

test_suite = unittest.TestSuite()
test_suite.addTest(Blog(‘test_01‘))
test_suite.addTest(Blog(‘test_02‘))


with open(report_path, ‘wb‘) as fp:
runner = HTMLTestRunner.HTMLTestRunner(title=report_title, stream=fp, description=explain)
runner.run(test_suite)
fp.close()

只開啟一次瀏覽器,產生html測試報告<小緊張中......>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.