3.2 Set Cookie
You can insert previously stored cookies into the browser session being tested to simulate an authenticated or preconfigured state.
Syntax
setCookies("<domain_name>", "<variable_name>");
Parameter | Description |
<domain_name> | The domain for which the cookies should be applied. |
<variable_name> | The variable holding cookie objects.
Possible values:
- Cookie object fetched using the get cookies task
- Cookie value stored locally in a QEngine variable
 Note:
1. In order to save a cookie manually, it must be in a key-value format. If you use the get cookie task, QEngine returns the full cookie object. You'll need to manually extract and paste only the required key-value pairs to reuse the cookie in your test script. 2. Cookies are passed as key=value pairs; some web URLs expect multiple cookies to be sent in a single string. To handle this, you can store multiple cookies in one variable by joining the key-value pairs with semicolons (;).
<variable_name> =<key1=value1>;<key2=value2>;<key3=value3>
For example,
cookieVar=token=abc123; sessionid=xyz789 |
3.3 Example
To optimize your automated testing workflow, especially for scenarios that require a logged-in user, you can leverage session cookie management using get and set cookies.
After successfully logging in to the application, use the get cookies task to save the authentication cookies. This enables reuse across multiple test cases.
- getcookies("authCookies");
Before executing a test case that requires an authenticated session, inject the stored cookies using the set cookies task.
- setCookies("zylker.com", "authCookies");
4. Use cases
Use case 1: Reusing authentication across test cases using cookies
In this use case, we demonstrate a test plan used to automate an end-to-end scenario in a mining management platform. Each step in the user flow is represented by its own test case, covering signing in, reusing authentication via cookies, and performing operations such as adding mines.
Test Case 1: Signing in and capturing the authentication cookie
The first test case covers the sign-in process. Upon successful login, the session cookie is extracted using the get cookie task. This cookie holds the authenticated state, which will be reused in subsequent test cases.
Test Case 2 (Function): Parsing cookies via the login function The cookie_login() function applies the stored cookie fetched from test case 1, using the set cookie task. This approach programmatically restores the authentication session in a reusable manner.
This function can be reused across any test case throughout the test plan in order to maintain a logged-in state without repeating the login flow.
Test Case 3: Performing authenticated operations
The login function in test case 2 restores the session, allowing this test case to proceed with authenticated operations such as adding a mine to the platform.
Example 2: Using pre-saved cookies for flexible test execution
In this use case, we demonstrate how to create a flexible test case that logs in using a pre-saved cookie stored in a variable without needing to interact with the login form. QEngine provides built-in functions, such as IS_PREVIEW_RUN, that allow your script to adapt automatically based on the type of execution. This ensures the test behaves appropriately, whether it's running as a live preview or as part of a full test plan.
In the following script, if the test is running in live preview, it uses the pre-saved cookie variable ($cookie_value) for authentication. If the test is running as part of a test plan, it retrieves the session cookie from the browser actions created by previous test cases, saves it into a variable ("login_cookie"), and applies it using the set cookie task.
- Test cases
- Variables
6. What's next
Next step
You can now start using get and set cookie tasks within your test scripts to manage session states and simulate user behavior.