IntelliJ IDEA 2024.3 Help

Playwright

Playwright is an open-source test automation framework. It provides a high-level API for automating web browsers such as Chrome, Firefox, and Safari.

Create a new Playwright project

  1. In the main menu, go to File | New | Project.

    Alternatively, if you're on the Welcome screen, click New Project.

  2. From the list on the left, select Playwright.

    Creating a new Playwright project
  3. Name the new project and change its location if necessary.

  4. Specify the Node interpreter.

  5. Specify the command to install Playwright.

  6. Click Create.

  1. In the main menu, go to File | New | Project.

    Alternatively, if you're on the Welcome screen, click New Project.

  2. From the list on the left, select Playwright for JVM.

    Creating a new Playwright for JVM project
  3. Name the new project and change its location if necessary.

  4. (Optional) Enable the Create Git repository option to place the new project under version control. If you do not want to do it now, you can do it later at any time.

  5. Select the Language that you want to use.

  6. Select the build system that you want to use in your project: Maven or Gradle.

  7. Select the test framework that you want to use for test cases management: JUnit or TestNG.

  8. (Optional) Modify the Group and Artifact parameters.

  9. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

  10. (Optional) Enable the Add sample code parameter.

  11. Click Next.

  12. Click Create.

  1. In the main menu, go to File | New | Project.

    Alternatively, if you're on the Welcome screen, click New Project.

  2. From the list on the left, select Playwright for Python.

    Creating a new Playwright for Python project
  3. Name the new project and change its location if necessary.

  4. (Optional) Enable the Create Git repository option to place the new project under version control. If you do not want to do it now, you can do it later at any time.

  5. Select whether you want to create a new or use an existing Environment and configure the corresponding settings.

    Configure a new Virtualenv environment
    • Select the base interpreter from the list, or click Choose the base interpreter and find the Python executable in your file system.

    • Specify the location of the new virtual environment in the Location field, or click Virtual environment location and browse for the location in your file system. The directory for the new virtual environment should be empty.

    • Select the Inherit global site-packages checkbox if you want all packages installed in the global Python on your machine to be added to the virtual environment you're going to create. This checkbox corresponds to the --system-site-packages option of the virtualenv tool.

    • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

    For more information, refer to the PyCharm documentation.

    Configure a new Conda environment
    • Specify the location of the new conda environment in the Location field, or click Conda environment location and browse for the location in your file system. The directory for the new conda environment should be empty.

    • Select the Python version from the list.

    • Normally, IntelliJ IDEA will detect conda installation.

      Otherwise, specify the location of the conda executable, or click Conda executable location to browse for it.

    • Select the Make available to all projects checkbox if you want to reuse this environment when creating Python interpreters in IntelliJ IDEA.

    For more information, refer to the PyCharm documentation.

    Configure a new Pipenv environment
    • Choose the base interpreter from the list, or click Choose the base interpreter and find the Python executable in your file system.

    • If you have added the base binary directory to your PATH environmental variable, you don't need to set any additional options: the path to the pipenv executable will be autodetected.

      If the pipenv executable is not found, follow the pipenv installation procedure to discover the executable path, and then specify it in the dialog.

    For more information, refer to the PyCharm documentation.

    Configure a new Poetry environment
    • Select the base interpreter from the list or click Choose the base interpreter and find the Python executable in your file system.

    • If IntelliJ IDEA doesn't detect the poetry executable, specify the following path in the dialog, replacing jetbrains with your username:

      /Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetry
      C:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe
      /home/jetbrains/.local/bin/poetry

    For more information, refer to the PyCharm documentation.

    Configure an existing environment
    • Select the interpreter from the list. If there are no available interpreters, click Add Interpreter and configure one according to your needs.

  6. Click Create.

A new project is created according to the options that you have selected.

Initialize the Playwright project

Once the project is created, you need to initialize it. During this procedure, you will select the desired language, install Playwright browsers, and specify other settings.

Project initialization

To start the project initialization:

  1. In the Run tool window Alt+4, press the up/down arrow buttons on your keyboard to select the language that you want to use in the project.

  2. Type the name of the folder where you want to put your end-to-end tests.

  3. Specify if you want to add a GitHub Actions workflow.

  4. Specify if you want to install Playwright browsers.

When you specify all the parameters, the initialization starts. The system will notify you once the process is completed:

Project successfully initialized

Role-based locators support

In Playwright, you can locate an element by its role. This helps to pinpoint a specific element on the page, be it a button, checkbox, heading, link, or other elements.

IntelliJ IDEA can generate such locators and add them to your code, so you can use them in your tests.

