thienphuong / playwright-with-typescript

0 stars 0 forks source link

Sự tương đồng trong cú pháp giữa Java và TypeScript #2

Open thienphuong opened 7 months ago

thienphuong commented 7 months ago

1/ Import a package, class or interface

TypeScript

import { Clear, Click, DoubleClick, Enter, Hover, Key, PageElement, Press } from '@serenity-js/web';

Java

import net.serenitybdd.screenplay.Task;
import net.serenitybdd.screenplay.actions.*;
thienphuong commented 7 months ago
import { test, expect, type Page } from '@playwright/test'; // pure playwright
--> import { describe, it } from '@serenity-js/playwright-test' // playwright is wrapper by serenityJS

// Junit5: @BeforeEach
test.beforeEach(async ({ page }) => {
  await page.goto('https://demo.playwright.dev/todomvc');
});

const TODO_ITEMS = [
  'buy some cheese',
  'feed the cat',
  'book a doctors appointment'
];

.....
// One or more nested: 
test.describe('Mark all as completed', () => {

  test.beforeEach(async ({ page }) => {
    await createDefaultTodos(page);
    await checkNumberOfTodosInLocalStorage(page, 3);
  });

  test.afterEach(async ({ page }) => {
    await checkNumberOfTodosInLocalStorage(page, 3);
  });

  // @Test  - serenityJS syntax
  it('ishould allow me to mark all items as completed', async ({ actor }) => {
                await actor.attemptsTo(
                    Send.a(GetRequest.to('/todos/1')),
                    Ensure.that(LastResponse.status(), equals(200)),
                    Ensure.that(LastResponse.body<TodoItem>().id, equals(1)),
                );
    });

  // @Test: playwright syntax
  test('should allow me to mark all items as completed', async ({ page }) => { 
    // Complete all todos.
    await page.getByLabel('Mark all as complete').check();

    // Ensure all todos have 'completed' class.
    await expect(page.getByTestId('todo-item')).toHaveClass(['completed', 'completed', 'completed']);
    await checkNumberOfCompletedTodosInLocalStorage(page, 3);
  });
});