What's new in Playwright 1.33 and 1.34
Tags: playwright, e2e, testing, javascript
Published: 2023-06-06 18:43:36



Hello everyone!

Today, we have a special edition for you as we delve into the exciting updates we've recently rolled out in our releases 1.33 and 1.34. Our dynamic duo of Debbie and Lusha sat down to discuss these in detail. So, without further ado, let's dive right in.

## UI Mode Update

The [UI mode](http://playwright.dev/docs/test-ui-mode) has been the favorite of many and it just got better! In its latest incarnation, the UI mode now includes features such as visualizing the hooks that run before and after a test, as well as fixtures. Moreover, the update also encompasses custom navigation steps within the UI mode.

```js
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }) => {
  await test.step('navigate', async () => {
    await page.goto('https://playwright.dev/');
  });
});

test('has title', async ({ page }) => {
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  // ...
});
```

![ui mode with steps and hooks](https://github.com/microsoft/playwright/assets/13063165/62a6ce81-9ba4-4071-bdac-60db3f222bcc)

Not only does this latest update allow for the smoother running of tests, but it also allows for better visual regression testing. For instance, if we do a screenshot test and then someone goes into the code and removes a button, you'd see a failure. This update includes an "Attachments" tab that allows you to explore and compare the screenshots complete with a slider to overlay and better see the differences.

```js
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }) => {
  await test.step('navigate', async () => {
    await page.goto('https://playwright.dev/');
  });
});

test('should take screenshot', async ({ page }) => {
  // programmatically remove the button for this example
  await page.getByText('Get Started').evaluate((el) => el.remove());

  await expect(page).toHaveScreenshot();
});
```

![ui mode with attachments](https://github.com/microsoft/playwright/assets/13063165/a6f8e46a-688a-41e4-be35-5c662aad264d)

And here's the cherry on top: all these UI mode features are also coming to the trace viewer. So, when tests fail on CI, you will still get the same experience.

## Project Teardown

Before we dive into project teardown, it's important to discuss setup and [dependencies](http://playwright.dev/docs/test-projects#dependencies). Setting up dependencies between different projects is  a useful feature when data is shared between them, for example setting up a database.

Previously, tearing down a database after testing was a difficult task. However, the latest update introduces a "teardown" feature, which allows the smooth running of setup, dependencies, and finally, the cleanup process.

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

export default defineConfig({
  projects: [
    {
      name: 'setup db',
      testMatch: /global.setup\.ts/,
      teardown: 'cleanup db',
    },
    {
      name: 'cleanup db',
      testMatch: /global.teardown\.ts/,
    },
    {
      name: 'chromium',
      use: devices['Desktop Chrome'],
      dependencies: ['setup'],
    },
    {
      name: 'webkit',
      use: devices['Desktop Safari'],
      dependencies: ['setup'],
    },
  ],
});
```

![global setup and teardown](https://github.com/microsoft/playwright/assets/13063165/dfcf10a9-f601-4d0c-bd8d-9490e6efbf7a)


## New Locators API

With the new [Locators](http://playwright.dev/docs/next/locators) API, you can select elements with more precision. The `locator.and` feature allows you to combine different selectors to pinpoint a precise element. 

![locator and](https://github.com/microsoft/playwright/assets/13063165/d026207d-292d-4faf-ae91-edae099ad4c4)

```js
await page.getByRole('button', { name: 'Sign Up' })
          .and(page.getByTitle('Sign Up Today'))
          .click();
```

The `locator.or` API is used when you want to wait for either of two elements to appear. 

![locator or](https://github.com/microsoft/playwright/assets/13063165/e039525b-c814-4a37-b5e7-34e6ed1de68d)


```js
const newEmail = page.getByRole('button', { name: 'New email' });
const dialog = page.getByText('Confirm security settings');
await expect(newEmail.or(dialog)).toBeVisible();
```

Lastly, the `hasNot` and `hasNotText` API is great for negation, allowing you to locate elements that don't have certain properties.

![locator has not and has not text](https://github.com/microsoft/playwright/assets/13063165/c5987b28-e0da-4439-9800-6c7c1b9d9621)

```js
await page.locator('tr')
          .filter({ hasNotText: 'helicopter' })
          .filter({ hasNot: page.getByRole('button') })
          .screenshot();
```

## Miscellaneous Features

Alongside the major updates, there are some nifty miscellaneous additions as well. These include:

- [Expect Configure](http://playwright.dev/docs/test-assertions#expectconfigure): This new method allows you to create new instances of "expect" such as soft assertions and slow expects, which enable you to control the behavior of your tests in a more granular way.

  ```js
  const softAssert = expect.configure({ soft: true });
  const slowExpect = expect.configure({ timeout: 10_000 });
  ```

- [Web Server Configurations](http://playwright.dev/docs/test-webserver#configuring-a-web-server): The web server now allows you to configure stderr and stdout, helping you to manage your local Dev server more efficiently.
  
  ```js
  // playwright.config.ts
  import { defineConfig } from '@playwright/test';

  export default defineConfig({
    // Run your local dev server before starting the tests
    webServer: {
      command: 'npm run start',
      url: 'http://127.0.0.1:3000',
      reuseExistingServer: !process.env.CI,
      stdout: 'pipe', // Can be ‘ignore’ (default) or ‘pipe’
      stderr: 'pipe', // Can be ‘ignore’ or ‘pipe’ (default)
    },
  });
 ```
- [New Web Assertion](http://playwright.dev/docs/test-assertions#list-of-assertions): A new web assertion `toBeAttached` has been added, which checks whether an element is present in the DOM, even if it is hidden.
  
```js
  await expect(page.getByRole('button', { name: 'Sign Up' })).toBeAttached();
```
- Custom Reporters' API Method: There's a new async method, 'reporter on exit', which allows you to run postponed jobs.
  
```js
  await reporter.onExit();
```

- New Events on Browser Context: Console and dialog events now bubble up from all the pages to the browser context.

  Console:

```js
  browserContext.on('console', message => console.log(message));
  await page.evaluate(() => console.log('hello context!'));
```
  Dialog:

```js
  context.on('dialog', dialog => {
    dialog.accept();
  });
```

All in all, these latest updates offer a multitude of improvements that make the user experience more intuitive and efficient. We are excited for you to try out these new features and we eagerly look forward to your feedback. As always, keep an eye out for our future releases!

And, don't forget to give us a star, subscribe to our Twitter, YouTube, and Discord and give us a [star on GitHub](https://github.com/microsoft/playwright). Write us your comments - we read all of them.

Till the next update, happy testing!

- Read the [docs](https://playwright.dev/docs/)
- Watch the video [here](https://www.youtube.com/watch?v=8Z3QX6Z3Z5o)
- Join our community on [Discord](https://aka.ms/playwright-discord)