new Element()
Example
// Grab a handle to a page element:
const button = api.getElement('input[type="submit"]', 'input', 'a', 'submit button element');
// Perform an action on the element:
button.click();
// Test something about the element:
button.expectAttr('type', 'submit');
// Test that the element is no longer in the DOM:
button.expectDeleted();
    
Members
- 
    
aOrAn
 - 
    
    
One of ['a', 'an'] as appropriate for the description, e.g. 'an input element' or 'a paragraph tag'
 - 
    
description
 - 
    
    
Description of the element, in singular form e.g. 'input element' or 'paragraph tag'
 - 
    
selector
 - 
    
    
This is the selector used with querySelector() to retrieve the element from the DOM.
 
Methods
- 
    
action(actionDescription, actionFn [, options])
 - 
    
    
Perform an action on the element that the student should know about (e.g. interacting with our DOM element to trigger an event). The action is a function that is executed in the browser context, using our element handle as the first parameter. The action description will be listed in the failure report if a subsequent test check fails.
Parameters:
Name Type Argument Description actionDescriptionstring Describes the action performed (past tense). E.g. `Cleared the text in the ${element.description} and unfocused it.`
actionFnPageElementFunction Executed in the browser context.
optionsObject <optional> 
[{}]
Properties
Name Type Argument Description nTimesNumber <optional> 
[1] Number of times to perform the action.
Example
// Perform a generic action on the element -- Mark the string as a SafeString to avoid formatting disappointment. element.action(api.S(`Focused the ${element.description} and unfocused it`), (element) => { element.focus(); element.blur(); }); - 
    
click( [options])
 - 
    
    
Click the middle of the element. The options can be passed through to puppeteer to choose which button and the number of clicks and so on.
Parameters:
Name Type Argument Description optionsObject <optional> 
Puppeteer mouse click options, plus the following:
Properties
Name Type Argument Description nTimesNumber <optional> 
[1] number of times to perform the action.
 - 
    
dispose()
 - 
    
    
Explicitly dispose of the element handle to allow garbage collection in the user page.
 - 
    
evaluate(fn, args)
 - 
    
    
Execute a function in the browser context, using our element handle as the first parameter. If you expect your function to make changes in the page that the student should know about (e.g. triggering an event handler), use action instead. FIXME: Test what happens if we do an evaluate() that causes a page navigation.
Parameters:
Name Type Argument Description fnPageElementFunction Function to execute inside the user's page. The first argument should be the element (DOM node).
args* <repeatable> 
Additional arguments to pass to the function.
Returns:
The result from the PageElementFunction.
- Type
 - *
 
 - 
    
expectAttr(attr, expectedValue)
 - 
    
    
Expect a particular value for an attribute for a specific selector. Shorthand for using expectAttrs() with a single key-value pair. See also expectNotAttr.
Parameters:
Name Type Description attrstring name of the attribute, e.g. 'src'
expectedValuestring  - 
    
expectAttrs(attrObject [, options])
 - 
    
    
Expect a set of attributes on the element to have the desired values.
Parameters:
Name Type Argument Description attrObjectAttributeMap key/value pairs, with each key an attribute that you wish to check.
optionsObject <optional> 
 - 
    
expectComputedStyle(style, expected [, options])
 - 
    
    
Expect a certain style value associated with our element.
Parameters:
Name Type Argument Description stylestring name of the style attribute to check e.g. 'background-color'
expectedstring Use 'none' (or as appropriate for the style) if you expect the attribute not to be set.
optionsObject <optional> 
Properties
Name Type Description afterTransitionstring number of milliseconds to allow for a CSS transition to occur before checking.
 - 
    
expectComputedStyles(styleObject [, options])
 - 
    
    
Expect certain style values associated with our element. Note that if you expect the style attribute not to be set, use an expected value of 'none' as appropriate for the style.
Parameters:
Name Type Argument Description styleObjectStyleAttributeMap key/value pairs, with keys for each style attribute you wish to check
optionsObject <optional> 
Properties
Name Type Description afterTransitionstring number of milliseconds to allow for a CSS transition to occur before checking.
 - 
    
expectDeleted( [options])
 - 
    
    
Expect that our element is no longer present in the DOM.
Parameters:
Name Type Argument Description optionsObject <optional> 
 - 
    
expectExists( [options])
 - 
    
    
Expect that our element still exists in the DOM.
Parameters:
Name Type Argument Description optionsObject <optional> 
 - 
    
expectNotAttr(attr, undesirableValue)
 - 
    
    
Expect that the attribute is not set to the given undesirable value.
Parameters:
Name Type Description attrstring name of the attribute, e.g. 'src'
undesirableValuestring  - 
    
expectTextContent(expected [, options])
 - 
    
    
Check for matching textContent. By default, we collapse whitespace inside the textContent and expected.text in order to do exact string comparison. Turn this off with the noCollapse option.
Parameters:
Name Type Argument Description expectedstring This is matched exactly against the textContent of the element, after collapsing whitespace.
optionsObject <optional> 
Properties
Name Type Argument Description noCollapseboolean <optional> 
[false] Override default behaviour, which is to strip and collapse the whitespace in the textContent before checking.
hintstring <optional> 
If present, this is shown after the failure message details, to give extra feedback to the user.
 - 
    
expectValue(expectedValue)
 - 
    
    
Validate the value of our element.
Parameters:
Name Type Description expectedValuestring  - 
    
getAttr(attr [, options])
 - 
    
    
Get the value of an attribute for the element, or an empty string if the attribute is not set.
Parameters:
Name Type Argument Description attrstring optionsObject <optional> 
Properties
Name Type Argument Description failIfMissingboolean <optional> 
[false] Set to true to fail the test if the attribute is not set.
Returns:
The escaped value of the attribute.
- Type
 - BaseString
 
 - 
    
getComputedStyle(styleAttr)
 - 
    
    
Get the unformatted value of the requested style attribute.
Parameters:
Name Type Description styleAttrstring style attribute requested, e.g. 'top' or 'background-color' etc.
Returns:
An unformatted value. Use formatStyleValue for pretty printing.
- Type
 - BaseString
 
 - 
    
getComputedStyles(styleAttrs)
 - 
    
    
Returns a sub-set of the style map, fetching specific style attributes from the specific element. Returns a mapping of style to value for a specific element (a sub-set of the full style on the element).
Parameters:
Name Type Argument Description styleAttrsArray.<string> <repeatable> 
style attributes requested, e.g. 'top' or 'background-color' etc.
Returns:
Returns an object with only the style attributes requested. Values are unformatted, see formatStyleValue for pretty printing.
- Type
 - StyleAttributeMap
 
 - 
    
getElement(selector, tag, aOrAn, description)
 - 
    
    
Fetches an element handle for the given selector, searching the DOM using the current element sub-tree. We fail the test case if the selector is not found in the Element sub-tree.
Parameters:
Name Type Description selectorstring tagstring aOrAnstring precedes the description wherever we would refer to 'an element'.
descriptionstring describes the element in the singular, e.g. 'cookie element' or '"Add" button'. Occurrences of
'<tag>'are reformatted. For example, with a tag set to'li', a description of<li> elementwill become<code data-lang="tag">li</code> element.Returns:
- Type
 - Element
 
 - 
    
getTextContent( [options])
 - 
    
    
Get the element textContent. By default, we collapse whitespace inside the textContent. Turn this off with the noCollapse option.
Parameters:
Name Type Argument Description optionsObject <optional> 
Properties
Name Type Argument Description noCollapseboolean <optional> 
[false] Override default behaviour, which is to strip and collapse the whitespace in the textContent before checking.
Returns:
The textContent with whitespace collapsed, as required.
- Type
 - BaseString
 
 - 
    
getValue()
 - 
    
    
Get the value of our element.
Returns:
A string-like object which contains the escaped value from the element.
- Type
 - BaseString
 
 - 
    
isInDOM()
 - 
    
    
Returns true if the element is still connected to the DOM.
Returns:
True, if the element is still connected to the DOM.
- Type
 - boolean
 
 - 
    
isSameElement(otherElement)
 - 
    
    
Returns true if the otherElement object passed is referring to the same DOM element as ours inside the user's page.
Parameters:
Name Type Description otherElementElement  - 
    
mouseDown( [options])
 - 
    
    
Perform a mouse down action, onto the centre of the element.
Parameters:
Name Type Argument Description optionsObject <optional> 
 - 
    
mouseUp( [options])
 - 
    
    
Perform a mouse up action, after moving to the centre of the element.
Parameters:
Name Type Argument Description optionsObject <optional> 
 - 
    
setValueByTyping(toValue [, options])
 - 
    
    
Set the value for a particular DOM element by typing into the element. This will clear the existing value in the element.
Parameters:
Name Type Argument Description toValuestring string to enter into the input.
optionsObject <optional>