Rayrun
← Back to Discord Forum

cypress.run equivalent in playwright

Hi all, we are trying to migrate our tests from Cypress to playwright . We have an API exposed by cypress run which we can use to programmatically run tests . Do we have something similar in playwright ?

This thread is trying to answer question "Is there an equivalent of the Cypress `run` API in Playwright for programmatically running tests and injecting environment variables?"

6 replies

To be more clear we want to inject environment variables into our tests when it is running on pipeline . We are using bitbucket as our pipeline . For few environment variables we are using the bitbuckets repository variables but for few we have some constants defined in code . These constants are different for different tests . In cypress we do something like this

cypress.run({ config, env: ( we are injecting variables here)}

Searching the documentation for .env should give you the required info I think. https://playwright.dev/docs/intro

So the only way to inject environment variables is to use them along with npx playwright ? We are not exposing any api from the playwright ?

vertigo448
vertigo448

there is no 1:1 equivalent of cypress.run in Playwright @shyam2794 handling env variables in .env file is a way to go or any different similar stuff like config etc. overall seams like a better solution than maintaining .run (just my opinion)

I had to convert how we implemented override configuration from Robot Framework, I am using https://www.npmjs.com/package/dotenv to build my .env files and I call a function in the playwright.config file that reads in an override file as well as the original .env file. It also validates that it has an entry for all environment files as safeguard from running the suite without a environment variable set. In GH actions, you just need to setup the .env file since it is holding secrets and should not be persisted, assuming BB has a similar approach. If you are interested in more details on this solution, let me know and I can share a sanitized version.

Answer

Sure, you can run tests and inject environment variables in Playwright. Use the npx playwright test command to run tests. To inject environment variables, set them in the command line like this: USERNAME=me PASSWORD=secret npx playwright test.

You can also use a configuration file, playwright.config.ts, to define your base URL and other configurations using environment variables. Here's an example:

import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
  },
});

Run your tests with different values for the STAGING environment variable to switch between environments.

If you prefer, use .env files to manage environment variables. Create a .env file with key-value pairs:

STAGING=0
USERNAME=me
PASSWORD=secret

Then, in your playwright.config.ts, read these values using a package like dotenv:

import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

dotenv.config();
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });

export default defineConfig({
  use: {
    baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
  },
});

This way, you can edit the .env file to set any variables you'd like without modifying your test code. Playwright provides multiple ways to run tests and inject environment variables. You can customize your test runs based on different environments or configurations.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.