Collection of cypress commands to test your WordPress page.
Inspired by the @wordpress/e2e-test-utils
package which does the same for Puppeteer.
-
Install npm dependency
# NPM npm install cypress-wp-test-utils --save-dev # Yarn yarn add cypress-wp-test-utils --dev
-
Register commands in your cypress project (normally in
cypress/support/index.js
)import { registerWpTestCommands } from 'cypress-wp-test-utils'; registerWpTestCommands();
-
Use the commands in your tests!
There are several options to configure the commands. These options can be defined in your cypress configuration file (normally cypress.json
):
{
"env": {
"cypress-wp-test-utils": {
"username": "myuser",
"password": "supersecurepassword"
}
}
}
username
(string
): Username which is used as the default username in theloginUser
command. (Default:admin
)password
(string
): Password which is used as the default password in theloginUser
command. (Default:password
)
Activates a plugin.
slug
(string
) Slug of the plugin which should be activated.
Clicks a button in the toolbar of the currently selected block.
label
(string
) The text string of the button label.index?
(number
) The index of the button if multiple buttons were found with the given label. (Default:0
)
Clicks a button.
label
(string
) The text string of the button label.buttonLabelType?
('ariaLabel' | 'content'
) The type of button label: 'ariaLabel' or 'content'. (Default:'content'
)
Clicks an element by a given text.
elementType
(string
) The element type (eg.'button'
,'input'
).text
(string
) The text of the element.strict?
(boolean
) If set to true the given text has to match exactly the text of the element (Default:false
).index?
(number
) The index of the element if multiple elements were found with the given label. (Default:0
)
Closes the welcome guide if displayed.
Creates new post.
object
(object
) Object to create new post, along with tips enabling option.object.postType?
(string
) Post type of the new post.object.title?
(string
) Title of the new post.object.content?
(string
) Content of the new post.object.excerpt?
(string
) Excerpt of the new post.object.showWelcomeGuide?
(boolean
) Whether to show the welcome guide or not. (Default:false
)
Deactivates a plugin.
slug
(string
) Slug of the plugin which should be deactivated.
Ensures that the sidebar is opened (opens sidebar if closed).
Gets a checkbox element by label.
label
(string
) Label of checkbox element.
Returns current post content as object. see: https://developer.wordpress.org/block-editor/reference-guides/data/data-core-editor/#getCurrentPost
Returns the content of the post being edited as string.
Gets a input element by label.
label
(string
) Label of input element.
Gets a select element by label.
label
(string
) Label of select element.
Gets a TextControl element by label.
label
(string
) Label of TextControl element.
Gets a toggle element by label.
label
(string
) Label of toggle element.
Opens the inserter, searches for the given term, then selects the first result that appears.
searchTerm
(string
) The text to search the inserter for.blockLabel?
(string
) The label of the block to insert.
Performs a login with specified username and password.
username?
(string
) Username which should be used. (Default:admin
)password?
(string
) Password which should be used. (Default:password
)
Opens the global block inserter.
Opens sidebar panel with given title (if closed).
title
(string
) Title of sidebar panel to open.
Search for block in the global inserter.
searchTerm
(string
) The text to search the inserter for.
Selects a block by its name.
name
(string
) Name of the block to select. (eg.'core/paragraph'
)index?
(number
) The index of the block if multiple blocks were found with the given name.
Checks if a certain option in a select box is available.
selectLabel
(string
) Label of the select box.optionValue
(string
) The value of the option to search for.
Sets the editor content.
content
(string
) New editor content.
Sets the value of a TextControl element by its label.
label
(string
) Label of the TextControl element.value
(string
) Value to set.
Checks if a toolbar option is active.
toolbarLabel
(string
) Label of the toolbar option to check.buttonText
(string
) Text of the button to check if it's active.toolbarIndex?
(number
) The index of the toolbar option if multiple options were found with the given label.buttonIndex?
(number
) The index of the button if multiple buttons were found with the given text.
Visits a given admin page.
adminPath
(string
) Path to admin page.query?
(string
) Query string which should be added to URL.
- WordPress 5.7, 5.8, 5.9, 6.0, 6.1
- Node >=12
- Cypress >=5.x
Right now those commands are only compatible with WordPress 5.7, 5.8, 5.9, 6.0 and 6.1. The goal is to support further WordPress versions but since there are always a lot of changes in the editor between WordPress versions it's difficult to support all of them.
This package uses and includes the @cypress/xpath command. If you're using it as well in your tests you don't need to include it again.