• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一次完整的自动化登录测试-2017-7-4

python 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论
<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_i<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码网(</div>d(<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的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一次完整的自动化登录测试-2017-7-4
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址