Karate: Course URL: https://qttechacademy.udemy.com/course/karate-dsl-api-automation-and-performance-from-zero-to-hero/learn/lecture/22174566#overview
To set up of api, we do use dummy ui app.
Steps:
- Clone the repo - https://github.com/gothinkster/angular-realworld-example-app
- open the terminal and 'yarn install'
- npm start - will publish the app in http://localhost:4200/
- Inspect the calls and for API and set it up in Postman
- Created a Sample Tag Request, based on network details from the osted app. Once the setup is done, move to karate framework
Karate Framework:
- Git url: https://github.com/intuit/karate
- Create a folder for this FW, and follow the steps from quickstart - https://github.com/intuit/karate#quickstart
- Import and explore
- To Run, open termninal from VS Code and mvn test will run the default two cases
Tips/ Notes:
- All the feature files present in the same folder / sub folder of Runner file, will only be picked
Plugins:
- Matirial Icon Theme - for all file and folder icon
- Cucumber (Gherkin) Full
- Java Extension Pack
- Karate Runner
- Open in Default browser
- Docker Plugin
Execution single feature file / tags
Efficient way: mvn test "-Dkarate.options=--tags @debug" mvn test "-Dkarate.options=--tags @assertions"
Runner File: org.junit.jupiter.api.Test;
Run cmd to switch env
mvn test "-Dkarate.options=--tags @configTest" -Dkarate.env="dev" mvn test "-Dkarate.options=--tags @configTest" -Dkarate.env="qa"
Configure Global Header
callSingle is the method, take the parameter of the feature file, take a object which holds username and password, and last value after '.' is where the value will get stored
All the values in config file will be available for all the feature files
var accessToken = karate.callSingle('classpath:helpers/CreateToken.feature', config).authToken karate.configure('headers', {Authorization: 'Token' + accessToken})
Schema validations:
Java Faker - For Random test data generator
We can generate the Java file method inside feature file
under """ """
-------
@parallel = false on feature/scneario / scenario outline file will exclude that particular feature from running in parallel otherwise change in the runner for number of thread counts, so you can
To enable cucumber reports https://github.com/damianszczepanik/cucumber-reporting
add maven, add the method in your runner file and call that method from runner file
Method available in - https://github.com/intuit/karate/tree/master/karate-demo
--------
Execution of Karate in Docker Container
- Download and login in Docker Desktop
- Create a file 'Dockerfile' exactly same in root directory
- Since our project is maven, lets pull the latest maven docker image from docker hub https://hub.docker.com/layers/maven/library/maven/3.6.3-jdk-11/images/sha256-1801d353e27e68d60355b371ddfd2ed8d06204bc3266f1746fdade897bb8b6bd?context=explore
- Give the workDIR - host machine path - in this example '/usr/src/app'
- Copy local files to host machine path '/usr/src/app'
- Run the command mvn test with keyword 'CMD mvn test'
7.Build the docker image and run the container
-
Command to build the docker Image without quotes: 'docker build -t karatedockertest .'
here karatedockertest is the name of the image
-
Once you build the docker images, it will list in desktop docker app - under images
-
Run the docker image : 'docker run -it karatedockertest'
There is no cache, it runs in isolated env always. so dependencies will be downloaded everytime we run
'''
Dockerfile without Compose
FROM maven:3.6.3-jdk-11
WORKDIR /usr/src/app
COPY pom.xml /usr/src/app COPY ./src/test/java /usr/src/app/src/test/java
CMD mvn test '''
Docker Compose (Efficient Way) Need for Docker Compose:
- Docker results are stored in container and not in local
- Everytime dependencies are downloaded
Inside 'docker-compose.yml' file
- version is docker compose version , 3.8 being latest now
- services: karate-tests: (Name the service) image: karate-tests (name the image) build: context: . (build under root directory) dockerfile: ./Dockerfile (path of dockerfile inside root directory) volumes: (Mapping) - ./target: /usr/src/app/target (left side host machine, rt side container, copy from host to local mapping so results can be accessed) - ~ /.m2 : /root/.m2 (checks cache first and downloads only new dependency, first checks our local machine) command: mvn test (remove from Dockerfile and add it here)
Build Docker Compose:
docker-compose up --build
File: docker-compose.yml
version: "3.8" services: karate-tests: image: karate-tests build: context: . dockerfile: ./Dockerfile volumes: - ./target:/usr/src/app/target - ~ /.m2:/root/.m2 command: mvn test
Remove mvn test command from Dockerfile when we include compose.
Performance Testing Using Gatling:
Gatling uses Scala, Karate and Gatling goes well. We can use same feature file to execute it.
Lets see the basic example. Refer: https://github.com/intuit/karate/tree/master/karate-gatling
- Add Dependency and Plugin
- Plugin, config the performance folder, change the version
- Create a data folder - for test data used for Simulation - is where Gatling look for testdata
Command to run: mvn clean test-compile gatling:test
If we run this, we will get this error: --> No simulations to run
-
Create a feature file for Perfomance, copy paste any of feature
-
Create a Scala file for Simulation. What is simulation, eg.: loading virtual users to the service, eg. if create is 1 request, we can simulate with 4, 5 or anyusers for that matter
-
Create a scala file, copy paste the content from Usage in same link and edit it
-
setUp() - is the function default example: create.inject(rampUsers(10) during (5 seconds)).protocols(protocol)
-
Run the command again, after updating the scala file and re-run
Scala File after changing:
package aut
import com.intuit.karate.gatling.PreDef._ import io.gatling.core.Predef._ import scala.concurrent.duration._
class PerfTest extends Simulation {
val protocol = karateProtocol()
//protocol.nameResolver = (req, ctx) => req.getHeader("karate-name")
val createArticle = scenario("Create Article").exec(karateFeature("classpath:aut/perfomance/CreateArticlePerfomance.feature"))
setUp( createArticle.inject(atOnceUsers(1)).protocols(protocol) )
}
-
The reports will be present inside target/gatling
-
Change the config the atOnceUsers(3) - so 3 virtuals users will be loaded, check the service calls in the reports after changing the config
Feeders from file, passes dynamic data to virtual users gatling is creating
__gatling - will have gatling session object
Feeder file / test data file is csv file
Stratergies -> to make sure the logic on how to use the testdata by Gatling for our test
Name Resolver: Change the title in the Gatling Report
karate-name is the keyword
Increase the thread pool size for dispatcher for parallel configurations
Change the logback.xml - Debug to Info for minimum logs or to avoid JVM error