The removeElementTargetAttribute extension removes the target attribute from a stored page element. This is commonly used for links that would otherwise open in a new browser tab or window.
This is useful when an automated journey needs to click a link, such as a PDF or download link, but the application opens that link in a new tab. Removing target lets the same click continue in the current browsing context, which can make the following journey steps more stable.
Parameters:
-
storedElementrequired, a string containing the stored element details returned by a previous Virtuoso step such asstore element details of ... in $linkElement. The documentation may describe this value aslinkElement, but the source code expects the configured extension input name to bestoredElement.
Note: Pass the stored element variable into the storedElement input. The stored value must contain a selectors array with supported selector types such as CSS_SELECTOR, XPATH, XPATH_ID, or ID.
How to apply this to your journey
Use the extension in a journey by calling removeElementTargetAttribute with the execute command. Pass the stored element variable to the extension input using as storedElement.
Note: This extension changes the live DOM during the test session. It does not click the link itself; run it before the click step that should stay in the same window.
execute "removeElementTargetAttribute" using "$linkElement" as storedElementYou can use this immediately before clicking a link that normally opens a new tab.
store element details of "Open new tab" in $linkElement
execute "removeElementTargetAttribute" using "$linkElement" as storedElement
click on "Open new tab"You can also store the element details in a variable and reuse that variable in the extension call.
execute "removeElementTargetAttribute" using "$storedLink" as storedElementstore element details of "Download PDF" in $storedLink
execute "removeElementTargetAttribute" using "$storedLink" as storedElement
click on "Download PDF"This extension performs a DOM action and does not return meaningful data. If the step succeeds, the selected element has had its target attribute removed.
This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension depends on the stored element details still resolving to the intended element at the moment the step runs. It parses storedElement as JSON, loops through the stored selectors, and uses document.querySelector, document.evaluate, or document.getElementById to locate the first matching element. If the page has re-rendered, the selector no longer matches, the element is inside an inaccessible iframe, a closed shadow DOM, or a browser-controlled viewer, the step can fail with a selector error. The extension removes only the HTML target attribute; it does not change click handlers, JavaScript navigation logic, download headers, popup behavior, authentication checks, or browser PDF/download settings. Because this step mutates the current page DOM, use it only where changing the link behavior is acceptable for the test. Cross-browser note: This extension runs in the browser or device selected for the journey execution. Because it uses DOM lookup and attribute mutation APIs, behavior can differ from Virtuoso's default Chromium-based browser in Safari, Firefox, Edge, iOS, Android, or remote-grid executions, especially when links open files, downloads, PDFs, tabs, windows, or mobile browser viewers. Validate it in each browser/device configuration used by your plan.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 05/09/2022
Resources:
This extension does not require any external resource.
// Last updated: 05/09/2022, 13:42:36 UTC
function getElement (selectors) {
let element = null
for (const selectorDetails of selectors) {
switch (selectorDetails.type) {
case 'CSS_SELECTOR':
element = document.querySelector(selectorDetails.value)
break
case 'XPATH_ID':
case 'XPATH':
element = document.evaluate(selectorDetails.value, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
break
case 'ID':
element = document.getElementById(selectorDetails.value)
break
default:
continue
}
// if we found a valid element we don't need to keep searching
if (element) return element
}
throw new Error('Found no suitable selector: ' + JSON.stringify(selectors))
}
const { selectors } = JSON.parse(storedElement)
const element = getElement(selectors)
element.removeAttribute('target')
Comments
0 comments
Please sign in to leave a comment.