Skip to content

Commit

Permalink
feat: add option to ignore urls for auto waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Jan 10, 2024
1 parent 6421e80 commit 3a0445d
Show file tree
Hide file tree
Showing 10 changed files with 909 additions and 22 deletions.
53 changes: 53 additions & 0 deletions client-side-js/injectXHRPatch.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
async function clientSide_injectXHRPatch(config, browserInstance) {
return await browserInstance.executeAsync((config, done) => {
const originalFetch = window.fetch

const autoWaitUrlIgnoreRegex = config.wdi5.autoWaitUrlIgnoreRegex
function checkURL(url) {
return autoWaitUrlIgnoreRegex?.map((regex) => new RegExp(regex))?.some((regex) => url.match(regex)) || false
}

//Load the XHRWaiter before our overwrite so we are called first
sap.ui.require(
["sap/ui/thirdparty/sinon", "sap/ui/test/autowaiter/_XHRWaiter", "sap/ui/test/autowaiter/_fetchWaiter"],
function (sinon, _XHRWaiter, _fetchWaiter) {
// Hook into XHR open for sinon XHRs
const fnOriginalFakeOpen = sinon.FakeXMLHttpRequest.prototype.open
sinon.FakeXMLHttpRequest.prototype.open = function () {
return fnOriginalFakeOpen.apply(this, hooketoXHROpen.apply(this, arguments))
}

// Hook into XHR open for regular XHRs
const fnOriginalOpen = XMLHttpRequest.prototype.open
XMLHttpRequest.prototype.open = function () {
return fnOriginalOpen.apply(this, hooketoXHROpen.apply(this, arguments))
}

function hooketoXHROpen(method, url, async) {
//The ignore property will force the OPA5 _XHRWaiter to ignore certain calls for auto waiting
//https://github.com/SAP/openui5/blob/45e49887f632d0a8a8ef195bd3edf10eb0be9015/src/sap.ui.core/src/sap/ui/test/autowaiter/_XHRWaiter.js
//This ist the XHR request instance so setting it here will only affect the specific request
this.ignored = checkURL(url)

return arguments
}

const sapFetch = window.fetch

window.fetch = function (resource) {
const url = typeof resource === "object" ? resource.url : resource
if (checkURL(url)) {
return originalFetch.apply(this, arguments)
} else {
return sapFetch.apply(this, arguments)
}
}
done(true)
}
)
}, config)
}

module.exports = {
clientSide_injectXHRPatch
}
5 changes: 4 additions & 1 deletion examples/ui5-js-app/e2e-test-config/wdio-ui5tooling.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const merge = require("deepmerge")

const _config = {
specs: [join("..", "webapp", "test", "e2e", "basic.test.js"), join("webapp", "test", "e2e", "hash-nav.test.js")],
baseUrl: "http://localhost:8080/index.html"
baseUrl: "http://localhost:8080/index.html?isui5toolingTest=true",
wdi5: {
autoWaitUrlIgnoreRegex: [".*/Categories.*"]
}
}

exports.config = merge(baseConfig, _config)
3 changes: 2 additions & 1 deletion examples/ui5-js-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@wdio/mocha-framework": "^8",
"@wdio/spec-reporter": "^8",
"ui5-middleware-simpleproxy": "latest",
"wdio-ui5-service": "*"
"wdio-ui5-service": "*",
"@sap-ux/ui5-middleware-fe-mockserver": "latest"
},
"dependencies": {
"@wdio/sauce-service": "^8"
Expand Down
13 changes: 13 additions & 0 deletions examples/ui5-js-app/scripts/delayedMockServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const femiddleware = require("@sap-ux/ui5-middleware-fe-mockserver")

module.exports = async function middleware(middlewareConfig) {
const feMiddleware = await femiddleware(middlewareConfig)
return async (req, res, next) => {
if (req.originalUrl.startsWith("/V2") && req.originalUrl.includes("/Categories")) {
await new Promise((resolve) => setTimeout(resolve, 1000))
feMiddleware(req, res, next)
return
}
next()
}
}
16 changes: 16 additions & 0 deletions examples/ui5-js-app/ui5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ server:
configuration:
baseUri: "https://services.odata.org/V2"
strictSSL: false
- name: delayed-mockserver
beforeMiddleware: ui5-middleware-simpleproxy
configuration:
service:
urlBasePath: "/V2/Northwind/Northwind.svc"
name: ""
metadataXmlPath: "./webapp/localService/metadata.xml"
generateMockData: true
---
specVersion: "1.0"
metadata:
name: delayed-mockserver
kind: extension
type: server-middleware
middleware:
path: scripts/delayedMockServer.js
14 changes: 14 additions & 0 deletions examples/ui5-js-app/webapp/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ sap.ui.define(

// set the device model
this.setModel(models.createDeviceModel(), "device")

const url = new URL(location.href)
if (url.searchParams.get("isui5toolingTest")?.toLocaleLowerCase() === "true") {
const startXHR = () => {
this.getModel().read("/Categories", {
success: startXHR
})
}
startXHR()
const startFetch = () => {
fetch("/V2/Northwind/Northwind.svc/Categories").then(startFetch)
}
startFetch()
}
}
})
}
Expand Down
Loading

0 comments on commit 3a0445d

Please sign in to comment.