Rayrun
← Back to Discord Forum

Merging test cases

dragonshadow2019posted in #help-playwright
Open in Discord
dragonshadow2019
dragonshadow2019

Hi,

I have an e-commerce website with a separated functional tests. For example 5 tests run for testing the login functionality. But I want to use 1 test from this group to add an individual e2e regression test. So I would like to use one test which part of the login tests in the separated e2e test.

For example: I have one test for success login in the login tests file. But I would like to use the same test in the e2e test login step.

Like a Lego pice 🙂

Is there any option for this scenario?

Thanks for the help

This thread is trying to answer question "Is there any option to reuse a test case from a group of functional tests in a separate end-to-end regression test?"

6 replies

You should be able to do this using tags. If you're using them already it's easier, you'd just add an @e2e tag for e.g & grep it when wanting to run the tests.. If you're not using them it shouldn't be a problem to start just off the top of my head.. Here's the detail: https://playwright.dev/docs/test-annotations

Another option if you are averse to tags could be that you could make a function that returns the test case, and then use that function to "generate" the test case in the "actual" test file and as a part of the e2e test file. Something like this in JavaScript:

// testGenerator.js
const { test } = require('@playwright/test');

const testReturningFunction = (testName) => {
  return test(testName, async ({page}) => {
    // test steps
  });
};
module.exports = { testReturningFunction };


// normalTestCase.js
const { testReturningFunction } = require('./testGenerator.js'); 

testReturningFunction('regular test');


// e2eTest.js
const { testReturningFunction } = require('./testGenerator.js'); 

test.describe('or maybe serial mode', () => {
  testReturningFunction('one step in e2e test');
  // other steps
}
dragonshadow2019
dragonshadow2019

Thanks for the answers. What is your opinion about reusable function? For example i have a successful login function test in a separate file, and I export this function to the e2e test and the login functional test too. Other login related tests are added to the login functional tests file like invalid credentials test.

For login specifically, a fixture or a page object model may make more sense. The tradeoff for reusing functions etc is of course maintainability: if you need to change the reused thing and the change doesn't apply to some of your use cases, that will take some refactoring. I do have helper functions in a folder called "helper-functions" for functions I need every now and then, e.g. generating random strings and dates. I also use POM.

Well, now that I think about it, limiting POM to just login is probably a bit silly: it's more of an "investment" than that.

dragonshadow2019
dragonshadow2019

Thanks for the answer 🙂

Answer

Sure, you can run a single test from a group of functional tests in an e2e regression test with Playwright Test. You can do this by using test annotations and tags.

Grouping Tests

You can group tests together with test.describe. This function gives your tests a logical name or scope. Inside the test.describe block, you can define multiple tests using the test function. Each test can have its own unique functionality.

test.describe('Login Tests', () => {
  test('Login with valid credentials', async ({ page }) => {
    // Test code here
  });
  test('Login with invalid credentials', async ({ page }) => {
    // Test code here
  });
});

Tagging Tests

You can tag your tests and selectively run them based on these tags. For example, you can tag a test as "@fast" or "@slow".

test('@fast Login with valid credentials', async ({ page }) => {
  // Test code here
});

To run only tests with a certain tag, use the --grep command line flag followed by the desired tag value.

npx playwright test --grep @fast

This will execute only those tests that are tagged as "@fast".

Skipping Tests

If you want to skip tests with a certain tag, you can use the --grep-invert command line flag followed by the desired tag value.

npx playwright test --grep-invert @slow

This will skip all tests that are tagged as "@slow".

Running Tests with Multiple Tags

If you want to run tests containing either of multiple tags, you can use regex lookaheads with --grep.

npx playwright test --grep "@fast|@slow"

This will execute all tests that are tagged either as "@fast" or "@slow".

If you want to run only those tests that contain both tags, again using regex lookaheads with --grep.

npx playwright test --grep "(?=.*@fast)(?=.*@slow)"

This will execute only those tests that are tagged as both "@fast" and "@slow".

By using these test annotations and tags, you can selectively include or exclude specific tests from a group of functional tests in your e2e regression test. This provides flexibility in running targeted tests based on their characteristics or requirements.

For more information on organizing your Playwright tests using tags, check out this blog post.

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.