From cd5c746214d662156317b10e8acf40bbe6dbaff3 Mon Sep 17 00:00:00 2001 From: gokcesahinnn <111432634+gokcesahinnn@users.noreply.github.com> Date: Thu, 30 Mar 2023 16:12:18 +0300 Subject: [PATCH] Add files via upload --- webinar/pom.xml | 67 +++++++++++++++++++ webinar/src/test/README.md | 27 ++++++++ webinar/src/test/java/CucumberRunner.java | 59 ++++++++++++++++ .../createBooking/createBookingCaller.feature | 16 +++++ .../createReqRes/createReqResCaller.feature | 19 ++++++ .../generateToken/createTokenCaller.feature | 12 ++++ .../java/features/getReqRes/getReqRes.feature | 18 +++++ .../updateReqRes/updateReqRes.feature | 17 +++++ .../src/test/java/helpers/DataGenerator.java | 29 ++++++++ .../test/java/helpers/randomDataGenerator.js | 5 ++ webinar/src/test/java/karate-config.js | 21 ++++++ webinar/src/test/java/logback-test.xml | 24 +++++++ webinar/src/test/java/model/createRegRes.json | 4 ++ .../test/java/model/createUpdateBooking.json | 11 +++ 14 files changed, 329 insertions(+) create mode 100644 webinar/pom.xml create mode 100644 webinar/src/test/README.md create mode 100644 webinar/src/test/java/CucumberRunner.java create mode 100644 webinar/src/test/java/caller/createBooking/createBookingCaller.feature create mode 100644 webinar/src/test/java/caller/createReqRes/createReqResCaller.feature create mode 100644 webinar/src/test/java/caller/generateToken/createTokenCaller.feature create mode 100644 webinar/src/test/java/features/getReqRes/getReqRes.feature create mode 100644 webinar/src/test/java/features/updateReqRes/updateReqRes.feature create mode 100644 webinar/src/test/java/helpers/DataGenerator.java create mode 100644 webinar/src/test/java/helpers/randomDataGenerator.js create mode 100644 webinar/src/test/java/karate-config.js create mode 100644 webinar/src/test/java/logback-test.xml create mode 100644 webinar/src/test/java/model/createRegRes.json create mode 100644 webinar/src/test/java/model/createUpdateBooking.json diff --git a/webinar/pom.xml b/webinar/pom.xml new file mode 100644 index 0000000..578c9f7 --- /dev/null +++ b/webinar/pom.xml @@ -0,0 +1,67 @@ + + 4.0.0 + + com.mycompany + webinar + 1.0-SNAPSHOT + jar + + + UTF-8 + 1.8 + 3.8.1 + 2.22.2 + 1.3.1 + + + + + com.intuit.karate + karate-junit5 + ${karate.version} + test + + + + net.masterthought + cucumber-reporting + 5.0.0 + test + + + + + + + + src/test/java + + **/*.java + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.version} + + UTF-8 + ${java.version} + ${java.version} + -Werror + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven.surefire.version} + + -Dfile.encoding=UTF-8 + + + + + + \ No newline at end of file diff --git a/webinar/src/test/README.md b/webinar/src/test/README.md new file mode 100644 index 0000000..619f5d3 --- /dev/null +++ b/webinar/src/test/README.md @@ -0,0 +1,27 @@ +# Kloia Karate Webinar 2023 Karate API Functional Testing Automation Project + + +# Tool stack + +* **Karate Framework** - Development Framework +* **Gatling Framework** - Performance Test Framework +* **Java/Javascript/Scala** - Development Language (For situations where it is necessary) +* **IntelliJ IDE** - Development IDE +* **Maven** - Package Management + +# Quick Setup ( for mac and windows ) + +* cd /Users/gsahin/Desktop/WEBINAR +* mvn archetype:generate -DarchetypeGroupId=com.intuit.karate -DarchetypeArtifactId=karate-archetype -DarchetypeVersion=1.3.1 -DgroupId=com.mycompany -DartifactId=webinar + +# Running Tests + +1. It can be run based on scenario or feature by pressing the green RUN button on the IDE. + + +2. Scenarios determined on the JUnit runner can be run again by pressing the green RUN button. + + +3. `Running it via CLI by giving 'Runner class' and 'tagname'. + + `mvn clean test -Dtest=RunnerName "-Dkarate.options=--tags @tagName"` \ No newline at end of file diff --git a/webinar/src/test/java/CucumberRunner.java b/webinar/src/test/java/CucumberRunner.java new file mode 100644 index 0000000..5d6d958 --- /dev/null +++ b/webinar/src/test/java/CucumberRunner.java @@ -0,0 +1,59 @@ +import com.intuit.karate.Results; +import com.intuit.karate.Runner; +import net.masterthought.cucumber.Configuration; +import net.masterthought.cucumber.ReportBuilder; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static java.lang.Integer.parseInt; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CucumberRunner { + + @Test + void testAll() { + // mvn clean test -Dtest="CucumberRunner#testAll" -DbuildNumber=4445 + // run command line: mvn test -Dtest="CucumberRunner#testAll" + // mvn clean test -Dtest="CucumberRunner#testAll" -Dkarate.options="--tags @regression" + List tagList = Arrays.asList("~@excluded", "~@wip"); + Results results = Runner.path("classpath:features").tags(tagList).outputCucumberJson(true).parallel(0); + generateReport(results.getReportDir()); + } + + @Test + void testSmoke() { + //run command line: mvn test -Dtest="CucumberRunner#testSmoke" + List tagList = Arrays.asList("@smoke", "~@excluded", "~@wip"); + Results results = Runner.path("classpath:features").tags(tagList).outputCucumberJson(true).parallel(0); + generateReport(results.getReportDir()); + } + + @Test + void testParallel() { + // run command line: mvn clean test -Dtest="CucumberRunner#testParallel" "-Dkarate.options=--tags @smoke" -Dcount=10 + String threadCount = System.getProperty("count"); + List tagList = Arrays.asList("~@excluded", "~@wip"); + int count = (threadCount == null) ? 5 : parseInt(threadCount); + Results results = Runner.path("classpath:features").tags(tagList).outputCucumberJson(true).parallel(count); + generateReport(results.getReportDir()); + assertEquals(0, results.getFailCount(), results.getErrorMessages()); + } + + public static void generateReport(String karateOutputPath) { + Collection jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[]{"json"}, true); + List jsonPaths = new ArrayList(jsonFiles.size()); + jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath())); + Configuration config = new Configuration(new File("target"), "karateWebinar"); + //config.setBuildNumber("4444"); + config.setBuildNumber(System.getProperty("buildNumber")); + ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config); + reportBuilder.generateReports(); + + } +} diff --git a/webinar/src/test/java/caller/createBooking/createBookingCaller.feature b/webinar/src/test/java/caller/createBooking/createBookingCaller.feature new file mode 100644 index 0000000..e79ab89 --- /dev/null +++ b/webinar/src/test/java/caller/createBooking/createBookingCaller.feature @@ -0,0 +1,16 @@ +Feature: Create booking Caller + + Background: + Given url baseUrl + And path 'booking' + And header Accept = 'application/json' + * def dataGenerator = Java.type('helpers.DataGenerator') + * def randomName = dataGenerator.generateRandomString(5) + * print "randomName:", randomName + + @create_booking_caller + Scenario: Create Booking + * def requestBody = read('classpath:model/createUpdateBooking.json') + And request requestBody + When method post + Then status 200 \ No newline at end of file diff --git a/webinar/src/test/java/caller/createReqRes/createReqResCaller.feature b/webinar/src/test/java/caller/createReqRes/createReqResCaller.feature new file mode 100644 index 0000000..d562c5b --- /dev/null +++ b/webinar/src/test/java/caller/createReqRes/createReqResCaller.feature @@ -0,0 +1,19 @@ +Feature: Create Req Res Caller + + Background: + Given url reqResBaseUrl + And path 'api/users' + * def dataGenerator = Java.type('helpers.DataGenerator') + * def randomName = dataGenerator.generateRandomString(5) + * def jsRandomName = call read('classpath:helpers/randomDataGenerator.js') + * def name = karate.get('__arg.name',jsRandomName) + * print "name:", name + + @create_req_res_caller + Scenario: Crete Req Res + * def requestBody = read('classpath:model/createRegRes.json') + * requestBody.name = name + And request requestBody + When method post + Then status 201 + diff --git a/webinar/src/test/java/caller/generateToken/createTokenCaller.feature b/webinar/src/test/java/caller/generateToken/createTokenCaller.feature new file mode 100644 index 0000000..b2a3e5c --- /dev/null +++ b/webinar/src/test/java/caller/generateToken/createTokenCaller.feature @@ -0,0 +1,12 @@ +Feature: Create Token Caller + + Background: + Given url baseUrl + And path 'auth' + + @create_token_caller + Scenario: Create Token + * def requestBody = {"username" : "admin","password" : "password123"} + And request requestBody + When method post + Then status 200 \ No newline at end of file diff --git a/webinar/src/test/java/features/getReqRes/getReqRes.feature b/webinar/src/test/java/features/getReqRes/getReqRes.feature new file mode 100644 index 0000000..bbcab53 --- /dev/null +++ b/webinar/src/test/java/features/getReqRes/getReqRes.feature @@ -0,0 +1,18 @@ +Feature: Get Req res Feature + + Background: + * def nameFeature = 'kloia' + * def createReqRes = call read('classpath:caller/createReqRes/createReqResCaller.feature@create_req_res_caller') + * def id = createReqRes.response.id + Given url reqResBaseUrl + + @get_req_res + Scenario Outline: Get req res + And path '/api/users', + When method get + Then status 200 + * print response + Examples: + | id | + | 2 | + | 3 | \ No newline at end of file diff --git a/webinar/src/test/java/features/updateReqRes/updateReqRes.feature b/webinar/src/test/java/features/updateReqRes/updateReqRes.feature new file mode 100644 index 0000000..f6cd49c --- /dev/null +++ b/webinar/src/test/java/features/updateReqRes/updateReqRes.feature @@ -0,0 +1,17 @@ +Feature: + + Background: + * def createReqRes = call read('classpath:caller/createReqRes/createReqResCaller.feature@create_req_res_caller') + * def id = createReqRes.response.id + Given url reqResBaseUrl + And path '/api/users', 2 + + @update_req_res + Scenario: + #* def requestBody = createReqRes.requestBody + * def requestBody = read('classpath:model/createRegRes.json') + * requestBody.job = 'Engineer' + And request requestBody + When method put + Then status 200 + * print response \ No newline at end of file diff --git a/webinar/src/test/java/helpers/DataGenerator.java b/webinar/src/test/java/helpers/DataGenerator.java new file mode 100644 index 0000000..8fd2cfc --- /dev/null +++ b/webinar/src/test/java/helpers/DataGenerator.java @@ -0,0 +1,29 @@ +package helpers; + +import java.util.Random; + +public class DataGenerator { + + public static String generateRandomString(int range) { + String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + StringBuilder randStr = new StringBuilder(); + for (int i = 0; i < range; i++) { + int number = new Random().nextInt(CHAR_LIST.length()); + char ch = CHAR_LIST.charAt(number); + randStr.append(ch); + } + return randStr.toString(); + } + + public static String generateRandomInteger(int range) { + String CHAR_LIST = "1234567890"; + StringBuilder randStr = new StringBuilder(); + for (int i = 0; i < range; i++) { + int number = new Random().nextInt(CHAR_LIST.length()); + char ch = CHAR_LIST.charAt(number); + randStr.append(ch); + } + return randStr.toString(); + } + +} diff --git a/webinar/src/test/java/helpers/randomDataGenerator.js b/webinar/src/test/java/helpers/randomDataGenerator.js new file mode 100644 index 0000000..9c92905 --- /dev/null +++ b/webinar/src/test/java/helpers/randomDataGenerator.js @@ -0,0 +1,5 @@ +function generateRandomName() { + const names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Henry", "Isabella", "James", "Kate", "Liam", "Mia", "Nora", "Oliver", "Penelope", "Quentin", "Riley", "Samantha", "Thomas", "Ursula", "Victoria", "William", "Xander", "Yara", "Zachary"]; + const randomIndex = Math.floor(Math.random() * names.length); + return names[randomIndex]; +} \ No newline at end of file diff --git a/webinar/src/test/java/karate-config.js b/webinar/src/test/java/karate-config.js new file mode 100644 index 0000000..e55294b --- /dev/null +++ b/webinar/src/test/java/karate-config.js @@ -0,0 +1,21 @@ +function fn() { + var env = karate.env; // get system property 'karate.env' + karate.log('karate.env system property was:', env); + if (!env) { + env = 'dev'; + } + var config = { + env: env, + myVarName: 'someValue', + baseUrl: 'https://restful-booker.herokuapp.com/', + reqResBaseUrl:'https://reqres.in/' + }; + + var generalTokenResult = karate.callSingle('classpath:caller/generateToken/createTokenCaller.feature@create_token_caller',config); + config.generalToken = generalTokenResult.response.token; + karate.configure('headers',{'Content-Type':'application/json'}); + + karate.configure('connectTimeout', 50000); + karate.configure('readTimeout', 50000); + return config; +} \ No newline at end of file diff --git a/webinar/src/test/java/logback-test.xml b/webinar/src/test/java/logback-test.xml new file mode 100644 index 0000000..c4d1e2d --- /dev/null +++ b/webinar/src/test/java/logback-test.xml @@ -0,0 +1,24 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + target/karate.log + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + \ No newline at end of file diff --git a/webinar/src/test/java/model/createRegRes.json b/webinar/src/test/java/model/createRegRes.json new file mode 100644 index 0000000..1a1b487 --- /dev/null +++ b/webinar/src/test/java/model/createRegRes.json @@ -0,0 +1,4 @@ +{ + "name": "morpheus", + "job": "leader" +} \ No newline at end of file diff --git a/webinar/src/test/java/model/createUpdateBooking.json b/webinar/src/test/java/model/createUpdateBooking.json new file mode 100644 index 0000000..5727f6d --- /dev/null +++ b/webinar/src/test/java/model/createUpdateBooking.json @@ -0,0 +1,11 @@ +{ + "firstname": "Jim", + "lastname": "Brown", + "totalprice": 111, + "depositpaid": true, + "bookingdates": { + "checkin": "2018-01-01", + "checkout": "2019-01-01" + }, + "additionalneeds": "Breakfast" +} \ No newline at end of file