Step 28: Integration Test with OPA集成测试OPA

一、基本概念

OPA是一个基于JavaScript的集成测试框架,作用是:

  • ✅ 提供真实的浏览器测试环境

  • ✅ 支持复杂的用户交互模拟

  • ✅ 与UI5框架深度集成

  • ✅ 适合企业级应用测试

  • ✅ 支持持续集成和自动化

二、练习

新建文件夹28,复制练习UI5_Walkthrough_Step 26:Mock Server Configuration 模拟服务器配置-

文件夹下内容,在test文件下新建文件夹integration,integration文件夹下再新建文件夹pages

2.1 webapp\test\integration\pages\App.js

App.js定义了对应用程序中某个页面或视图的操作和断言,定义测试框架的页面对象

  • 声明模块依赖:"sap/ui/test/Opa5","sap/ui/test/actions/Press"
复制代码
Opa5.createPageObjects :页面对象模式
actions:定义测试的操作,这里测试的是HelloPanel页面的id为helloDialogButton控件执行Press点击的验证
assertions:验证断言,waitForOPA5核心方法,等待控件出现,success 回调:找到控件后执行验证断言
  • sViewName = "sap.ui5.walkthrough.view.HelloPanel"为验证的页面视图文件路径

如下为验证按钮弹窗的运行结果

javascript 复制代码
sap.ui.define([
	"sap/ui/test/Opa5",
	"sap/ui/test/actions/Press"
], function (Opa5, Press) {
	"use strict";

	var sViewName = "sap.ui5.walkthrough.view.HelloPanel";

	Opa5.createPageObjects({
		onTheAppPage: {
			actions: {
				iPressTheSayHelloWithDialogButton: function () {
					return this.waitFor({
						id: "helloDialogButton",
						viewName: sViewName,
						actions: new Press(),
						errorMessage: "Did not find the 'Say Hello With Dialog' button on the HelloPanel view"
					});
				}
			},

			assertions: {
				iShouldSeeTheHelloDialog: function () {
					return this.waitFor({
						controlType: "sap.m.Dialog",
						success: function () {
							// we set the view busy, so we need to query the parent of the app
							Opa5.assert.ok(true, "The dialog is open");
						},
						errorMessage: "Did not find the dialog control"
					});
				}
			}
		}
	});
});

2.2 webapp\test\integration\NavigationJourney.js

复制代码
   ├── 定义测试模块
   ├── 定义测试用例 (opaTest)
   └── 调用App.js中的方法

NavigationJourney.js,测试用例集合,定义具体的测试场景和步骤,依赖于App.js,使用页面对象中的操作和断言(App.js)来模拟用户行为。

  • 初始化mock server(用于模拟数据):mockserver.init();

  • 启动UI组件(即我们的应用):Given.iStartMyUIComponent

  • 执行动作:点击打开对话框的按钮。When.onTheAppPage

  • 断言:对话框应该打开:Then.onTheAppPage

  • 清理:关闭应用:Then.iTeardownMyApp();

Given-When-Then 模式:

是一种用于编写测试用例的结构化方法,它源自行为驱动开发(BDD)。它将测试用例分解为三个部分:

  1. Given:描述测试开始前的初始状态或上下文(前置条件)。

  2. When:描述被测试的行为或事件(触发动作)。

  3. Then:描述预期的结果或断言(后置条件)。

javascript 复制代码
/*global QUnit, opaTest*/

sap.ui.define([
	"sap/ui/demo/walkthrough/localService/mockserver",
	"sap/ui/test/opaQunit",
	"./pages/App"
], function (mockserver) {
	"use strict";

	QUnit.module("Navigation");

	opaTest("Should open the Hello dialog", function (Given, When, Then) {
		// initialize the mock server
		mockserver.init();

		// Arrangements
		Given.iStartMyUIComponent({
			componentConfig: {
				name: "sap.ui5.walkthrough"
			}
		});

		//Actions
		When.onTheAppPage.iPressTheSayHelloWithDialogButton();

		// Assertions
		Then.onTheAppPage.iShouldSeeTheHelloDialog();

		// Cleanup
		Then.iTeardownMyApp();
	});
});

2.3 webapp\test\integration\opaTests.qunit.html

opaTests.qunit.html,用于在浏览器中加载和运行OPA测试,集成测试的启动页面

复制代码
  ├── 加载UI5框架
  ├── 加载QUnit测试框架
  └── 加载opaTests.qunit.js
html 复制代码
<!DOCTYPE html>
<html>
<head>
	<title>Integration tests for SAPUI5 Walkthrough</title>
	<meta charset="utf-8">

	<script
		id="sap-ui-bootstrap"
		src="https://sdk.openui5.org/resources/sap-ui-core.js"
		data-sap-ui-theme="sap_belize"
		data-sap-ui-resourceroots='{
			"sap.ui5.walkthrough": "../../"
		}'
		data-sap-ui-animation="false"
		data-sap-ui-compatVersion="edge"
		data-sap-ui-async="true">
	</script>

	<link rel="stylesheet" type="text/css" href="https://sdk.openui5.org/resources/sap/ui/thirdparty/qunit-2.css">

	<script src="https://sdk.openui5.org/resources/sap/ui/thirdparty/qunit-2.js"></script>
	<script src="https://sdk.openui5.org/resources/sap/ui/qunit/qunit-junit.js"></script>

	<script src="opaTests.qunit.js"></script>
</head>
<body>
	<div id="qunit"></div>
	<div id="qunit-fixture"></div>
</body>
</html>

2.4 webapp\test\integration\opaTests.qunit.js

复制代码
 opaTests.qunit.js ,测试配置和启动的脚本 
   ├── 配置OPA框架
   │   ├── Opa5.extendConfig({...})
   │   └── 设置全局参数
   └── 启动测试套件
javascript 复制代码
/* global QUnit */

QUnit.config.autostart = false;

sap.ui.getCore().attachInit(function () {
	"use strict";

	sap.ui.require([
		"sap/ui5/walkthrough/test/integration/NavigationJourney"
	], function () {
		QUnit.start();
	});
});

2.4 运行结果

http://localhost:8080/webapp/test/integration/opaTests.qunit.html

相关推荐
未定义.2214 天前
第5篇:进阶优化:数据驱动+日志体系+失败重试实战
python·ui·自动化·jenkins·集成测试·pytest
未定义.2214 天前
第4篇:企业级框架搭建,Pytest+PO模式从0到1实战
python·ui·自动化·jenkins·集成测试·pytest
Wang201220135 天前
芯片铝垫钝化层作用和厚度
集成测试
未定义.2215 天前
第3篇:UI自动化核心操作:输入、点击、弹窗、下拉框全场景实战
运维·python·ui·自动化·jenkins·集成测试·pytest
Wang201220136 天前
芯片做好bumping后保存条件和多久做进一步封装好点
集成测试
vx-bot5556667 天前
企业微信接口集成测试策略与实践指南
log4j·集成测试·企业微信
帅次8 天前
从开发到部署:软件实现、测试与交付全流程核心技术解析
功能测试·单元测试·测试用例·集成测试·压力测试·模块测试·安全性测试
Wang2012201310 天前
CP(Chip Probing) 探针材质的选择和针头类型的选择
集成测试
QH1392923188011 天前
Anritsu MT8821C MT8000A无线电通信分析仪
网络·科技·集成测试·模块测试
熊文豪11 天前
LazyLLM 生态集成测试:与主流开源软件的协同效果评测
集成测试·lazyllm