OPA5入门教程

文件结构

integration/navigationJourney.js
OPA测试用例描述

sap.ui.require([
    //需要引入opaQunit,则可以按照Qunit的书写方式进行书写
    "sap/ui/test/opaQunit"
], function () {
    "use strict";

    // 一个测试module,类似于Qunit
    QUnit.module("Navigation");

    // 一个测试用例,第一个参数字符串描述测试用例功能
    opaTest("Should open the hello dialog", function (Given, When, Then) {

        // Arrangements,测试的前期准备工作,例如应用程序启动,特定数据存在
        Given.iStartMyAppInAFrame(jQuery.sap.getResourcePath("sap/ui/demo/app/test", ".html"));

        //Actions,虚拟用户动作,以在Assertions中检查是否到达预期状态,主要操作为查找,操作
        When.onTheAppPage.iPressTheSayHelloWithDialogButton();

        // Assertions,检查用户操作是否达到预期,主要操作为查找,断言判断,最后关闭iFrame
        Then.onTheAppPage.iShouldSeeTheHelloDialog().
            and.iTeardownMyAppFrame();
    });
});

integration/pages/App.js
具体定义navigationJourney中的各种函数

sap.ui.require([
    "sap/ui/test/Opa5" //引入Opa5,在其基础上使用createPageObjects()创建辅助对象,定义函数体
    "导入需要用的依赖"
    "sap/ui/test/actions/Press" //可以在waitFor中的actions : new Press()进行按键操作
    "sap/ui/test/matchers/PropertyStrictEquals"  //可以在waitFor中的matchers : new PropertyStrictEquals({ name : "text", value : "I got pressed"})匹配找到的元素的某属性是否和期待的一致
],
function (Opa5) {
    "use strict";
    Opa5.createPageObjects({
        // opa对于AppPage的辅助对象
        onTheAppPage: {
            //Actions,虚拟用户动作,以在Assertions中检查是否到达预期状态,主要操作为查找,success中进行操作
            actions: {
                iPressTheSayHelloWithDialogButton: function () {
                    return this.waitFor({
                        controlType: "sap.m.Button",
                        success: function (aButtons) {
                            aButtons[0].$().trigger("tap");
                        },
                        errorMessage: "Did not find the helloDialogButton button on the app page"
                    });
                }
            },

            // Assertions,检查用户操作是否达到预期,主要操作为查找,success中进行断言判断
            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"
                    });
                }
            }
        }
    });
});

integration/opaTests.qunit.html
测试页面


<html>
<head>
    <title>Opa tests for SAPUI5 Walkthroughtitle>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta charset="utf-8">

    <script id="sap-ui-bootstrap"
            src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
            data-sap-ui-resourceroots='{
            //设置相对路径到该文件所在路径
            "sap.ui.demo.walkthrough.test.integration": "./",
            "sap.ui.demo.app.test" : "../mockServer"
        }'>
    script>

    <script>
        jQuery.sap.require("sap.ui.qunit.qunit-css");
        jQuery.sap.require("sap.ui.thirdparty.qunit");
        jQuery.sap.require("sap.ui.qunit.qunit-junit");
        jQuery.sap.require("sap.ui.test.opaQunit");
        jQuery.sap.require("sap.ui.test.Opa5");

        // 导入pages(即用例中调用的函数具体定义)
        jQuery.sap.require("sap.ui.demo.walkthrough.test.integration.pages.App");

        // 导入journeys(即用例描述)
        jQuery.sap.require("sap.ui.demo.walkthrough.test.integration.navigationJourney");
    script>

head>
<body>
<div id="content">div>
<div id="qunit">div>
<div id="qunit-fixture">div>
body>
html>

测试开始:
可以以UIComponent开始测试或者以iFrame开始测试,每次开始测试时都要将上一个UIComponet或者iFrame拆卸
UIComponet:

.iStartMyUIComponent({
    componentConfig: {
        name: "samples.components.button"
    } ,
    hash: "newHashValue"
});

拆卸:

.iTeardownMyUIComponent();

iFrame:

.iStartMyAppInAFrame("index.html?responderOn=true");

拆卸:

new sap.ui.test.Opa5().iTeardownMyApp();
// or
new sap.ui.test.Opa5().iTeardownMyAppFrame();

你可能感兴趣的:(SAP,UI5)