软件测试之自动化测试
【摘要】 基于selenium对网页接口进行自动化测试。
基于selenium对网页接口进行自动化测试
执行环境:python3.6脚本
单个测试百度搜索框
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com/")
#driver.find_element_by_css_selector(".s_ipt").send_keys("西安工业大学")
#driver.find_element_by_css_selector("#su").click()
driver.find_element_by_id("kw").send_keys("西安工业大学")
driver.find_element_by_id("su").click()
#设置长宽
driver.set_window_size(400,400)
#智能等待
driver.implicitly_wait(10)
driver.find_element_by_partial_link_text("百度百科").click()
driver.maximize_window()
print("title:"+driver.title)
print("url:"+driver.current_url)
time.sleep(8)
driver.quit()
指定html页面测试
from selenium import webdriver
import time
import os
driver=webdriver.Chrome()
#指定文件路径下的html
file='file:///'+os.path.abspath("C:/Users/Administrator/Desktop/admin.html")
driver.get(file)
driver.find_element_by_id("su").click()
#设置长宽
driver.set_window_size(400,400)
#智能等待
driver.implicitly_wait(10)
driver.maximize_window()
print("title:"+driver.title)
print("url:"+driver.current_url)
time.sleep(8)
driver.quit()
多个测试点,采用类封装,逐个运行测试
from selenium import webdriver
import unittest
import time
class Baidu22(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("西安工业大学")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("NASA")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("911")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
from selenium import webdriver
import unittest
import time
class Baidu11(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("西安")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("china")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("119")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
from selenium import webdriver
import unittest
import time
class Baidu22(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("b站")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("NBA")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("918")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
多个测试用例,封装为套件,整体测试
work为工程 baidu1 baidu2为封装的测试用例
import unittest
from work import baidu
from work import baidu1
from work import baidu2
def createsuite():
suite=unittest.TestSuite()
suite.addTest(baidu.baidu("test_CN"))
suite.addTest(baidu.baidu("test_EN"))
suite.addTest(baidu1.baidu11("test_CN"))
suite.addTest(baidu1.baidu11("test_EN"))
suite.addTest(baidu2.baidu22("test_CN"))
suite.addTest(baidu2.baidu22("test_EN"))
return suite
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTes
tRunner(verbosity=1)
runner.run(suite)
makesuit解决测试用例逐个添加,适用于大量测试用例
import unittest
from work import baidu
from work import baidu1
from work import baidu2
def createsuite():
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(baidu1.baidu11))
suite.addTest(unittest.makeSuite(baidu2.baidu22))
return suite
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTestRunner(verbosity=1)
runner.run(suite)
discovers解决多个测试脚本,把文件夹下所有脚本中的test方法添加在测试套件中
import unittest
def createsuite():
discovers=unittest.defaultTestLoader.discover("../work",pattern="baidu*.py",top_level_dir=None)
print(discovers)
return discovers
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTestRunner(verbosity=1)
————————————————
版权声明:本文为CSDN博主「AllenSquirrel」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36131611/article/details/119182277
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)