自动化测试:
自动化元素定位-》管理页面元素-》选择套用设计模式来进行管理
(1)基于关键字形态。所有元素信息全部保存在文件中
关键字驱动+数据驱动的实现模式 (面向对象)
数据驱动(代码与数据分离,确保代码稳定性)
(2)基于POM形态。所有元素信息与页面对象关联
pom设计模式:基于页面对象实现自动化测试
pom结构:基类:关键字驱动封装的底层类
页面对象类:提供不同页面的对象。在实际测试中通过调用页面对象实现自动化测试流程
测试用例类:unitest、pytest
测试数据类:yaml,excel
单接口多条件覆盖
链路测试
———————————————————————————————————————————
基于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.TextTestRunner(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)
runner.run(suite)