Nội dung bài học

✅ Cài đặt thư viện Cucumber với Maven Project
✅ Cài đặt Cucumber và Gherkin Plugin trên IntelliJ IDEA
✅ Tạo Cucumber Feature File
✅ Create Cucumber Test Runner
🔆 Kết quả chạy thử nghiệm

Cài đặt thư viện Cucumber với Maven project giống như Selenium Java. Chúng ta sẽ add thư viện vào file pom.xml được lấy từ https://mvnrepository.com/ với version mới nhất.

An có chuẩn bị sẵn ở mục menu bài TÀI NGUYÊN CÀI ĐẶT MÔI TRƯỜNG cho các bạn tiện copy.

✅ Cài đặt thư viện Cucumber với Maven Project

Chúng ta sẽ tạo một project mới với dạng Maven trên IntelliJ IDEA. Sau đó chúng ta cứ update các thư viện với phiên bản mới nhất nhé.

Selenium

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.9.1</version>
</dependency>

 

WebDriverManager

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.3.3</version>
</dependency>

 

TestNG

Khi copy trên MVN về thì bỏ cái dòng <scope>test</scope> ra

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.8.0</version>
</dependency>

 

Cucumber Java

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.12.0</version>
</dependency>


Cucumber TestNG

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>7.12.0</version>
</dependency>

 

Cucumber Gherkin

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-gherkin -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-gherkin</artifactId>
    <version>7.12.0</version>
</dependency>

 

Gherkin

<!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>gherkin</artifactId>
    <version>26.2.0</version>
</dependency>


Trên là các thư viện bắt buộc có. Ngoài ra chúng ta cần add thêm các thư viện bổ trợ khác như bên dưới:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anhtester</groupId>
    <artifactId>CucumberTestNG</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.8.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>7.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-gherkin -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-gherkin</artifactId>
            <version>7.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>26.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.7</version>
        </dependency>


    </dependencies>

</project>



✅ Cài đặt Cucumber và Gherkin Plugin trên IntelliJ IDEA

Các bạn vào mục Plugin trên IntelliJ IDEA và search 2 plugin để cài đặt vào như bên dưới.


🔆 Cucumber for Java

Plugin này dùng để hỗ trợ mã hóa cho các Steps được generate từ Gherkin để nó mapping các bước với nhau.

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester


🔆 Gherkin

Plugin này dùng để hỗ trợ nhận diện nội dung từ ngôn ngữ Gherkin để nó có thể mã hoá được các steps khi generate ra.

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

✅ Tạo Cucumber Feature File

Sau khi cài đặt Plugin hỗ trợ thì các bạn tiến hành tạo Feature file để có thể trình bày một test cases với ngôn ngữ Gherkin.

Chúng ta sẽ tạo tạo folder features để chứa các file với đuôi mở rộng là .feature và đặt nó trong folder resources bên package test src/test/resources/features

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester


File mới tạo ra sẽ không có gì bên trong. Tiếp theo chúng ta dán đoạn test case theo format Gherkin vào.

Feature: Login to CRM
  As a user, I want to be able to log into the CRM system
  So that I can manage customer information

  Scenario: Successful login
    Given I am on the login page
    When I enter my username and password
    And I click the Login button
    Then I should be taken to the Dashboard page
    And I should see the "Customers" menu


[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Format Gherkin hiện màu Vàng là báo hiệu nó chưa tìm thấy code Step Definitions tương ứng. Nhờ có 2 plugin hỗ trợ mà chúng ta đã cài vào IntelliJ nó báo hiệu.

Tiếp theo chúng ta tạo file step definitions để chứa hết tất cả các step trong Gherkin.

Bước 1: Rê chuột vào một step bất kỳ. Dialog gợi ý hiện lên và chọn more actions

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Bước 2: nhấn chọn Create all step definitions để nó generate ra tất cả các step bên trong Gherkin.

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Bước 3: đặt tên cho File java để lưu các steps trên. Các bạn có thể chọn như hình

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Bước 4: Khi nhấn Ok thì nó sẽ tạo file để chứa Steps và Gherkin cũng đã mapping với các steps đó

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Vậy là xong phần tạo Gherkin hay là một Feature file mẫu để chạy code thử nghiệm rồi.

Tiếp theo chúng ta phải cần cài đặt một Cucumber Test Runner để nó mới có thể khởi động được Cucumber Framework và tích hợp với TestNG Framework.

✅ Create Cucumber Test Runner

Các bạn tạo một class mới để vào trong phần package test ví dụ RunCucumberTests sau đó dán đoạn code sau vào:

import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.Test;

@CucumberOptions(
        features = "src/test/resources/features",
        glue = "",
        plugin = {"pretty", "html:target/cucumber-html-report.html"}
)
@Test
public class RunCucumberTests extends AbstractTestNGCucumberTests {

}

 

 

  • Chỉ định vị trí của các tệp Gherkin (.feature): Trong tham số "features" chúng ta chỉ định vị trí của các tệp tính năng. Trong ví dụ này, các tệp tính năng nằm trong thư mục "src/test/resources/features".

  • Chỉ định vị trí của các bước định nghĩa (step definitions): Trong tham số "glue" chúng ta chỉ định vị trí của các steps. Trong ví dụ này, định nghĩa bước nằm trong gói "src/test/java". Nhưng thư mục này nó hiểu là thư mục gốc, và mình không ặt nó nằmtrong package con nào nữa, nên chính vì thế chổ này để trống "".

    Nếu chúng ta để nó vào các package con thì phải ghi theo cú pháp ví dụ: "com.anhtester.stepdefinitions" lưu ý là dùng dấu chấm để phân cách các package.

  • Chỉ định định dạng của các báo cáo: Trong tham số plugin chúng ta chỉ định định dạng của báo cáo nếu cần. Trong ví dụ này, báo cáo sẽ được tạo ở định dạng đã format làm đẹp và lưu dưới dạng file HTML và sẽ được lưu trữ trong thư mục "target/cucumber-html-report".

  • Chạy thử nghiệm: Để chạy thử nghiệm, chỉ cần chạy trên chính lớp RunCucumberTests này luôn dưới dạng thử nghiệm TestNG (@Test). Các bài kiểm tra của Gherkin chỉ định bên trên sẽ được thực hiện và một báo cáo sẽ được tạo ở định dạng được chỉ định.
    Ngoài ra chúng ta có thể gọi class RunCucumberTests này vào file Suite XML để chạy nhờ sự hỗ trợ của TestNG.

    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    <suite name="Suite Name">
        <test name="Test Name">
            <classes>
                <class name="RunCucumberTests"/>
            </classes>
        </test>
    </suite>​

🔆 Kết quả chạy thử nghiệm

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Nó đã chạy pass các step và scenario cũng như gherkin đó chạy thành công.

Nó tự động generate report dạng file html trong thư mục target

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

[Cucumber TestNG] Bài 3: Cài đặt Cucumber Framework với Maven project trên IntelliJ IDEA | Anh Tester

Như vậy là xong rồi đó 😋


Cộng đồng Automation Testing Việt Nam

🌱 Facebook Fanpage: Anh Tester
🌱 Telegram
Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

  • Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu


Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ kiến thức lên trang

Bạn có thể đăng bài để chia sẻ kiến thức, bài viết của chính bạn lên trang Anh Tester Blog

Danh sách bài học