← Back to Discord Forum

Can I use static locators for (SPA) elements that are always there, e.g. navigation bar?

I'm testing a Single Page Application (SPA) which has some components that are always present, such as the navigation bar. I'm also using the Page Object Model (POM), and I'm considering marking some of the ever-present locators as static. In TypeScript, it looks like this:

import { Page, Locator } from '@playwright/test';

class SinglePageApplication {
  private page: Page;
  private static navigationBar: Locator;

1. Are there any problems doing this? 
2. Will it affect parallelization of my tests? 
3. If I reuse Page context between tests, does the answer to 1 and 2 change?

This thread is trying to answer question "Can I use static locators for Single Page Application (SPA) elements that are always there, such as the navigation bar, and will it cause any problems or affect parallelization of my tests?"

1 reply

Locator just holds the instruction to how find the element, not the element itself; on each action/assertion Playwright will look for that element so technically I think there's no difference if locator variable is static or not


Sure, you can use static locators for elements that are always present in a Single Page Application (SPA), like the navigation bar. These locators are efficient and reliable as they target specific elements based on their constant attributes or properties.

Parallelization, or running multiple tests simultaneously, and reusing the page context between tests, which helps maintain state and avoid unnecessary setup steps, won't be significantly impacted by using static locators.

Static locators provide a consistent way of locating elements, which is beneficial for both parallelization and reusability of page contexts. This is because these elements have stable attributes or properties, allowing multiple tests to interact with them without conflicts.

However, be aware that dynamic content within your SPA could affect the reliability of static locators. If parts of your navigation bar change dynamically based on user interactions or other factors, you may need to adjust your locator strategy.

In short, using static locators for consistently present elements like the navigation bar in an SPA is a good practice, provided those elements have stable attributes or properties. It won't negatively impact parallelization of tests or reusing the page context between tests.

Related Discord Threads

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.