首頁 >後端開發 >Python教學 >一次完整的自動化登入測試-2017-7-4

一次完整的自動化登入測試-2017-7-4

PHP中文网
PHP中文网原創
2017-07-04 19:30:15996瀏覽
<span style="color: #577999; font-style: italic">#coding=utf-8<br><span style="color: #577999; font-style: italic"><br><span style="color: #161b99; font-style: italic">'''<br><span style="color: #161b99; font-style: italic">   Created on 2017-7-4<br><span style="color: #161b99; font-style: italic">   @auther:Qigege<br><span style="color: #161b99; font-style: italic">   project:<span style='color: #161b99; font-style: italic; font-family: "宋体"'>登录测试用例<span style="color: #161b99; font-style: italic"><br><span style="color: #161b99; font-style: italic">'''<br><span style="color: #161b99; font-style: italic"><br><span style="color: #4038e3">import unittest<br><span style="color: #4038e3">from selenium <span style="color: #4038e3">import webdriver<br><span style="color: #4038e3">from selenium.webdriver.firefox.firefox_binary <span style="color: #4038e3">import  FirefoxBinary<br><span style="color: #4038e3">import time<br><br><span style="color: #4038e3">class <span style="color: #dd2eee">LoginCase(unittest.TestCase):<br><span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">setUp(<span style="color: #94558d">self):<br>        binary=FirefoxBinary(<span style="color: #ef3142">'F:<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">ff<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">MozillaFirefox50.1.0<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">firefox.exe')<br><span style="color: #94558d">self.driver=webdriver.Firefox(<span style="color: #660099">firefox_binary=binary)<br><span style="color: #94558d">self.url=<span style="color: #ef3142">'https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F'<br><span style="color: #ef3142"><br><span style="color: #ef3142">    <span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>定义登录的方法<br><span style='color: #577999; font-style: italic; font-family: "宋体"'>    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">login(<span style="color: #94558d">self,<span style="font-style: italic">username,<span style="font-style: italic">password):<br>        driver=<span style="color: #94558d">self.driver<br>        url=<span style="color: #94558d">self.url<br>        driver.get(url)<br>        driver.find_element_by_id(<span style="color: #ef3142">'input1').send_keys(<span style="font-style: italic">username)<br>        driver.find_element_by_id(<span style="color: #ef3142">'input2').send_keys(<span style="font-style: italic">password)<br>        driver.find_element_by_id(<span style="color: #ef3142">'signin').click()<br><br><span style="color: #577999; font-style: italic">#@unittest.skip(u'<span style='color: #577999; font-style: italic; font-family: "宋体"'>条件均正确暂时跳过<span style="color: #577999; font-style: italic">')<br><span style="color: #577999; font-style: italic">    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">test_login_success(<span style="color: #94558d">self):<br><span style="color: #161b99; font-style: italic">'''username and password right'''<br><span style="color: #161b99; font-style: italic">        <span style="color: #94558d">self.login(<span style="color: #ef3142">'Qigege',<span style="color: #ef3142">'******')<br>        time.sleep(<span style="color: #549937">2)<br>        link=<span style="color: #94558d">self.driver.find_element_by_link_text(<span style="color: #ef3142">'Qigege')<br><span style="color: #94558d">self.assertTrue(<span style="color: #ef3142">'Qigege' <span style="color: #4038e3">in link.text)<br><span style="color: #577999; font-style: italic">#<span style='color: #577999; font-style: italic; font-family: "宋体"'>截图<br><span style='color: #577999; font-style: italic; font-family: "宋体"'>        <span style="color: #94558d">self.driver.get_screenshot_as_file(<span style="color: #ef3142">'E:<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">py-sql<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">example<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">pro5_note<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">user_pwd.jpg')<br><span style="color: #4038e3">print <span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>条件均正确<span style="color: #008080; font-weight: bold">——<span style='color: #008080; font-weight: bold; font-family: "宋体"'>测试用例<span style="color: #008080; font-weight: bold">'<br><span style="color: #008080; font-weight: bold"><br><span style="color: #008080; font-weight: bold">    <span style="color: #577999; font-style: italic">#@unittest.skip(u'<span style='color: #577999; font-style: italic; font-family: "宋体"'>密码错误暂时跳过<span style="color: #577999; font-style: italic">')<br><span style="color: #577999; font-style: italic">    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">test_login_pwd_error(<span style="color: #94558d">self):<br><span style="color: #161b99; font-style: italic">'''username right and password error'''<br><span style="color: #161b99; font-style: italic">        <span style="color: #94558d">self.login(<span style="color: #ef3142">'Qigege',<span style="color: #ef3142">'123123')<br>        time.sleep(<span style="color: #549937">2)<br>        link=<span style="color: #94558d">self.driver.find_element_by_id(<span style="color: #ef3142">'tip_btn')<br><span style="color: #94558d">self.assertTrue(<span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>用户名或密码错误<span style="color: #008080; font-weight: bold">',link.text)<br><span style="color: #94558d">self.driver.get_screenshot_as_file(<span style="color: #ef3142">'E:<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">py-sql<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">example<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">pro5_note<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">err_pwd.jpg')<br><span style="color: #4038e3">print <span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>密码错误<span style="color: #008080; font-weight: bold">——<span style='color: #008080; font-weight: bold; font-family: "宋体"'>测试用例<span style="color: #008080; font-weight: bold">'<br><span style="color: #008080; font-weight: bold"><br><span style="color: #008080; font-weight: bold">    <span style="color: #577999; font-style: italic">#@unittest.skip(u'<span style='color: #577999; font-style: italic; font-family: "宋体"'>密码为空暂时跳过<span style="color: #577999; font-style: italic">')<br><span style="color: #577999; font-style: italic">    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">test_login_pwd_null(<span style="color: #94558d">self):<br><span style="color: #161b99; font-style: italic">'''username right and password null'''<br><span style="color: #161b99; font-style: italic">        <span style="color: #94558d">self.login(<span style="color: #ef3142">'Qigege',<span style="color: #ef3142">'')<br>        time.sleep(<span style="color: #549937">2)<br>        link=<span style="color: #94558d">self.driver.find_element_by_id(<span style="color: #ef3142">'tip_input2')<br><span style="color: #94558d">self.assertTrue(<span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>请输入密码<span style="color: #008080; font-weight: bold">' <span style="color: #4038e3">in link.text)<br><span style="color: #94558d">self.driver.get_screenshot_as_file(<span style="color: #ef3142">'E:<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">py-sql<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">example<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">pro5_note<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">null_pwd.jpg')<br><span style="color: #4038e3">print <span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>密码为空<span style="color: #008080; font-weight: bold">——<span style='color: #008080; font-weight: bold; font-family: "宋体"'>测试用例<span style="color: #008080; font-weight: bold">'<br><span style="color: #008080; font-weight: bold"><br><span style="color: #008080; font-weight: bold">    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">test_login_user_err(<span style="color: #94558d">self):<br><span style="color: #161b99; font-style: italic">'''username error and password right'''<br><span style="color: #161b99; font-style: italic">        <span style="color: #94558d">self.login(<span style="color: #ef3142">'gege',<span style="color: #ef3142">'******')<br>        time.sleep(<span style="color: #549937">2)<br>        link=<span style="color: #94558d">self.driver.find_element_by_id(<span style="color: #ef3142">'tip_input2')<br><span style="color: #94558d">self.assertTrue(<span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>用户名或密码错误<span style="color: #008080; font-weight: bold">',link.text)<br><span style="color: #94558d">self.driver.get_screenshot_as_file(<span style="color: #ef3142">'E:<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">py-sql<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">example<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">pro5_note<span style="color: #000080; font-weight: bold">\\<span style="color: #ef3142">err_user.jpg')<br><span style="color: #4038e3">print <span style="color: #008080; font-weight: bold">u'<span style='color: #008080; font-weight: bold; font-family: "宋体"'>用户名错误<span style="color: #008080; font-weight: bold">——<span style='color: #008080; font-weight: bold; font-family: "宋体"'>测试用例<span style="color: #008080; font-weight: bold">'<br><span style="color: #008080; font-weight: bold"><br><span style="color: #008080; font-weight: bold">    <span style="color: #4038e3">def <span style="color: #990000; font-weight: bold">test_login_user_null(<span style="color: #94558d">self):        '''username null and password right'''        self.login('','******')        link=self.driver.find_element_by_id('tip_input1')        self.assertTrue(u'请输入登录用户名' in link.text)        self.driver.get_screenshot_as_file('E:\\py-sql\\example\\pro5_note\\null_user.jpg')        print u'用户名为空——测试用例'    def tearDown(self):        time.sleep(2)        print u'测试完毕!'        self.driver.quit()if __name__=='__main__':    unittest.main()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

以上是一次完整的自動化登入測試-2017-7-4的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn