Ky Functions

Ky functions that are available in all Ky projects, in addition to the built-in cypress functions

ky.assertDoesNotHaveClass()

Assert that an element does not have a specific className

ParameterOptionalDescription
opts No object containing all arguments
opts.item No the css selector for finding the element
opts.className No the className to check for

Returns: Cypress chainable containing the element that was checked

ky.assertHasClass()

Assert that an element has a specific className

ParameterOptionalDescription
opts No object containing all arguments
opts.item No the css selector for finding the element
opts.className No the className to check for

Returns: Cypress chainable containing the element that was checked

ky.assertNumElements()

Assert a certain number of elements

ParameterOptionalDescription
opts No object containing all arguments
opts.item No a CSS selector corresponding to the item
opts.num No the precise number of elements expected

ky.checkIfElementExists()

Check if an element exists on the page

ParameterOptionalDescription
selector No - The selector for the element to check

ky.extractDataFromClass()

Obtain data from a class attribute that starts with a specific prefix and return it

ParameterOptionalDescription
opts No the options object
opts.selector No the css selector for finding the element
opts.classPrefix No the prefix to look for in the class attribute

Returns: the extracted data

ky.extractDataFromClassByContents()

Obtain data from a class attribute that starts with a specific prefix and with specific contents, and return it

ParameterOptionalDescription
opts No the options object
opts.contents No the contents of the element to find
opts.selector No the css selector for finding the element
opts.classPrefix No the prefix to look for in the class attribute

Returns: the extracted data

ky.genTextOfLength()

Generate a string of specified length for Cypress tests

ParameterOptionalDescription
opts No options object
opts.length No the desired length of the string
opts.text No optional text to repeat to form the string; defaults to '123456789x' if not provided

Returns: a string of the specified length, if the length is not a multiple of the text length, it is truncated

ky.getAttribute()

Get the attribute of an element

ParameterOptionalDescription
opts No options object
opts.selector No the CSS selector of the element
opts.attribute No the attribute to retrieve

Returns: the attribute of the element

ky.getClassName()

Get the class attribute of an element

ParameterOptionalDescription
selector No the CSS selector of the element

Returns: the class of the element, if multiple classes exist, they are space-separated

ky.getCredentialValue()

Get a credential from GlobalCredentials

ParameterOptionalDescription
key No the key of the environment variable to retrieve

Returns: The value of the specified environment variable

ky.getCredentialValues()

Alias for cy.env for retrieving multiple environment variables at once Obtains multiple credential values from the Cypress env depending on the provided keys.

ParameterOptionalDescription
keys No the keys of the environment variables to retrieve

Returns: Object where keys are the provided keys and the values are

ky.getCurrentDateInfo()

Get the current day

Returns: the current month as a number (1-31)

ky.getId()

Get the id of an element

ParameterOptionalDescription
selector No the CSS selector of the element

Returns: the id of the element

ky.getJSON()

Obtain JSON data from a request and return it.

ParameterOptionalDescription
url No the URL to fetch the JSON data from

Returns: the JSON data

ky.getNumElements()

Get number of elements

ParameterOptionalDescription
selector No a CSS selector corresponding to the item to count

Returns: Cypress chainable containing the number of elements matching the selector

ky.getProfileValue()

Get a (non-credential) value from the current profile, GlobalValues, or GlobalResources

ParameterOptionalDescription
key No the key of the environment variable to retrieve

ky.getSpecialChars()

Generate all printable special characters for Cypress tests

Returns: a string containing all special characters

ky.getTitle()

Get the current title of the page

ky.handleHarvardKey()

Handle a HarvardKey login page for a user via UI (form filling with cy.origin(), not API calls)

ParameterOptionalDescription
name No the name of the user environment variable

Returns: Cypress chainable (void) - performs authentication flow, no return value

ky.launchAs()

Log into Canvas and launch an LTI app as a specific user from environment variables. The user should be defined as an environment variable with the following properties:

  • accessToken: Canvas access token for the user (required for production mode)
  • username: the HarvardKey username of the user
  • password: the HarvardKey password of the user
ParameterOptionalDescription
name No the name of the user environment variable
opts No object containing all arguments
opts.courseId Yes the Canvas ID of the course to launch from
opts.appName Yes the name of the app as it appears in the course's left-hand nav

Returns: Cypress chainable (void) - launches the LTI app, no return value

ky.listSelectLabels()

Get the labels of all the options within a select element

ParameterOptionalDescription
selector No the CSS selector of the select element

Returns: an array of strings representing the labels of the options

ky.listSelectValues()

Get the values of all the options within a select element

ParameterOptionalDescription
selector No the CSS selector of the select element

Returns: an array of strings representing the values of the options

ky.logIntoPorta()

Log into Porta via HarvardKey authentication using API requests (no UI interaction).

Flow:

  1. GET Cirrus Identity discovery page → extract HarvardKey IdP button href
  2. GET IdP login page → extract form action and hidden fields
  3. POST credentials to the login form
  4. POST SAML form to ACS
ParameterOptionalDescription
name No the name of the user environment variable

Returns: Cypress chainable (void) - performs authentication flow, no return value

ky.navigateToHref()

Navigate to href attribute of Harvard identity provider button

ParameterOptionalDescription
item No the CSS selector of interest

Returns: Cypress chainable containing the full URL string (hostname + href attribute value)

ky.padWithZeros()

Pads a number with leading zeros to a user-defined length.

ParameterOptionalDescription
opts No options object
opts.length No the desired total length of the output string, must be larger than the length of the number
opts.number No the number to be padded with leading zeros

Returns: padded string with leading zeros

ky.runScript()

Run a script on the page

ParameterOptionalDescription
script No the script to run

Returns: Cypress chainable containing the return value from executing the script

ky.tap()

Click on an element.

ParameterOptionalDescription
item No the CSS selector of interest

Returns: Cypress chainable containing the clicked element

ky.tapInIFrame()

Given an iframe, begins controlling it.

ParameterOptionalDescription
iframeSelector No - The selector for the iframe to control
selectorToTap No - The selector for the element inside the iframe to tap

ky.typeInto()

Type text into an element. This function first removes the previous text in the element

ParameterOptionalDescription
opts No object containing all arguments
opts.item No the CSS selector of interest
opts.text No the text to type
opts.pressEnter Yes if true, after typing into the text field, simulate pressing enter
opts.append Yes if true, append the text to the end of the existing text in the element instead of replacing it

Returns: Cypress chainable containing the input element that text was typed into

ky.uniquify()

Makes a string unique by appending a timestamp and a counter.

ParameterOptionalDescription
text No the original string

Returns: the uniquified string

ky.visitCanvasEndpoint()

Send a request to a Canvas API endpoint.

ParameterOptionalDescription
opts No options object
opts.method No the HTTP method (e.g., 'GET', 'POST', 'DELETE')
opts.path No the API endpoint path excluding 'https://canvas.harvard.edu', (e.g., '/api/v1/courses/123')
opts.accessToken No the access token for authentication
opts.params No the request params (if any)

Returns: Cypress chainable containing the Canvas API response body

ky.waitForAtLeastOneElementPresent()

Wait for any element in a list to be present (check every tenth of a second for status)

ParameterOptionalDescription
opts No object containing all arguments
opts.items No list of CSS selectors of interest
opts.timeoutSec Yes the number of seconds to wait before

Returns: the selector of the item that was found first, ties

ky.waitForElementVisible()

Wait for an element to be visible

ParameterOptionalDescription
item No the CSS selector of interest
timeoutSec Yes =10] the number of seconds to wait before timing out

Returns: Cypress chainable containing the visible element