Mục tiêu bài viết

Tự động test chức năng login default trong Laravel 5 sử dụng Behat-Laravel-Extension

Tham khảo link github : https://github.com/laracasts/Behat-Laravel-Extension

Mục tiêu của bài viết là quen với cách viết sử dụng behat, thực hành trực tiếp vào chức năng login trong Laravel.

Giới thiệu về Behat-Laravel-Extension

1. Cài đặt

composer require behat/behat behat/mink behat/mink-extension laracasts/behat-laravel-extension --dev

Tạo file behat.yml và config

default:
    extensions:
        Laracasts\Behat:
            # env_path: .env.behat
        Behat\MinkExtension:
            default_session: laravel
            laravel: ~

Tạo thư mục feature để thực hiện viết scenario test.

vendor/bin/behat --init 

Sau khi chạy xong lệnh trên sẽ tự động tạo một thư mục có tên feature.
Chúng ta sẽ tạo file auth.feature trong đây để phục vụ cho ví dụ ở dưới đây.

2. Nội dung thực hiện test

Trong Laravel 5 hoạt động xác thực được cung cấp default. Chúng ta sẽ thêm các hành vi thực hiện test hoạt động xác thực khi login như dưới đây.

NOTE : Mình có thay đổi một chút về design cũng như nội dung error, sẽ viết cụ thể ở nội dung file auth.feature

  1. Trường hợp chuyển về trang login.
  2. Trường hợp không nhập email sẽ hiển thị thông báo bạn cần nhập Email Address.
  3. Trường hợp không nhập password sẽ hiển thị thông báo bạn cần nhập Password.
  4. Trường hợp không nhập email và password sẽ hiển thị thông báo bạn cần nhập email và password.
  5. Trường hợp email hoặc password không đúng sẽ hiên thị thông báo email hoặc password của bạn không chính xác.
  6. Trường hợp email được đăng ký và nhập password đúng sẽ đăng nhập thành công.

3. Chuẩn bị trước khi viết một feature

Có thể tham khảo nội dung file README.md của link github Behat-Laravel-Extension.

Hơn nữa có thể xác nhận lại cách viết bằng cách sử dụng command

$ ./vendor/behat/behat/bin/behat -dl

Ngoài ra có thể cài đặt ngôn ngữ test trong feature.

Có thể cài đặt ngôn ngữ ja trong feature như sau.

# language: ja

Các ngôn ngữ khác các bạn có thể làm tương tự. Có thể tham khảo trong kho của MinkExtension rất dễ hiểu.
Link tham khảo : https://github.com/Behat/MinkExtension/blob/master/i18n/ja.xliff

4. Thử viết feature

1 . Trường hợp chưa login thành công sẽ bị redirect ra page login

# 1. Non auth on redirect to login page
  Scenario: Non Authenticated on redirect to login page 
    # home page
    Given I am on the homepage
    # login page
    Then I should be on "/auth/login"

2 . Trường hợp không nhập email

# 2. Try required error of email
  Scenario: try required error of email
    # link auth login
    Given I am on "/auth/login"
    # show form login
    When I fill in the following:
    # input[name=email] is empty
      | email |  |
    # input[name=password] is「password123456」
      | password | password123456 |
    # 「ログインする」click button
    And I press "ログインする"
    # redirect page login
    Then I should be on "/auth/login"
    # should see「ログインに失敗しました。」
    And I should see "ログインに失敗しました。"
    # should see「メールアドレスは必須です。」
    And I should see "メールアドレス は必須です。"

3 . Trường hợp không nhập password và lưu lại thông tin email đã nhập

# 3. Try required error of password, and email input keep
  Scenario: try required error of password, and email input keep
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "パスワード は必須です。"
    # email input keep
    And the "email" field should contain "test@example.com"

4 . Trường hợp không nhập email và password

# 4. Try required error of email and password
  Scenario: try required error of email and password
    Given I am on "/auth/login"
    When I fill in the following:
      | email |  |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレス は必須です。"
    And I should see "パスワード は必須です。"

5 . Trường hợp nhập email đúng, password sai.

# 5. Try error on login
  Scenario: try error on login
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordincorrect |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレスまたはパスワードは不正です。"
    And the "email" field should contain "test@example.com"

6 . Trường hợp nhập đúng email và password

# 6. Auth success
  Scenario: auth success
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordcorrect |
    And I press "ログインする"
    # when login show page admin
    Then I should be on "/"
    And the response should contain "ログアウト"

Gộp các scenario lại ta được file auth.feature có nội dung như dưới :

auth.feature
# Content test
Feature: Laravel Web Auth
  It has access to the top page.
  Change the behavior redirect on whether authenticated.

# 1. Non auth on redirect to login page
  Scenario: Non Authenticated on redirect to login page 
    # home page
    Given I am on the homepage
    # login page
    Then I should be on "/auth/login"


# 2. Try required error of email
  Scenario: try required error of email
    # link auth login
    Given I am on "/auth/login"
    # show form login
    When I fill in the following:
    # input[name=email] is empty
      | email |  |
    # input[name=password] is「password123456」
      | password | password123456 |
    # 「ログインする」click button
    And I press "ログインする"
    # redirect page login
    Then I should be on "/auth/login"
    # should see「ログインに失敗しました。」
    And I should see "ログインに失敗しました。"
    # should see「メールアドレスは必須です。」
    And I should see "メールアドレス は必須です。"


# 3. Try required error of password, and email input keep
  Scenario: try required error of password, and email input keep
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "パスワード は必須です。"
    # email input keep
    And the "email" field should contain "test@example.com"


# 4. Try required error of email and password
  Scenario: try required error of email and password
    Given I am on "/auth/login"
    When I fill in the following:
      | email |  |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレス は必須です。"
    And I should see "パスワード は必須です。"


# 5. Try error on login
  Scenario: try error on login
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordincorrect |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレスとパスワードをお確かめください。"
    And the "email" field should contain "test@example.com"


# 6. Auth success
  Scenario: auth success
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordcorrect |
    And I press "ログインする"
    # when login show page admin
    Then I should be on "/"
    And the response should contain "ログアウト"

5. Run

Sử dụng command :

$ ./vendor/behat/behat/bin/behat

Nếu bạn nhìn thấy màn hình xanh hết các bước test thì kết quả test các kịch bản bạn đưa ra là thành công.

Ngoài ra có thể đặt hiển thị current URL, last URL để kiểm tra xem bước test của mình có thất bại hay thành công.

Then print current URL
Then print last response
Then show last response

Kết Luận

Ứng dụng tool Behat vào các dự án thực tế là rất rộng. Trên đây chỉ là một ví dụ thực tế đơn giản cho việc áp dụng behat vào các dự án thực tiễn. Trên thực tế có nhiều bài toán khó khăn hơn rất nhiều đôi khi việc sử dụng một tool để làm được điều đó là không thể. Vậy nên cần suy nghĩ việc kết hợp nhiều tool với nhau để thực hiện sao cho hợp lý nhất.

Bài toán mới đặt ra là làm thế nào để khi anh Dev mỗi khi push code lên git có thể chạy test một cách tự động lại toàn bộ các scenario. Ở bài viết tiếp theo chúng ta sẽ thực hiện điều đó. Bài viết tiếp theo sẽ là Ứng dụng Jenkins CI vào test tự động.

Tham khảo :
http://behat.org/en/latest/