0

Xây dựng Serenity project

Hiện nay có rất nhiều công cụ tự động hóa quá trình build và quản lý các thư viện, nhằm tối ưu hóa công đoạn biên dịch, đóng gói, thuận tiện hơn cho việc phát triển phần mềm. Tiêu biểu trong số chúng là Gradle và Maven. Thật tuyệt vời là Serenity BDD có thể dễ dàng tích hợp vào các công cụ này giúp quá trình cài đặt, làm việc của chúng ta với Serenity trở nên tối ưu, và tiết kiệm thời gian, chi phí hơn.

Xây dựng dự án Serenity với Gradle

Serenity BDD rất dễ để tích hợp với Gradle bằng cách sử dụng serenity-gradle-plugin. Bạn có thể tham khảo ví dụ dưới đây.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath group: 'net.serenity-bdd', name: 'serenity-gradle-plugin', version: '2.0.80'
    }
}

plugins {
    id 'java'
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'net.serenity-bdd.aggregator'

group 'chungnd'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'net.serenity-bdd', name: 'serenity-core', version: '2.0.80'
    compile group: 'net.serenity-bdd', name: 'serenity-screenplay', version: '2.0.80'
    compile group: 'net.serenity-bdd', name: 'serenity-screenplay-webdriver', version: '2.0.80'
    compile group: 'net.serenity-bdd', name: 'serenity-screenplay-rest', version: '2.0.80'
    compile group: 'net.serenity-bdd', name: 'serenity-cucumber', version: '1.9.45'
}
test {
    testLogging.showStandardStreams = true
    systemProperties System.getProperties()
}
test.finalizedBy(aggregate)

Đầu tiên, chúng ta phải thêm serenity-gradle-plugin vào trong buildscript. Điều này cho phép Gradle tìm, cài đặt và sử dụng các thư viện cho dự án của bạn. Tiếp theo, bạn phải áp dụng plugin này vào dự án bằng các lệnh "apply". Việc cần làm sau đó là thêm các dependencies vào để Gradle tải và cài đặt các thư viện cần thiết cho dự án. Trong Serenity BDD, thường thì chúng ta ít nhất cần sử dụng serenity-core và một thư viện phục vụ cho công việc test. Ở đây tôi sử dụng junit phiên bản 4.12. Ngoài ra các bạn có thể thêm các thư viện khác để phục vụ cho nhu cầu của dự án. Các bạn có thể tìm các thư viện mới nhất ở website https://mvnrepository.com/ Serenity-gradle-plugin sẽ thêm 2 task vào trong dự án là aggregate và checkOutcomes. aggregate có nhiệm vụ lấy file json được tạo ra từ kết quả test, sau đó phân tích lại và tạo thành một báo cáo tổng hợp checkOutcomes thì kiểm tra kết quả đầu ra và báo fail nếu xảy ra lỗi. Để chạy dự án trên cửa sổ dòng lệnh các bạn có thể sử dụng lệnh sau:

gradle clean test

Các test case khi đó sẽ được khởi chạy và tạo ra báo cáo trong thư mục target/site/serenity.

Xây dựng dự án Serenity với Maven

Tương tự như trong Gradle thì để tích hợp Serenity BDD với Maven thì bạn cũng phải sử dụng một plugin tương ứng. Ở đây là serenity-maven-plugin. Bạn có thể tham khảo file xml sau:

<?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>chungnd</groupId>
    <artifactId>swag-labs</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>2.0.60</serenity.version>
        <webdriver.driver>chrome</webdriver.driver>
        <serenity.cucumber.version>1.9.43</serenity.cucumber.version>
    </properties>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray-plugins</name>
            <url>http://jcenter.bintray.com</url>
        </pluginRepository>
    </pluginRepositories>
    <dependencies>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-junit</artifactId>
            <version>2.0.60</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay-webdriver</artifactId>
            <version>2.0.60</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.cucumber.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                    <argLine>-Xmx512m</argLine>
                    <systemPropertyVariables>
                        <webdriver.driver>${webdriver.driver}</webdriver.driver>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> 
</project>

Đầu tiên, chúng ta thêm dependency serenity-core và các thư viện cần thiết vào vào project. Ví dụ như junit, cucumber..v.v. Sau đó bạn cần thêm vào dự án maven-failsafe-plugin. Thông thường, trọng dự án chúng ta sẽ có nhiều trường hợp kiểm thử. Nếu có trường hợp fail thì có thể dự án sẽ bị dừng đột ngột. maven-failsafe-plugin cho phép thực hiện các trường hợp kiểm thử khác cho đến khi tất cả các trường hợp được thực hiện test. Sau đó bạn cần thêm serenity-maven-plugin để thực thi aggregate goal sau khi thực thi post-integration-test. Sau khi aggregate goal được thực thi thì báo cáo tổng hợp của Serenity mới được sinh ra. Để khởi chạy dự án với cửa sổ dòng lệnh bạn gõ lệnh:

mvn clean verify

Sau khi khởi chạy xong các testcase thì báo cáo cũng được tạo ra trong thư mục target/site/serenity. Nếu báo cáo của bạn bị thiếu file index.html. Hãy kiểm tra lại việc cấu hình và thực thi aggegate goal.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí