In mabl, you can train tests to work with the cookies that customize your users' web experience. This article describes the different ways you can work with cookies and cookie attributes in the mabl Trainer:
Setting cookies
With "set cookie" steps, you can quickly update the state of your web app. Use cases include:
- Dismissing content banners
- Controlling which app version or feature to test. See automating across A/B variants for more details.
- Restoring a login session to subsequent tests. See sharing variables between tests for a complete workflow.
To set a cookie, take the following steps in the Trainer:
- Add a "set cookie" step: + (Add step) > Cookies > Set cookie.
- Fill out the name and value. If you want to set the name or value to an existing variable's value, use mabl variable syntax:
{{@variable_name}}. - Optional: to set additional attributes, toggle on Set advanced options and customize as needed.
- Click OK to save the cookie.
Most web apps only check for cookies when the page loads. You may need to record a page refresh after adding a "set cookie" step to ensure the cookie has been applied to the session.
Setting cookies from API responses
If your web app's underlying API includes endpoints that return a "Set-Cookie" header, you can use API steps to set cookies. For example, if your API supports it, you can use an API step to apply auth cookies to a browser session or quickly set up a specific browser state.
Learn more in the article on API steps in UI tests.
Capturing cookie values
In addition to setting cookie values, you can extract existing cookie values and share them for validation elsewhere. For example, you could capture an auth cookie value and pass it on to other tests using a shared variables workflow.
To grab a cookie value and use it elsewhere, take the following steps in the Trainer:
- Create a new variable: {x} (Variables) > Create a variable.
- Set variable using "Cookie value".
- Select the cookie by name and use its value for the variable.
- Give the variable a name.
- Click OK to save.
HTTP Only cookies
The list of selectable cookies includes HTTP Only cookies. Commonly used for authentication, HTTP Only cookies are not accessible via JavaScript snippets, and they can only be accessed in the variable creation menu.
Verifying cookies
To confirm expected cookie values are present, add a cookie assertion:
- Add a new assertion: ✔︎ (Assert) > Cookie
- Select a cookie from the dropdown menu to view its attribute. The Trainer automatically populates this dropdown with a list of all cookies that are associated with the current page.
- Click OK.
- Configure the assertion details, including type, property, and value to compare against.
- Click OK to save your assertion step.
Removing cookies
If you want to reset the browser state, you can clear all cookies in the Trainer: + (Add step) > Cookies > Clear all page cookies. Note that clearing cookies may sign the user out of your application.
In some cases, you may want to remove a specific cookie. For example, you may want to remove an auth cookie to test a logged-out scenario. Take the following steps to remove a specific cookie in the Trainer:
- + (Add step) > Cookies > Remove cookie.
- Enter the name of the cookie you want to remove.
- Click OK.