thienphuong / playwright-with-typescript

0 stars 0 forks source link

Cấu trúc Test file với TypeScript #3

Open thienphuong opened 7 months ago

thienphuong commented 7 months ago
import { describe, it } from '@serenity-js/playwright-test'
import { Navigate, PageElements, By } from '@serenity-js/web'
import { Ensure, equals } from '@serenity-js/assertions'

describe('Todo List', () => {                                   // - feature or component name

    const displayedItems = () =>
        PageElements.located(By.css('.todo-list li'))
            .describedAs('displayed items')

    describe('when the user is', () => {                        // - one or more nested `describe` blocks
        describe('a guest', () => {                             //   to group scenarios
            describe('their list', () => {                      //   by context in which they apply

                it('is empty', async ({ actor }) => {           // - verify expected behaviour
                    await actor.attemptsTo(                     //   using a default `actor`
                        Navigate.to('https://todo-app.serenity-js.org/'),
                        Ensure.that(displayedItems().count(), equals(0))
                    )
                })
            })
        })
    })
})
thienphuong commented 7 months ago

Mỗi một describe blocks tương ứng như một class. Bố cục giống việc sử dụng Nested Test Classes của Junit5.

If we have setup or tear-down methods that repeat for some of the tests, but not for all of them, a @Nested test class can be very useful.

Furthermore, using nested classes for setting up groups of tests lead to more expressive test scenarios and a clear relationship between our tests.

public class TodoListTest {

    Actor actor =Actor.named("jacob") ;

    @Nested
    class WhenTheUserIs {

        @Nested
        class AGuest {
              @Nested
              class TheirList {
                    @Test
                    void IsEmpty() {
                        actor.attemptsTo(
                            Navigate.to('https://todo-app.serenity-js.org/'),
                            Ensure.that(displayedItems().count(), equals(0)))
                    }
              }
       }

    }

    @Nested
    class SecondNestedClass {
        @Test
        void test() {
            System.out.println("SecondNestedClass.test()");
        }
    }
}