Business Example
In this exercise, you will write OPA tests.
SAP Business Technology Platform and its tools are cloud services. Due to the nature of cloud software, step procedures and the naming of fields and buttons may differ from the exercise solution.
Task 1: Create the SAP Fiori Freestyle Application to Test
Steps
Log into your SAP Business Application Studio.
Perform this step as shown in a previous exercise.
Create a new SAP Fiori Freestyle project using the following information:
SAP Fiori Freestyle Project Information
Field Value Template SAP Fiori application Template Type SAP Fiori Template Basic Data sources None View name Main Module name ux402_opa Application title OPA Tests Application namespace student##.com.sap.training.ux402.opa Description An OPA-test Application. Project folder path /home/user/projects Leave the rest of the fields and radio buttons set to their default values.
Perform this step as shown in a previous exercise.
In the
Main.view.xml
file, add asap.m.Button
element in the header of the page, with the following attributes:Attribute-value Pairs for the sap.m.Button Element
Attribute Value id headerButton text Header Button Open the
Main.view.xml
file with the Code Editor of your SAP Business Application Studio.Add the
headerContent
aggregation to thePage
element. Use the following code:Add an
sap.m.Button
element to theheaderContent
aggregation with the listed attribute-value combinations. Use the following code:Save your changes
Add the following elements with the listed attribute-value combinations to the
content
aggregation of thesap.m.Page
element.Attribute-value Pairs for the Elements
Elements Attributes Value sap.m.Label text Firstname sap.m.Label id firstNameLabel sap.m.Input value John sap.m.Input id firstNameInput sap.m.Label text Lastname sap.m.Label id lastNameLabel sap.m.Input value Johnson sap.m.Input id lastNameInput If it is not already open, open the
Main.view.xml
file.Add the elements with the listed attribute-value combinations to the
content
aggregation of thesap.m.Page
element. Use the following code:Code snippetExpand<Label id="firstNameLabel" text="Firstname"/> <Input id="firstNameInput" value="John"/> <Label id="lastNameLabel" text="Lastname"/> <Input id="lastNameInput" value="Johnson"/>
Save your changes.
Add an
sap.m.Button
element after the lastsap.m.Input
element to thecontent
aggregation with the following attribute-value combinations.Attribute-value Pairs for the sap.m.Button Element
Attribute Value id helloButton text Press me press onPress If not already open, open the
Main.view.xml
file in the Code Editor.Add the
sap.m.Button
element with the attribute-value combinations in the table provided to thecontent
aggregation. Use the following code:Your code should now look like the following :
Code snippetExpand<Page id="page" title="{i18n>title}"> <headerContent> <Button id="headerButton" text="Header Button"/> </headerContent> <content> <Label id="firstNameLabel" text="Firstname"/> <Input id="firstNameInput" value="John"/> <Label id="lastNameLabel" text="Lastname"/> <Input id="lastNameInput" value="Johnson"/> <Button id="helloButton" text="Press me" press="onPress"/> </content> </Page>
Save your changes and close the
Main.view.xml
file.
Implement the
onPress
event handler function in theMain.controller.js
file for thepress
event of thesap.m.Button
element of theMain.view.xml
file. Add anoEvent
parameter to the function. The event handler function should show ansap.m.MessageBox
with the textButton pressed
. Use the show function of thesap.m.MessageBox
to display the message.Open the
Main.controller.js
file, which is located in the controller folder of your project.Add a reference to the
sap.m.MessageBox
in the module definition. Use the following code:Code snippetExpandsap.ui.define([ "sap/ui/core/mvc/Controller", "sap/m/MessageBox" ], function (Controller, MessageBox) { "use strict";
Add an
onPress
function with a parameter,oEvent
, to the controller implementation. Use the following code:Code snippetExpandreturn Controller.extend("student##.com.sap.training.ux402.opa.ux402opa.controller.Main", { onInit: function () { }, onPress: function (oEvent) { } });
Implement the
onPress
function. On theMessageBox
reference, invoke theshow
function and pass the string literalButton pressed
to theshow
function, as in the following code:
Run your application. Choose the Press me button. A dialog appears with the message Button pressed.
Open the context menu on your project folder (ux402_opa).
Select Preview Application.
Choose start fiori run –open ".." on the opened dialog.
If prompt, select Open on the dialog in the bottom right corner.
Task 2: Implement OPA Tests
Steps
Implement a test function with the name
iShouldFindAButton
in thewebapp/test/integration/pages/Main.js
file and check if the Main-view implemented in the previous task contains a control with the idhelloButton
.Attribute-value Pairs for this.waitFor
Attribute Value viewName sViewName id helloButton success Callback function with one parameter named oButton errorMessage Did not find the hellobutton timeout 3 Open the
webapp/test/integration/pages/Main.js
file.Implement the
iShouldFindAButton
function after theiShouldSeeThePageView
function, using the following code:Code snippetExpandiShouldFindAButton: function () { return this.waitFor({ viewName: sViewName, id: "helloButton", success: function (oButton) { }, errorMessage: "Did not find the hello-Button", timeout: "3" }); }
Implement the
success
function. Trigger thepress
event on the passedoButton
object and print out an assertion of typeok
. Use the following code:Code snippetExpandoButton.$().trigger("press"); Opa5.assert.ok(oButton.getId(), "Button with the given ID found");
Save your work. Your implementation should now look like the following:
Code snippetExpandiShouldFindAButton: function () { return this.waitFor({ viewName: sViewName, id: "helloButton", success: function (oButton) { oButton.$().trigger("press"); Opa5.assert.ok(oButton.getId(), "Button with the given ID found"); }, errorMessage: "Did not find the hello-Button", timeout: "3" }); }
Add the assertion
iShouldFindAButton
to thewebapp/test/integration/NavigationJourney.js
file.Open the
NavigationJourney.js
file and add theiShouldFindAButton
as a new assertion. Add the following code after theiShouldSeeThePageView()
assertion:Save your changes. Your code should look like the following:
Code snippetExpandopaTest("Should see the initial page of the app", function (Given, When, Then) { // Arrangements Given.iStartMyApp(); // Assertions Then.onTheAppPage.iShouldSeeTheApp(); Then.onTheViewPage.iShouldSeeThePageView(); Then.onTheViewPage.iShouldFindAButton(); //Cleanup Then.iTeardownMyApp(); });
Start your application in int-tests mode. A new browser tab opens and the test case starts. When the test is complete, you can expand the test result to get more details. The results screen should look like the one shown in the following figure.
Open the context menu on our project folder (ux402_opa).
Select Preview Application.
Choose int-tests fiori run –open ".." on the opened dialog.
If prompted, choose Open on the dialog in the bottom right corner.
A new browser tab opens, and the test case starts. When the test is complete, you can expand the test result to get more details.