软件测试自动化测试

Posted AllenSquirrel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件测试自动化测试相关的知识,希望对你有一定的参考价值。

基于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)

以上是关于软件测试自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

浅析Minium,微信小程序自动化测试框架

OpenGL ES之“深度测试”与“模板测试”的使用流程

如何在片段着色器中进行自定义模板测试

深度测试

typescript Angular 2测试片段。代码库https://developers.livechatinc.com/blog/category/programming/angular-2/