Link tham khảo : https://github.com/intuit/karate
Dưới đây là một số ví dụ về cách sử dụng karate :
- 1. For API test scripts
Code: Select all
@booking
Feature: booking ids
Return the ids of all the bookings that existing within the API.
Can take optional query strings to search and return a subset of booking ids.
Background:
* url 'http://restful-booker.herokuapp.com'
Scenario: get all
Given path 'booking'
When method get
Then status 200
And match each response == { bookingid: '#number'}
Scenario Outline: filter by name
Given path 'booking'
And param 'firstname' = "<name>"
When method get
Then status 200
And match each response == { bookingid: '#number'}
Examples:
| name |
| sally |
- 2. For Browser
Code: Select all
Feature: browser automation
Background:
* configure driver = { type: 'chrome', executable: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' }
# * configure driver = { type: 'chromedriver', port: 9515, executable: '/Users/pthomas3/dev/webdriver/chromedriver' }
# * configure driver = { type: 'geckodriver', port: 4444, executable: '/Users/pthomas3/dev/webdriver/geckodriver' }
# * configure driver = { type: 'mswebdriver', port: 17556, executable: 'C:/Users/pthomas3/Downloads/MicrosoftWebDriver.exe' }
# * configure driver = { type: 'safaridriver', port: 4444, executable: 'safaridriver' }
Scenario: try to login to github and then do a google search
Given location 'https://github.com/login'
And input #login_field = 'hello'
And input #password = 'world'
When submit //input[@name='commit']
Then match driver.html('#js-flash-container') contains 'Incorrect username or password.'
Given location 'https://google.com'
And input //input[@name='q'] = 'karate dsl'
When submit //input[@name='btnI']
Then match driver.location == 'https://github.com/intuit/karate'
Code: Select all
<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.tvn</groupId>
<artifactId>rest-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.6.0</maven.compiler.version>
<karate.version>0.9.0.RC4</karate.version>
</properties>
<dependencies>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.20.0</version>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Werror</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>