Role-based locators

Add elements to code

  1. Open the file where you want to add an element.

  2. Click the Web Inspector button on the right-hand sidebar to open the Web Inspector toolbar window.

  3. Specify the URL of the page in the address bar.

    Address bar controls
  4. Click The Select Element in Page button and select the element that you want to add in the Web Inspector.

    Select element in page
  5. Once the element is selected, click Add Element to Code to add the element to the code.

    The Add Element to Code button

    To add a specific type of the selector (ID, Name, Tag with classes, and so on), click Add Element to Code By and select the required option.

    Add elements to code by

    If you want to switch specifically between CSS and XPath locators, click and select the required locator type.

    Select locator type

As a result, a piece of code is generated and added to the code editor.

Run tests

  • To run your tests, click the the Run button gutter icon next to the test class or test method, then select the Run option from the list.

    Run tests

    Alternatively, place the caret at the test class to run all tests in that class, or at the test method, and press Ctrl+Shift+F10.

You can run tests in a more customizable way using the run/debug configurations. For more information, refer to Run tests.

Run tests in headed mode

Running tests in headed mode allows you to get a visual representation of how Playwright interacts with the website.

To run tests in headed mode:

  1. Click the current configuration in the run/debug configuration switcher, then click and select the Edit option.

    Editing Run/Debug configurations
  2. In the Playwright options field, specify the --headed command:

    Configuring run/debug configuration to run tests in headed mode
  3. Apply changes and run your tests.

  1. Open the file that contains the tests that you want to run in headed mode.

  2. Add the following option to your code:

    launch(new BrowserType.LaunchOptions().setHeadless(false))
    Modifying code to run tests in headed mode
  3. Run your tests.

  1. Click the current configuration in the run/debug configuration switcher, then click and select the Edit option.

    Editing Run/Debug configurations
  2. In the Additional arguments field, specify the --headed command:

    Configuring run/debug configuration to run tests in headed mode
  3. Apply changes and run your tests.

As a result, a browser is opened to reproduce the steps from the tests.

Review test results

When the tests finish running, the results are displayed on the Test Runner tab of the Run tool window. On this tab, you can rerun tests, export and import test results, see how much time it took to run each test, and perform other actions.

Test results shown on the Test Runner tab of the Run tool window

For more information, refer to Explore test results.

Debug tests

In general, the debugging procedure includes the following steps:

  1. Set breakpoints for the lines of code where the execution must be suspended.

  2. Run your code in debug mode.

  3. Use the Debug tool window to analyze the code and locate potential issues.

  4. Fix the issues.

To reproduce these steps in the IDE, do the following:

  1. In the gutter, hover over the executable line of code where you want to suspend the execution and click .

    Set a breakpoint
  2. Click in the gutter and select Debug. This starts the debugger session and runs your code.

    Start the debug session
  3. Once the program hits the breakpoint, the execution becomes suspended, and you can review the results of the debugger session on the Debug tool window.

    Debug tool window overview

For more information on debugging, refer to Debug code.

Generate Playwright report

Playwright reporters provide detailed information about failed tests, allowing you to perform a more thorough analysis of what happened during the test execution.

By default, an html report is generated, but you can switch to an alternative format using run/debug configurations. To do this:

  1. Click the current configuration in the run/debug configuration switcher, then click and select the Edit option.

    Editing Run/Debug configurations
  2. In the Playwright options field, specify the desired reporter. For example:

    --reporter=line
  3. Apply changes and run your tests.

As a result, a detailed report is generated.

Generated Playwright report

Modify a run/debug configuration

If you want to modify the startup properties for your tests, edit the run/debug configuration:

  1. Click the current configuration in the run/debug configuration switcher and select the Edit Configurations option.

    Select the run/debug configuration
  2. In the Run/Debug Configurations dialog, select the configuration that you want to edit.

  3. Configure the required startup properties.

    Configure startup properties
    Configure startup properties
    Configure startup properties
  4. Apply changes and click OK.

For more information on run/debug configurations, refer to Run/debug configurations.

Customize the locator template

If you want to customize how selected elements are added to your code, you can modify the template:

  1. Click the framework name on the Status bar. The UI Automation Framework menu opens.

    Customize template
  2. Select the Customize Template option.

  3. In the File and Code Templates dialog, select the required framework from the list.

    The File and Code Templates dialog
  4. Rewrite the code generation algorithms for the web element locator.

  5. Click OK to save changes.

As a result, the locators will be added to the code according to the updated code generation algorithms.

Last modified: 25 October 2024