Skip to content

Commit

Permalink
feat: outsource _navTo
Browse files Browse the repository at this point in the history
  • Loading branch information
vobu committed Feb 13, 2022
1 parent bd9b3d2 commit 9eaa97c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
30 changes: 30 additions & 0 deletions client-side-js/_navTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function clientSide__navTo(sComponentId, sName, oParameters, oComponentTargetInfo, bReplace) {
return await browser.executeAsync(
(sComponentId, sName, oParameters, oComponentTargetInfo, bReplace, done) => {
window.bridge.waitForUI5(window.wdi5.waitForUI5Options).then(() => {
window.wdi5.Log.info(`[browser wdi5] navigation to ${sName} triggered`)

const router = sap.ui.getCore().getComponent(sComponentId).getRouter()
const hashChanger =
parseFloat(sap.ui.version) < 1.75
? sap.ui.core.routing.HashChanger.getInstance()
: router.getHashChanger()

// on success result is the router
hashChanger.attachEvent("hashChanged", (oEvent) => {
done(["success", parseFloat(sap.ui.version) < 1.75 ? hashChanger.getHash() : hashChanger.hash])
})

// get component and trigger router
// sName, oParameters?, oComponentTargetInfo?, bReplace?
router.navTo(sName, oParameters, oComponentTargetInfo, bReplace)
return parseFloat(sap.ui.version) < 1.75 ? hashChanger.getHash() : hashChanger.hash
})
},
sComponentId,
sName,
oParameters,
oComponentTargetInfo,
bReplace
)
}
73 changes: 73 additions & 0 deletions src/lib/wdi5-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { clientSide_injectUI5 } from "../../client-side-js/injectUI5"
import { clientSide_getSelectorForElement } from "../../client-side-js/getSelectorForElement"
import { clientSide__checkForUI5Ready } from "../../client-side-js/_checkForUI5Ready"
import { clientSide_getUI5Version } from "../../client-side-js/getUI5Version"
import { clientSide__navTo } from "../../client-side-js/_navTo"

import { Logger as _Logger } from "./Logger"
const Logger = _Logger.getInstance()
Expand Down Expand Up @@ -211,6 +212,54 @@ export async function addWdi5Commands() {
await _waitForUI5()
await _writeScreenshot(fileAppendix)
})

browser.addCommand("goTo", async (oOptions) => {
// allow for method sig to be both
// wdi5()...goTo("#/accounts/create")
// wdi5()...goTo({sHash:"#/accounts/create"})
let sHash
if (typeof oOptions === "string") {
sHash = oOptions
} else {
sHash = oOptions.sHash
}
const oRoute = oOptions.oRoute

if (sHash && sHash.length > 0) {
const url = (browser.config as wdi5Config).wdi5["url"]

// navigate via hash if defined
if (url && url.length > 0 && url !== "#") {
// prefix url config if is not just a hash (#)
const currentUrl = await browser.getUrl()
const alreadyNavByHash = currentUrl.includes("#")
const navToRoot = url.startsWith("/")
if (alreadyNavByHash && !navToRoot) {
await browser.url(`${currentUrl.split("#")[0]}${sHash}`)
} else {
await browser.url(`${url}${sHash}`)
}
} else if (url && url.length > 0 && url === "#") {
// route without the double hash
await browser.url(`${sHash}`)
} else {
// just a fallback
await browser.url(`${sHash}`)
}
} else if (oRoute && oRoute.sName) {
// navigate using the ui5 router
// sComponentId, sName, oParameters, oComponentTargetInfo, bReplace
await _navTo(
oRoute.sComponentId,
oRoute.sName,
oRoute.oParameters,
oRoute.oComponentTargetInfo,
oRoute.bReplace
)
} else {
Logger.error("ERROR: navigating to another page")
}
})
}

/**
Expand Down Expand Up @@ -274,3 +323,27 @@ function _getDateString() {
var x = new Date()
return `${x.getMonth() + 1}-${x.getDate()}-${x.getHours()}-${x.getMinutes()}-${x.getSeconds()}`
}

/**
* navigates to a UI5 route using the Component router
* @param {String} sComponentId
* @param {String} sName
* @param {Object} oParameters
* @param {Object} oComponentTargetInfo
* @param {Boolean} bReplace
*/
async function _navTo(sComponentId, sName, oParameters, oComponentTargetInfo, bReplace) {
const result = await clientSide__navTo(sComponentId, sName, oParameters, oComponentTargetInfo, bReplace)
if (Array.isArray(result)) {
if (result[0] === "error") {
Logger.error("ERROR: navigation using UI5 router failed because of: " + result[1])
return result[1]
} else if (result[0] === "success") {
Logger.log(`SUCCESS: navigation using UI5 router to hash: ${JSON.stringify(result[0])}`)
return result[1]
}
} else {
// Guess: was directly returned
return result
}
}

0 comments on commit 9eaa97c

Please sign in to comment.