Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCSSProperty.js #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions mabl snippets/getCSSProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Gets the specified CSS Property of a given element
*
* @param {object} mablInputs - Object containing mabl inputs such as variables (mablInputs.variables).
* Use mablInputs.variables.user for user defined variables
* (For example myVar may be accessed as mablInputs.variables.user.myVar)
*
* @param {function} callback - A callback function that must be called to complete
* the javascript step and provide a value to the following
* steps of the flow/journey. A return statement from this
* function call will not provide any results for use
* in the following steps in this flow or journey.
*
* Simplified Example:
* function mablJavaScriptStep(mablInputs, callback) {
* let selector = document.querySelector('[class="button demo"] div:nth-of-type(2)');
* let element = getComputedStyle(selector);
* callback(element.getPropertyValue('background-color'));
* }
*/

function mablJavaScriptStep(mablInputs, callback) {
// using an xpath
//let selector = getElementByXpath(mablInputs.variables.user.selector_xpath);

// using a css selector
let selector = document.querySelector(mablInputs.variables.user.selector_css);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of querySelector isn't a selector, but rather an element. Would it make sense to change this variable name to element and the one below this to style?

[nit] - selector_css could be be selectorCss to match the camel casing used below.


let element = getComputedStyle(selector);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I'm curious, we don't need window.getComputedStyle here right? it seems to work fine in dev tools when I'm testing it without window. I don't much about how the scoping works for our javascript snippets. Would be an easy thing to test though.

callback(element.getPropertyValue(mablInputs.variables.user.css_property));
}

function getElementByXpath(path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea having this here, it's handy :-)

return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}