我们希望测试结果都保存到文件中,这样方便查看。在此之前很有必要梳理一下当测试项目的目录结
构:
test_project/test_case/test_login.py ----测试用例
/test_case/public/login.py ---测试用调用的公共模块
/test_case/public/__init__.py
/test_date/login.xml -----用于存放测试数据文件
/report/log.txt ----测试执行的结果
/report/all_test.py ----测试所有测试用例
关于 all_test.py 文件的创建请参考第 7 章中的实例代码,唯一需要调整的就是 discover()方法的一
参数。这里就不再重复粘贴这段代码。现在运行 all_test.py 测试文件进行吧。
cmd.exe
E:\test_object>Python all_test.py >> report/log.txt 2>&1
执行的测试结果如下:
<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite
tests=[<test_login.TestLogin testMethod=test_error>,
<test_login.TestLogin testMethod=test_null>,
<test_login.TestLogin testMethod=test_pawd_null>,
<test_login.TestLogin testMethod=test_user_null>]>]>
....
----------------------------------------------------------------------
Ran 4 tests in 57.322s
OK
扩展自动化测试用例
当前测试项目的目录结构已经形成,接下来要做的事情是继续丰富我们的测试用例。继续以 126 邮箱
为例,实现发送邮件、搜索邮件、删除邮箱的自动化测试用例。
发送邮件
首先编写测试用例
用例 001 :
用例 id test_sendmail.py 发送邮件
步骤 动作 数据 验证点
1 打开邮箱 http://webcloud.126.com
2 用户登陆 username,password
3 写信 只输入收信人发送 发送成功
4 退出
test_send.py
#coding=utf-8
from selenium import webdriver
import unittest, time
from public import login
import xml.dom.minidom
#打开 xml 文档
dom = xml.dom.minidom.parse('E:\\test_object\\test_date\\login.xml')
#得到文档元素对象
root = dom.documentElement
class TestSendMail(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
logins = root.getElementsByTagName('url')
self.base_url=logins[0].firstChild.data
self.verificationErrors = []
#只填写收件人发送邮件
def test_send_mail(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,"testingwtb","a123456")
#写信
driver.find_element_by_css_selector("#_mail_component_47_47 >
span.oz0").click()
#填写收件人
driver.find_element_by_xpath("//*[@class='bz0']/div[2]/div/input")
.send_keys('testingwtb@126.com')
#发送邮件
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/header
/div/div/div/span[2]").click()
driver.find_element_by_xpath("//*[@class='nui-msgbox-ft-btns']
/div/span").click()
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text,u'发送成功')
login.logout(self)
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
在只输入收件人发送邮件时,系统会弹框提示“确定真的不需要写主题吗?”,在用例中默认 click
“确定”。邮件发送成功,验证“发送成功”的提示信息。
用例 002 :
用例 id test_sendmail.py 发送邮件
步骤 动作 数据 验证点
1 打开邮箱 http://webcloud.126.com
2 用户登陆 username,password
3 写信 只输入收信人与主题发送 发送成功
4 退出
在 test_send.py 文件中添加测试用例:
test_send.py
……
#填写收件人、主题发送邮件
def test_send_mail2(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,"testingwtb","a123456")
#写信
driver.find_element_by_css_selector("#_mail_component_47_47 >
span.oz0").click()
#填写收件人和主题
driver.find_element_by_xpath("//*[@class='bz0']/div[2]
/div/input").send_keys('testingwtb@126.com')
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and
@type='text' and @maxlength='256']").send_keys(u'给小明的信')
#发送邮件
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/header
/div/div/div/span[2]").click()
time.sleep(3)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text,u'发送成功')
login.logout(self)
……
在这个用例中,相比较前一条用例,增加了邮件主题的填写,填写邮件主题为“给小明的信”。有了
收件人和主题就算是一封完整的邮件了,点击发送就可以发送成功了。
……
#填写收件人、主题和附件发送邮件
def test_send_mail3(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,"testingwtb","a123456")
#写信
driver.find_element_by_css_selector("#_mail_component_47_47 >
span.oz0").click()
#填写收件人和主题
driver.find_element_by_xpath("//*[@class='bz0']/div[2]
/div/input").send_keys('testingwtb@126.com')
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and
@type='text' and @maxlength='256']").send_keys(u'给小明的信')
#上传附件
driver.find_element_by_class_name("O0").send_keys('D:\\upfile.txt')
#发送邮件
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/header
/div/div/div/span[2]").click()
time.sleep(3)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text,u'发送成功')
login.logout(self)
……
126 邮箱上传附件也是通过<input>标签实现的,所以可以通过 send_keys()方法发送一个本地文件的
绝对路径从而实现附件的上传。
……
#填写收件人、主题和正文发送邮件
def test_send_mail4(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,"testingwtb","a123456")
#写信
driver.find_element_by_css_selector("#_mail_component_47_47 >
span.oz0").click()
#填写收件人和主题
driver.find_element_by_xpath("//*[@class='bz0']/div[2]
/div/input").send_keys('testingwtb@126.com')
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and
@type='text' and @maxlength='256']").send_keys(u'给小明的信')
#定位富文本表单
class_name = driver.find_element_by_class_name('APP-editor-iframe')
driver.switch_to_frame(class_name)
#编写邮件正文
driver.find_element_by_tag_name('body').send_keys(u'你好,小明好久不见。')
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text,u'发送成功')
login.logout(self)
……
126 邮箱是一个富文本输入框,通过前端定位发现它是嵌套在页面里的 iframe。
Firebug
……
<div class="APP-editor-edtr-mask" style="display:none"></div>
<iframe class="APP-editor-iframe" frameborder="0"
style="position:absolute" tabindex="1">
<html webdriver="true">
<head></head>
<body class="nui-scroll" contenteditable="true">邮件正文
</body>
</html>
</html>
</iframe>
……
通过编写邮件正文内容发现,邮件的内容是写在 body 标签之间的,这和我们往常操作的 input 标签
并不一样,但其实 send_key()同样可以向 body 标签之间输入内容。