From 344af084543958b6d6b2d9f8cfaf69fcd0eac607 Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Mon, 22 Jan 2018 16:36:45 +0200 Subject: [PATCH 01/10] TestNG Practitest integration implementation --- api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml | 19 ++++- api.v2/java.v2/pom.xml | 51 +++++++++++++ .../CucumberJVMListenerForPractiTest.java | 6 ++ .../listeners/JUnitListenerForPractiTest.java | 6 ++ .../TestNGListenerForPractiTest.java | 48 ++++++++++++ .../practitest/api/example/PractiTestAPI.java | 32 +++++--- .../api/example/PractiTestWriter.java | 38 ++++++++++ .../practitest/integration/ExtractTests.java | 22 ++++++ .../com/webdriver/example/DriverFactory.java | 31 ++++++++ .../java/com/webdriver/example/Utils/Log.java | 74 +++++++++++++++++++ .../src/main/resources/log4j.properties | 16 ++++ .../src/main/resources/project.properties | 5 +- .../java.v2/src/test/java/Cucumber/Hooks.java | 44 +++++++++++ .../src/test/java/TestNG/BaseTest.java | 38 ++++++++++ .../test/java/TestNG/tests/SampleTest.java | 48 ++++++++++++ .../test/java/TestNG/tests/SampleTests2.java | 47 ++++++++++++ api.v2/java.v2/testNG.xml | 15 ++++ 17 files changed, 525 insertions(+), 15 deletions(-) create mode 100644 api.v2/java.v2/src/main/java/com/listeners/CucumberJVMListenerForPractiTest.java create mode 100644 api.v2/java.v2/src/main/java/com/listeners/JUnitListenerForPractiTest.java create mode 100644 api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java create mode 100644 api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java create mode 100644 api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java create mode 100644 api.v2/java.v2/src/main/java/com/webdriver/example/DriverFactory.java create mode 100644 api.v2/java.v2/src/main/java/com/webdriver/example/Utils/Log.java create mode 100644 api.v2/java.v2/src/main/resources/log4j.properties create mode 100644 api.v2/java.v2/src/test/java/Cucumber/Hooks.java create mode 100644 api.v2/java.v2/src/test/java/TestNG/BaseTest.java create mode 100644 api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java create mode 100644 api.v2/java.v2/src/test/java/TestNG/tests/SampleTests2.java create mode 100644 api.v2/java.v2/testNG.xml diff --git a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml index bb0b163..ef53204 100644 --- a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml +++ b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml @@ -1,6 +1,6 @@ - + @@ -32,7 +32,6 @@ - @@ -42,7 +41,7 @@ - + @@ -69,5 +68,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/api.v2/java.v2/pom.xml b/api.v2/java.v2/pom.xml index 46239c8..6d14f2c 100644 --- a/api.v2/java.v2/pom.xml +++ b/api.v2/java.v2/pom.xml @@ -7,6 +7,18 @@ JAVA_INTEGRATION_SAMPLE JAVA_INTEGRATION_SAMPLE 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + @@ -30,6 +42,45 @@ jackson-mapper-asl 1.9.2 + + com.google.guava + guava + 21.0 + + + info.cukes + cucumber-java8 + 1.2.5 + test + + + info.cukes + cucumber-testng + 1.2.5 + + + info.cukes + cucumber-picocontainer + 1.2.5 + test + + + log4j + log4j + 1.2.17 + + + + org.testng + testng + 6.13 + test + + + org.testng + testng + 6.11 + diff --git a/api.v2/java.v2/src/main/java/com/listeners/CucumberJVMListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/CucumberJVMListenerForPractiTest.java new file mode 100644 index 0000000..2688e0a --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/listeners/CucumberJVMListenerForPractiTest.java @@ -0,0 +1,6 @@ +package com.listeners; + +public class CucumberJVMListenerForPractiTest { + + //TODO: implement Cucumber Listener +} diff --git a/api.v2/java.v2/src/main/java/com/listeners/JUnitListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/JUnitListenerForPractiTest.java new file mode 100644 index 0000000..3672205 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/listeners/JUnitListenerForPractiTest.java @@ -0,0 +1,6 @@ +package com.listeners; + +public class JUnitListenerForPractiTest { + + //TODO: implement Junit Listener +} diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java new file mode 100644 index 0000000..e142077 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -0,0 +1,48 @@ +package com.listeners; + +import com.practitest.api.example.PractiTestWriter; +import com.practitest.integration.ExtractTests; +import org.testng.ITestContext; +import org.testng.ITestListener; +import org.testng.ITestResult; + +import java.util.List; + +public class TestNGListenerForPractiTest implements ITestListener { + + + @Override + public void onTestStart(ITestResult result) { + } + + @Override + public void onTestSuccess(ITestResult result) { + } + + @Override + public void onTestFailure(ITestResult result) { + } + + @Override + public void onTestSkipped(ITestResult result) { + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult result) { + } + + @Override + public void onStart(ITestContext context) { + List testIDs = ExtractTests.extractAllTestIds(context); + //Create test run for all tests in current execution + Integer setID = PractiTestWriter.createNewSet(testIDs); + //Store SetID for further usage + System.setProperty("currentSetId", setID.toString()); + PractiTestWriter.createNewSet(testIDs); + } + + + @Override + public void onFinish(ITestContext context) { + } +} diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index e64b1e7..b890821 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -12,49 +12,59 @@ public class PractiTestAPI { + private final static String projectID = System.getProperty("PROJECT_ID"); + public static Response sendGetSteps(String id) { - return RequestFactory.doGet("com/v2/projects/4650/steps.json?test-ids=" +id); + return RequestFactory.doGet("com/v2/projects/"+projectID+"/steps.json?test-ids=" +id); } public static void sendCreateRun(String body) { - RequestFactory.doPost("com/v2/projects/4650/runs.json", body).prettyPrint(); + RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", body); } - public static void sendCreateRun(int instanceID, List step) + public static Response sendCreateRun(int instanceID, List step) { Data data = new Data(); data.setType("instances"); data.setAttributes(new Attributes(instanceID)); data.setSteps(new Steps(step)); - RequestFactory.doPost("com/v2/projects/4650/runs.json", new RunsModel(data)).prettyPrint(); + return RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + } + + public static Response sendCreateRun(int instanceID) + { + Data data = new Data(); + data.setType("instances"); + data.setAttributes(new Attributes(instanceID)); + return RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } public static void sendCreateInstance(String body) { - RequestFactory.doPost("com/v2/projects/4650/instances.json", body).prettyPrint(); + RequestFactory.doPost("com/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); } - public static void sendCreateInstance(int setID, int testID) + public static Response sendCreateInstance(int setID, int testID) { com.practitest.api.model.instance.Data data = new com.practitest.api.model.instance.Data(); data.setAttributes(new com.practitest.api.model.instance.Attributes(setID, testID)); - RequestFactory.doPost("com/v2/projects/4650/instances.json", new InstanceModel(data)).prettyPrint(); + return RequestFactory.doPost("com/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); } - public static void sendCreateTestSet(String body) + public static Response sendCreateTestSet(String body) { - RequestFactory.doPost("com/v2/projects/4650/sets.json", body).prettyPrint(); + return RequestFactory.doPost("com/v2/projects/"+projectID+"/sets.json", body); } - public static void sendCreateTestSet(String name, List testIDs) + public static Response sendCreateTestSet(String name, List testIDs) { com.practitest.api.model.sets.Data data = new com.practitest.api.model.sets.Data(); data.setInstances(new Instances(testIDs)); data.setAttributes(new com.practitest.api.model.sets.Attributes(name)); - RequestFactory.doPost("com/v2/projects/4650/sets.json", new SetsModel(data)).prettyPrint(); + return RequestFactory.doPost("com/v2/projects/"+projectID+"/sets.json", new SetsModel(data)); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java new file mode 100644 index 0000000..cdcbe7a --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -0,0 +1,38 @@ +package com.practitest.api.example; + +import com.jayway.restassured.response.Response; +import com.practitest.api.model.runs.StepModel; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +public class PractiTestWriter { + + private static String getNameForNewRun() { + DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); + Date date = new Date(); + String testSuite = System.getProperty("test"); + return testSuite + " " + dateFormat.format(date); + } + + + public static Integer createNewSet(List testIDs) + { + Response response = PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs); + return PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs).getBody().jsonPath().get("data.id") ; + } + + public static Integer createNewInstance(Integer setID, Integer testID) + { + return PractiTestAPI.sendCreateInstance(setID, testID).getBody().jsonPath().get("data.id"); + } + + public static Integer submitResults(Integer instanceID, List stepModel) + { + return PractiTestAPI.sendCreateRun(instanceID, stepModel).getBody().jsonPath().get("get.id"); + + } + +} diff --git a/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java b/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java new file mode 100644 index 0000000..3d206e7 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java @@ -0,0 +1,22 @@ +package com.practitest.integration; + +import org.testng.ITestContext; +import org.testng.ITestNGMethod; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class ExtractTests { + + public static List extractAllTestIds(ITestContext context) + { + ITestNGMethod[] testMethods = context.getAllTestMethods(); + List testIds = new ArrayList(0); + for (ITestNGMethod testMethod : testMethods) { + testIds.add(testMethod.getDescription()); + } + return testIds.stream().map(Integer::parseInt).collect(Collectors.toList()); + } + +} diff --git a/api.v2/java.v2/src/main/java/com/webdriver/example/DriverFactory.java b/api.v2/java.v2/src/main/java/com/webdriver/example/DriverFactory.java new file mode 100644 index 0000000..cf10912 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/webdriver/example/DriverFactory.java @@ -0,0 +1,31 @@ +package com.webdriver.example; + +import io.github.bonigarcia.wdm.ChromeDriverManager; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; + +public class DriverFactory { + + static{ + ChromeDriverManager.getInstance().arch64().setup(); + } + + + private static ThreadLocal driverContainer = new ThreadLocal<>(); + + + public static WebDriver getBrowserInstance(){ + return driverContainer.get(); + } + + public static WebDriver openChrome() { + + driverContainer.set(new ChromeDriver()); + return driverContainer.get(); + } + + public static void tearDown() { + driverContainer.get().quit(); + driverContainer.set(null); + } +} diff --git a/api.v2/java.v2/src/main/java/com/webdriver/example/Utils/Log.java b/api.v2/java.v2/src/main/java/com/webdriver/example/Utils/Log.java new file mode 100644 index 0000000..52b43b2 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/webdriver/example/Utils/Log.java @@ -0,0 +1,74 @@ +package com.webdriver.example.Utils; + +import org.apache.log4j.Logger; + +public class Log { + + // Initialize Log4j logs + + private static Logger Log = Logger.getLogger(Log.class.getName());// + + // This is to print log for the beginning of the test case, as we usually run so many test cases as a test suite + + public static void startTestCase(){ + + Log.info("****************************************************************************************"); + + Log.info("****************************************************************************************"); + + Log.info("$$$$$$$$$$$$$$$$$$$$$ -S-T-A-R-T- $$$$$$$$$$$$$$$$$$$$$$$$$"); + + Log.info("****************************************************************************************"); + + Log.info("****************************************************************************************"); + + } + + //This is to print log for the ending of the test case + + public static void endTestCase(){ + + Log.info("XXXXXXXXXXXXXXXXXXXXXXX "+"-E---N---D-"+" XXXXXXXXXXXXXXXXXXXXXX"); + + Log.info("X"); + + Log.info("X"); + + Log.info("X"); + + Log.info("X"); + + } + + // Need to create these methods, so that they can be called + + public static void info(String message) { + + Log.info(message); + + } + + public static void warn(String message) { + + Log.warn(message); + + } + + public static void error(String message) { + + Log.error(message); + + } + + public static void fatal(String message) { + + Log.fatal(message); + + } + + public static void debug(String message) { + + Log.debug(message); + + } +} diff --git a/api.v2/java.v2/src/main/resources/log4j.properties b/api.v2/java.v2/src/main/resources/log4j.properties new file mode 100644 index 0000000..499aa94 --- /dev/null +++ b/api.v2/java.v2/src/main/resources/log4j.properties @@ -0,0 +1,16 @@ +# Root logger option +log4j.rootLogger=INFO, recipt, stdout + +# Direct log messages to a log recipt +log4j.appender.recipt=org.apache.log4j.RollingFileAppender +log4j.appender.recipt.File=logging.log +log4j.appender.recipt.MaxFileSize=10MB +log4j.appender.recipt.MaxBackupIndex=10 +log4j.appender.recipt.layout=org.apache.log4j.PatternLayout +log4j.appender.recipt.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Direct log messages to stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/api.v2/java.v2/src/main/resources/project.properties b/api.v2/java.v2/src/main/resources/project.properties index 2ac658e..3d5a92d 100644 --- a/api.v2/java.v2/src/main/resources/project.properties +++ b/api.v2/java.v2/src/main/resources/project.properties @@ -1,3 +1,6 @@ URI=URI DEVELOPER_EMAIL=developer@mail.com -API_TOKEN=TOKEN \ No newline at end of file +API_TOKEN=TOKEN +PROJECT_ID=project_id +test=googleTest +currentSetId= \ No newline at end of file diff --git a/api.v2/java.v2/src/test/java/Cucumber/Hooks.java b/api.v2/java.v2/src/test/java/Cucumber/Hooks.java new file mode 100644 index 0000000..547dfbe --- /dev/null +++ b/api.v2/java.v2/src/test/java/Cucumber/Hooks.java @@ -0,0 +1,44 @@ +package Cucumber; + +import com.webdriver.example.DriverFactory; +import com.webdriver.example.Utils.Log; +import cucumber.api.Scenario; +import cucumber.api.java.After; +import cucumber.api.java.Before; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriverException; + +public class Hooks { + public static ThreadLocal lastScenario = new ThreadLocal<>(); + + @Before + public void openBrowser() { + Log.startTestCase(); + DriverFactory.openChrome(); + DriverFactory.getBrowserInstance().manage().window().maximize(); + } + + + @After + public void afterScenario(Scenario scenario) { + lastScenario.set(scenario); + try { + if (scenario.isFailed()) { + try { + scenario.write("Current Page URL is " + DriverFactory.getBrowserInstance().getCurrentUrl()); + byte[] screenshot = ((TakesScreenshot) DriverFactory.getBrowserInstance()).getScreenshotAs(OutputType.BYTES); + scenario.embed(screenshot, "image/png"); + } catch (WebDriverException somePlatformsDontSupportScreenshots) { + System.err.println(somePlatformsDontSupportScreenshots.getMessage()); + } + } + }finally { + DriverFactory.tearDown(); + //if (TestRailWriter.isIsTestRailActive()) { TODO: implement practitest usage + // TestRailWriter.afterScenario(); TODO: implement after scenario case usage + // } + Log.endTestCase(); + } + } +} diff --git a/api.v2/java.v2/src/test/java/TestNG/BaseTest.java b/api.v2/java.v2/src/test/java/TestNG/BaseTest.java new file mode 100644 index 0000000..574a794 --- /dev/null +++ b/api.v2/java.v2/src/test/java/TestNG/BaseTest.java @@ -0,0 +1,38 @@ +package TestNG; + +import com.webdriver.example.DriverFactory; +import com.webdriver.example.Utils.Log; +import org.testng.annotations.AfterSuite; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.BeforeTest; + +public class BaseTest { + + @BeforeSuite + public void beforeTestSuite() + { + + } + + @BeforeTest + public void beforeTest() + { + Log.startTestCase(); + DriverFactory.openChrome(); + DriverFactory.getBrowserInstance().manage().window().maximize(); + } + + @AfterTest + public void afterTest() + { + DriverFactory.tearDown(); + Log.endTestCase(); + } + + @AfterSuite + public void afterTestSuite() + { + + } +} diff --git a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java new file mode 100644 index 0000000..31e0b2d --- /dev/null +++ b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java @@ -0,0 +1,48 @@ +package TestNG.tests; + +import TestNG.BaseTest; +import com.listeners.TestNGListenerForPractiTest; +import com.webdriver.example.Utils.Log; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +@Listeners(TestNGListenerForPractiTest.class) +public class SampleTest extends BaseTest{ + + + @Test(description = "1") + public void googleTest() + { + Log.info("Just checking"); + } + + @Test(description = "2") + public void googleTest1() + { + Log.info("Just checking"); + } + + @Test(description = "3") + public void googleTest2() + { + Log.info("Just 4"); + } + + @Test(description = "5") + public void googleTest3() + { + Log.info("Just 6"); + } + + @Test(description = "7") + public void googleTest4() + { + Log.info("Just 8"); + } + + @Test(description = "9") + public void googleTest5() + { + Log.info("Just checking"); + } +} diff --git a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTests2.java b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTests2.java new file mode 100644 index 0000000..e884cb2 --- /dev/null +++ b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTests2.java @@ -0,0 +1,47 @@ +package TestNG.tests; + +import com.listeners.TestNGListenerForPractiTest; +import com.webdriver.example.Utils.Log; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + + +@Listeners(TestNGListenerForPractiTest.class) +public class SampleTests2 { + + @Test(description = "10") + public void googleTest() + { + Log.info("Just checking"); + } + + @Test(description = "12") + public void googleTest1() + { + Log.info("Just checking"); + } + + @Test(description = "13") + public void googleTest2() + { + Log.info("Just 4"); + } + + @Test(description = "15") + public void googleTest3() + { + Log.info("Just 6"); + } + + @Test(description = "17") + public void googleTest4() + { + Log.info("Just 8"); + } + + @Test(description = "19") + public void googleTest5() + { + Log.info("Just checking"); + } +} diff --git a/api.v2/java.v2/testNG.xml b/api.v2/java.v2/testNG.xml new file mode 100644 index 0000000..e34e1d3 --- /dev/null +++ b/api.v2/java.v2/testNG.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file From 71ab1801bda969621d5a7e806547aa4b40f07a1c Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Wed, 7 Feb 2018 18:56:43 +0200 Subject: [PATCH 02/10] Update PractiTestAPI.java removed com from end point path. --- .../practitest/api/example/PractiTestAPI.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index b890821..614a830 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -16,12 +16,12 @@ public class PractiTestAPI { public static Response sendGetSteps(String id) { - return RequestFactory.doGet("com/v2/projects/"+projectID+"/steps.json?test-ids=" +id); + return RequestFactory.doGet("/v2/projects/"+projectID+"/steps.json?test-ids=" +id); } public static void sendCreateRun(String body) { - RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", body); + RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", body); } public static Response sendCreateRun(int instanceID, List step) @@ -30,7 +30,7 @@ public static Response sendCreateRun(int instanceID, List step) data.setType("instances"); data.setAttributes(new Attributes(instanceID)); data.setSteps(new Steps(step)); - return RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } public static Response sendCreateRun(int instanceID) @@ -38,12 +38,12 @@ public static Response sendCreateRun(int instanceID) Data data = new Data(); data.setType("instances"); data.setAttributes(new Attributes(instanceID)); - return RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } public static void sendCreateInstance(String body) { - RequestFactory.doPost("com/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); + RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); } public static Response sendCreateInstance(int setID, int testID) @@ -51,12 +51,12 @@ public static Response sendCreateInstance(int setID, int testID) com.practitest.api.model.instance.Data data = new com.practitest.api.model.instance.Data(); data.setAttributes(new com.practitest.api.model.instance.Attributes(setID, testID)); - return RequestFactory.doPost("com/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); + return RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); } public static Response sendCreateTestSet(String body) { - return RequestFactory.doPost("com/v2/projects/"+projectID+"/sets.json", body); + return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", body); } public static Response sendCreateTestSet(String name, List testIDs) @@ -64,7 +64,7 @@ public static Response sendCreateTestSet(String name, List testIDs) com.practitest.api.model.sets.Data data = new com.practitest.api.model.sets.Data(); data.setInstances(new Instances(testIDs)); data.setAttributes(new com.practitest.api.model.sets.Attributes(name)); - return RequestFactory.doPost("com/v2/projects/"+projectID+"/sets.json", new SetsModel(data)); + return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", new SetsModel(data)); } From 13cd1c625bc12f0f7ce8a0a36b8f7688a16e320d Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Sun, 25 Feb 2018 14:50:25 +0400 Subject: [PATCH 03/10] Existing Test Set iD usage implementation --- api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml | 6 +- .../TestNGListenerForPractiTest.java | 25 +++++-- .../practitest/api/example/PractiTestAPI.java | 70 ++++++++++++++++++- .../api/example/PractiTestWriter.java | 46 ++++++++++++ .../practitest/api/model/runs/Attributes.java | 18 ++++- 5 files changed, 154 insertions(+), 11 deletions(-) diff --git a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml index ef53204..bff2e98 100644 --- a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml +++ b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml @@ -41,8 +41,10 @@ - - + + + + diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index e142077..2547993 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -2,6 +2,7 @@ import com.practitest.api.example.PractiTestWriter; import com.practitest.integration.ExtractTests; +import com.webdriver.example.Utils.Log; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; @@ -10,6 +11,8 @@ public class TestNGListenerForPractiTest implements ITestListener { + protected Integer instanceID = null; + @Override public void onTestStart(ITestResult result) { @@ -17,10 +20,12 @@ public void onTestStart(ITestResult result) { @Override public void onTestSuccess(ITestResult result) { + PractiTestWriter.submitResults(instanceID, 0); } @Override public void onTestFailure(ITestResult result) { + PractiTestWriter.submitResults(instanceID, 1); } @Override @@ -33,12 +38,20 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { - List testIDs = ExtractTests.extractAllTestIds(context); - //Create test run for all tests in current execution - Integer setID = PractiTestWriter.createNewSet(testIDs); - //Store SetID for further usage - System.setProperty("currentSetId", setID.toString()); - PractiTestWriter.createNewSet(testIDs); + Integer existingTestID = PractiTestWriter.getSetID(); + if (existingTestID == 0) + { + List testIDs = ExtractTests.extractAllTestIds(context); + //Create test run for all tests in current execution + Integer setID = PractiTestWriter.createNewSet(testIDs); + //Create new instance + this.instanceID = PractiTestWriter.createNewInstance(setID, testIDs); + } + else + { + Log.info("Using existing TestSEtID: "+existingTestID.toString()); + } + } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index 614a830..736980a 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -12,40 +12,86 @@ public class PractiTestAPI { + /** + * Reads project.properties file for projectID + */ private final static String projectID = System.getProperty("PROJECT_ID"); + /** + * + * @param id + * @return + */ public static Response sendGetSteps(String id) { return RequestFactory.doGet("/v2/projects/"+projectID+"/steps.json?test-ids=" +id); } + /** + * + * @param body + */ public static void sendCreateRun(String body) { RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", body); } + /** + * + * @param instanceID + * @param step + * @return + */ public static Response sendCreateRun(int instanceID, List step) { Data data = new Data(); data.setType("instances"); - data.setAttributes(new Attributes(instanceID)); + data.setAttributes(new Attributes(instanceID, 0)); data.setSteps(new Steps(step)); return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } + /** + * + * @param instanceID + * @return + */ public static Response sendCreateRun(int instanceID) { Data data = new Data(); data.setType("instances"); - data.setAttributes(new Attributes(instanceID)); + data.setAttributes(new Attributes(instanceID, 0)); return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } + /** + * + * @param instanceID + * @return + */ + public static Response sendSubmitResult(int instanceID, int exitCode) + { + Data data = new Data(); + data.setType("instances"); + data.setAttributes(new Attributes(instanceID, exitCode)); + return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + } + + /** + * + * @param body + */ public static void sendCreateInstance(String body) { RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); } + /** + * + * @param setID + * @param testID + * @return + */ public static Response sendCreateInstance(int setID, int testID) { com.practitest.api.model.instance.Data data = new com.practitest.api.model.instance.Data(); @@ -54,11 +100,31 @@ public static Response sendCreateInstance(int setID, int testID) return RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); } + /** + * + * @param body + * @return + */ public static Response sendCreateTestSet(String body) { return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", body); } + /** + * + * @return + */ + public static Response sendGetTestSet() + { + return RequestFactory.doGet("/v2/projects/"+projectID+"sets.json"); + } + + /** + * + * @param name + * @param testIDs + * @return + */ public static Response sendCreateTestSet(String name, List testIDs) { com.practitest.api.model.sets.Data data = new com.practitest.api.model.sets.Data(); diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index cdcbe7a..b258627 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -10,6 +10,10 @@ public class PractiTestWriter { + /** + * + * @return Generates name for new test run with time stamp + */ private static String getNameForNewRun() { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(); @@ -17,22 +21,64 @@ private static String getNameForNewRun() { return testSuite + " " + dateFormat.format(date); } + /** + * + * @return set id for the project + */ + public static Integer getSetID() + { + Response response = PractiTestAPI.sendGetTestSet(); + try + { + return response.getBody().jsonPath().get("data.id"); + } + catch (Exception e) + { + return 0; + } + } + /** + * + * @param testIDs List of tests which should be assigned to this Test Set + * @return new TestSetID + */ public static Integer createNewSet(List testIDs) { Response response = PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs); return PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs).getBody().jsonPath().get("data.id") ; } + /** + * + * @param setID TestSet ID which should be used for new Instance + * @param testID TestCase ID which should be used to create new instance + * @return new Instance ID + */ public static Integer createNewInstance(Integer setID, Integer testID) { return PractiTestAPI.sendCreateInstance(setID, testID).getBody().jsonPath().get("data.id"); } + public static Integer createNewInstance(Integer setID, List testID) + { + return PractiTestAPI.sendCreateInstance(setID, testID.get(0)).getBody().jsonPath().get("data.id"); + } + + /** + * + * @param instanceID instance ID for test execution + * @param stepModel step Model which was used for this test + * @return extracts result ID for further usage + */ public static Integer submitResults(Integer instanceID, List stepModel) { return PractiTestAPI.sendCreateRun(instanceID, stepModel).getBody().jsonPath().get("get.id"); + } + public static Integer submitResults(Integer instanceID, Integer statusCode) + { + return PractiTestAPI.sendSubmitResult(instanceID, statusCode).getBody().jsonPath().get("data.id"); } } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java index c768d31..e26544c 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java @@ -14,6 +14,11 @@ public class Attributes { @JsonProperty("instance-id") private int instanceId; + + + @JsonProperty("exit-code") + private int exitcode; + /** * No args constructor for use in serialization * @@ -27,9 +32,10 @@ public Attributes() { */ - public Attributes(int instanceId) { + public Attributes(int instanceId, int exitcode) { super(); this.instanceId = instanceId; + this.exitcode = exitcode; } @JsonProperty("instance-id") @@ -42,4 +48,14 @@ public void setInstanceid(int instanceId) { this.instanceId = instanceId; } + @JsonProperty("exit-code") + public int getExitcode() { + return exitcode; + } + + @JsonProperty("exit-code") + public void setExitcode(int exitcode) { + this.exitcode = exitcode; + } + } From baebba446abb08b19149d0b79e4e67057079600d Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Tue, 20 Mar 2018 04:34:32 +0200 Subject: [PATCH 04/10] implementation based on code review comments from Stas. --- .../TestNGListenerForPractiTest.java | 29 ++++++---- .../practitest/api/example/PractiTestAPI.java | 53 ++++++++++++++----- .../api/example/PractiTestWriter.java | 39 +++++++++++--- .../practitest/api/model/runs/Attributes.java | 8 +-- 4 files changed, 98 insertions(+), 31 deletions(-) diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index 2547993..804aeec 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -11,7 +11,7 @@ public class TestNGListenerForPractiTest implements ITestListener { - protected Integer instanceID = null; + protected Integer setID = null; @Override @@ -20,11 +20,13 @@ public void onTestStart(ITestResult result) { @Override public void onTestSuccess(ITestResult result) { + String instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); PractiTestWriter.submitResults(instanceID, 0); } @Override public void onTestFailure(ITestResult result) { + String instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); PractiTestWriter.submitResults(instanceID, 1); } @@ -38,20 +40,29 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { - Integer existingTestID = PractiTestWriter.getSetID(); - if (existingTestID == 0) + Integer existingTestSetID = PractiTestWriter.getSetID("ExistingSet"); + //extract all tests for current execution + List testIDs = ExtractTests.extractAllTestIds(context); + if (existingTestSetID == 0) { - List testIDs = ExtractTests.extractAllTestIds(context); //Create test run for all tests in current execution - Integer setID = PractiTestWriter.createNewSet(testIDs); - //Create new instance - this.instanceID = PractiTestWriter.createNewInstance(setID, testIDs); + this.setID = PractiTestWriter.createNewSet(testIDs); } else { - Log.info("Using existing TestSEtID: "+existingTestID.toString()); + this.setID = existingTestSetID; + //get test id's for existing TestSet + List currentTestSetTestIDs = PractiTestWriter.getTestIDsForTestSetID(this.setID); + //Remove existing test IDs from List to get removed + currentTestSetTestIDs.removeAll(testIDs); + //Remove Instance IDs + for (Integer currentTestSetTestID : currentTestSetTestIDs) { + PractiTestWriter.removeInstance(PractiTestWriter.getInstancesByTestID(currentTestSetTestID)); + } + Log.info("Using existing TestSEtID: "+existingTestSetID.toString()); } - + //Create new instances for all tests in TestSet + PractiTestWriter.createAllInstances(this.setID, testIDs); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index 736980a..ad929cd 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -26,15 +26,15 @@ public static Response sendGetSteps(String id) { return RequestFactory.doGet("/v2/projects/"+projectID+"/steps.json?test-ids=" +id); } - - /** - * - * @param body - */ - public static void sendCreateRun(String body) - { - RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", body); - } +// +// /** +// * +// * @param body +// */ +// public static void sendCreateRun(String body) +// { +// RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", body); +// } /** * @@ -42,7 +42,7 @@ public static void sendCreateRun(String body) * @param step * @return */ - public static Response sendCreateRun(int instanceID, List step) + public static Response sendCreateRun(String instanceID, List step) { Data data = new Data(); data.setType("instances"); @@ -56,7 +56,7 @@ public static Response sendCreateRun(int instanceID, List step) * @param instanceID * @return */ - public static Response sendCreateRun(int instanceID) + public static Response sendCreateRun(String instanceID) { Data data = new Data(); data.setType("instances"); @@ -69,7 +69,7 @@ public static Response sendCreateRun(int instanceID) * @param instanceID * @return */ - public static Response sendSubmitResult(int instanceID, int exitCode) + public static Response sendSubmitResult(String instanceID, int exitCode) { Data data = new Data(); data.setType("instances"); @@ -86,6 +86,26 @@ public static void sendCreateInstance(String body) RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); } + public static Response sendGetInstances(Integer testSetID) + { + return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?set-ids="+testSetID.toString()); + } + + public static Response sendGetInstanceBytestID(Integer testID) + { + return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?test-ids="+testID.toString()); + } + + public static Response sendGetInstanceByTestIDAndTestSetID(String testID, Integer testSetID) + { + return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?test-ids="+testID+"&set-ids="+testSetID.toString()); + } + + public static Response sendRemoveInstance(Integer instanceID) + { + return RequestFactory.doDelete("/v2/projects/"+projectID+"/instances/"+instanceID.toString()+".json", ""); + } + /** * * @param setID @@ -119,6 +139,15 @@ public static Response sendGetTestSet() return RequestFactory.doGet("/v2/projects/"+projectID+"sets.json"); } + /** + * + * @return + */ + public static Response sendGetTestSetByName(String nameExect) + { + return RequestFactory.doGet("/v2/projects/"+projectID+"sets.json?name_exact="+nameExect); + } + /** * * @param name diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index b258627..e36dc51 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -25,9 +25,9 @@ private static String getNameForNewRun() { * * @return set id for the project */ - public static Integer getSetID() + public static Integer getSetID(String setName) { - Response response = PractiTestAPI.sendGetTestSet(); + Response response = PractiTestAPI.sendGetTestSetByName(setName); try { return response.getBody().jsonPath().get("data.id"); @@ -60,9 +60,36 @@ public static Integer createNewInstance(Integer setID, Integer testID) return PractiTestAPI.sendCreateInstance(setID, testID).getBody().jsonPath().get("data.id"); } - public static Integer createNewInstance(Integer setID, List testID) + public static void createAllInstances(Integer setID, List testID) { - return PractiTestAPI.sendCreateInstance(setID, testID.get(0)).getBody().jsonPath().get("data.id"); + for (Integer aTestID : testID) { + createNewInstance(setID, aTestID); + } + } + + public static List getInstancesByTestSetID(Integer testSet) + { + return PractiTestAPI.sendGetInstances(testSet).body().jsonPath().get("data.id"); + } + + public static Integer getInstancesByTestID(Integer testID) + { + return PractiTestAPI.sendGetInstanceBytestID(testID).body().jsonPath().get("data.id"); + } + + public static String getInstancesByTestIDAndTestSetID(String testID, Integer testSetID) + { + return PractiTestAPI.sendGetInstanceByTestIDAndTestSetID(testID, testSetID).body().jsonPath().get("data.id"); + } + + public static List getTestIDsForTestSetID(Integer testSet) + { + return PractiTestAPI.sendGetInstances(testSet).body().jsonPath().get("data.attributes.test-id"); + } + + public static void removeInstance(Integer instanceID) + { + PractiTestAPI.sendRemoveInstance(instanceID); } /** @@ -71,12 +98,12 @@ public static Integer createNewInstance(Integer setID, List testID) * @param stepModel step Model which was used for this test * @return extracts result ID for further usage */ - public static Integer submitResults(Integer instanceID, List stepModel) + public static Integer submitResults(String instanceID, List stepModel) { return PractiTestAPI.sendCreateRun(instanceID, stepModel).getBody().jsonPath().get("get.id"); } - public static Integer submitResults(Integer instanceID, Integer statusCode) + public static Integer submitResults(String instanceID, Integer statusCode) { return PractiTestAPI.sendSubmitResult(instanceID, statusCode).getBody().jsonPath().get("data.id"); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java index e26544c..0deb0e6 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java @@ -12,7 +12,7 @@ public class Attributes { @JsonProperty("instance-id") - private int instanceId; + private String instanceId; @@ -32,19 +32,19 @@ public Attributes() { */ - public Attributes(int instanceId, int exitcode) { + public Attributes(String instanceId, int exitcode) { super(); this.instanceId = instanceId; this.exitcode = exitcode; } @JsonProperty("instance-id") - public int getInstanceid() { + public String getInstanceid() { return instanceId; } @JsonProperty("instance-id") - public void setInstanceid(int instanceId) { + public void setInstanceid(String instanceId) { this.instanceId = instanceId; } From acf1de915c7d481533f6281997be4189705415b0 Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Tue, 20 Mar 2018 04:35:00 +0200 Subject: [PATCH 05/10] missed file --- api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml index bff2e98..8c734fd 100644 --- a/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml +++ b/api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml @@ -41,7 +41,7 @@ - + From 10ccf9a6f4a8ac8d59fb604f2b59098500e48162 Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Tue, 27 Mar 2018 16:49:15 +0300 Subject: [PATCH 06/10] fix all dependencies for project to be executable --- .../main/java/com/Config/GeneralConfig.java | 35 +++++++++++++ .../TestNGListenerForPractiTest.java | 16 +++--- .../practitest/api/common/RequestFactory.java | 45 +++++------------ .../practitest/api/example/PractiTestAPI.java | 50 +++++++++++-------- .../api/example/PractiTestWriter.java | 22 ++++---- .../api/model/instance/Attributes.java | 8 +-- .../src/main/resources/project.properties | 7 ++- .../java.v2/src/test/java/RunWithTests.java | 4 +- .../test/java/TestNG/tests/SampleTest.java | 12 ++--- 9 files changed, 113 insertions(+), 86 deletions(-) create mode 100644 api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java diff --git a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java new file mode 100644 index 0000000..16ff649 --- /dev/null +++ b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java @@ -0,0 +1,35 @@ +package com.Config; + +import com.webdriver.example.Utils.Log; + +import java.io.FileInputStream; +import java.util.Properties; + +public class GeneralConfig { + + public static final String PROPERTIES_PATH = "src/main/resources/project.properties"; + public static final String URI = "URI"; + public static final String DEV_MAIL = "DEVELOPER_EMAIL"; + public static final String API_TOKEN = "API_TOKEN"; + public static final String PROJECT_ID = "PROJECT_ID"; + + protected static Properties m_props = new Properties(); + + + + public static String getConfigurationValue(final String configurationName) { + + try{ + m_props.load(new FileInputStream(PROPERTIES_PATH)); + } + catch (Exception e) { + e.printStackTrace(); + Log.error("Unable to load parameters"); + } + return m_props.getProperty(configurationName); + } + + public static boolean setConfigurationValue(final String configurationName, final String value) { + return m_props.setProperty(configurationName, value) != null; + } +} diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index 804aeec..e14a9f0 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -11,7 +11,7 @@ public class TestNGListenerForPractiTest implements ITestListener { - protected Integer setID = null; + protected String setID = null; @Override @@ -20,14 +20,14 @@ public void onTestStart(ITestResult result) { @Override public void onTestSuccess(ITestResult result) { - String instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); - PractiTestWriter.submitResults(instanceID, 0); + List instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); + PractiTestWriter.submitResults(instanceID.get(0), 0); } @Override public void onTestFailure(ITestResult result) { - String instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); - PractiTestWriter.submitResults(instanceID, 1); + List instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); + PractiTestWriter.submitResults(instanceID.get(0), 1); } @Override @@ -40,10 +40,10 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { - Integer existingTestSetID = PractiTestWriter.getSetID("ExistingSet"); + String existingTestSetID = PractiTestWriter.getSetID("bla"); //extract all tests for current execution List testIDs = ExtractTests.extractAllTestIds(context); - if (existingTestSetID == 0) + if (existingTestSetID == null) { //Create test run for all tests in current execution this.setID = PractiTestWriter.createNewSet(testIDs); @@ -59,7 +59,7 @@ public void onStart(ITestContext context) { for (Integer currentTestSetTestID : currentTestSetTestIDs) { PractiTestWriter.removeInstance(PractiTestWriter.getInstancesByTestID(currentTestSetTestID)); } - Log.info("Using existing TestSEtID: "+existingTestSetID.toString()); + Log.info("Using existing TestSEtID: "+existingTestSetID); } //Create new instances for all tests in TestSet PractiTestWriter.createAllInstances(this.setID, testIDs); diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/common/RequestFactory.java b/api.v2/java.v2/src/main/java/com/practitest/api/common/RequestFactory.java index 76d5b87..ea28726 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/common/RequestFactory.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/common/RequestFactory.java @@ -1,41 +1,26 @@ package com.practitest.api.common; +import com.Config.GeneralConfig; import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Response; import org.apache.commons.codec.binary.Base64; -import java.io.FileInputStream; -import java.util.Properties; - import static com.jayway.restassured.RestAssured.given; import static com.jayway.restassured.config.SSLConfig.sslConfig; public class RequestFactory { - private static Properties props = new Properties(); - - private static void loadProperties() - { - try{ - props.load(new FileInputStream("src/main/resources/project.properties")); - } - catch (Exception e) - { - e.printStackTrace(); - } - } + private static String URI = GeneralConfig.getConfigurationValue(GeneralConfig.URI); private static byte[] encoding(){ - loadProperties(); - return Base64.encodeBase64((props.getProperty("DEVELOPER_EMAIL") + ":" + props.getProperty("API_TOKEN")).getBytes()); + return Base64.encodeBase64((GeneralConfig.getConfigurationValue(GeneralConfig.DEV_MAIL) + ":" + GeneralConfig.getConfigurationValue(GeneralConfig.API_TOKEN)).getBytes()); } public static Response doGet(String apiEndPoint) { - loadProperties(); System.out.println("================================================================================"); - System.out.println("GET "+props.getProperty("URI")+apiEndPoint); + System.out.println("GET "+ URI+apiEndPoint); System.out.println("================================================================================"); return given() .log().headers() @@ -43,15 +28,14 @@ public static Response doGet(String apiEndPoint) .config(RestAssured.config().sslConfig(sslConfig().relaxedHTTPSValidation())) .header("Content-Type", "application/json") .header("Authorization", "Basic " + new String(encoding())) - .get(props.getProperty("URI")+apiEndPoint) + .get(URI+apiEndPoint) .then().extract().response(); } public static Response doPost(String apiEndPoint, String body) { - loadProperties(); System.out.println("================================================================================"); - System.out.println("POST "+props.getProperty("URI")+apiEndPoint); + System.out.println("POST "+URI+apiEndPoint); System.out.println("================================================================================"); return given() .log().headers() @@ -60,15 +44,14 @@ public static Response doPost(String apiEndPoint, String body) .header("Content-Type", "application/json") .header("Authorization", "Basic " + new String(encoding())) .body(body) - .post(props.getProperty("URI")+apiEndPoint) + .post(URI+apiEndPoint) .then().extract().response(); } public static Response doPost(String apiEndPoint, Object body) { - loadProperties(); System.out.println("================================================================================"); - System.out.println("POST "+props.getProperty("URI")+apiEndPoint); + System.out.println("POST "+URI+apiEndPoint); System.out.println("================================================================================"); return given() .log().headers() @@ -77,15 +60,14 @@ public static Response doPost(String apiEndPoint, Object body) .header("Content-Type", "application/json") .header("Authorization", "Basic " + new String(encoding())) .body(body) - .post(props.getProperty("URI")+apiEndPoint) + .post(URI+apiEndPoint) .then().extract().response(); } public static Response doPut(String apiEndPoint, String body) { - loadProperties(); System.out.println("================================================================================"); - System.out.println("PUT "+props.getProperty("URI")+apiEndPoint); + System.out.println("PUT "+URI+apiEndPoint); System.out.println("================================================================================"); return given() .log().headers() @@ -94,15 +76,14 @@ public static Response doPut(String apiEndPoint, String body) .header("Content-Type", "application/json") .header("Authorization", "Basic " + new String(encoding())) .body(body) - .put(props.getProperty("URI")+apiEndPoint) + .put(URI+apiEndPoint) .then().extract().response(); } public static Response doDelete(String apiEndPoint, String body) { - loadProperties(); System.out.println("================================================================================"); - System.out.println("DELETE "+props.getProperty("URI")+apiEndPoint); + System.out.println("DELETE "+URI+apiEndPoint); System.out.println("================================================================================"); return given() .log().headers() @@ -111,7 +92,7 @@ public static Response doDelete(String apiEndPoint, String body) .header("Content-Type", "application/json") .header("Authorization", "Basic " + new String(encoding())) .body(body) - .delete(props.getProperty("URI")+apiEndPoint) + .delete(URI+apiEndPoint) .then().extract().response(); } } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index ad929cd..8ec4522 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -1,21 +1,24 @@ package com.practitest.api.example; +import com.Config.GeneralConfig; +import com.jayway.restassured.response.Response; import com.practitest.api.common.RequestFactory; import com.practitest.api.model.instance.InstanceModel; import com.practitest.api.model.runs.*; import com.practitest.api.model.sets.Instances; import com.practitest.api.model.sets.SetsModel; -import com.jayway.restassured.response.Response; import java.util.List; public class PractiTestAPI { + + /** * Reads project.properties file for projectID */ - private final static String projectID = System.getProperty("PROJECT_ID"); + private static String projectID = GeneralConfig.getConfigurationValue(GeneralConfig.PROJECT_ID); /** * @@ -24,7 +27,7 @@ public class PractiTestAPI { */ public static Response sendGetSteps(String id) { - return RequestFactory.doGet("/v2/projects/"+projectID+"/steps.json?test-ids=" +id); + return RequestFactory.doGet("/api/v2/projects/"+projectID+"/steps.json?test-ids=" +id); } // // /** @@ -48,7 +51,7 @@ public static Response sendCreateRun(String instanceID, List step) data.setType("instances"); data.setAttributes(new Attributes(instanceID, 0)); data.setSteps(new Steps(step)); - return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + return RequestFactory.doPost("/api/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } /** @@ -61,7 +64,7 @@ public static Response sendCreateRun(String instanceID) Data data = new Data(); data.setType("instances"); data.setAttributes(new Attributes(instanceID, 0)); - return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + return RequestFactory.doPost("/api/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); } /** @@ -74,7 +77,9 @@ public static Response sendSubmitResult(String instanceID, int exitCode) Data data = new Data(); data.setType("instances"); data.setAttributes(new Attributes(instanceID, exitCode)); - return RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", new RunsModel(data)).getBody().jsonPath().get("data.id"); + Response response = RequestFactory.doPost("/api/v2/projects/"+projectID+"/runs.json", new RunsModel(data)); + response.prettyPrint(); + return response; } /** @@ -83,27 +88,29 @@ public static Response sendSubmitResult(String instanceID, int exitCode) */ public static void sendCreateInstance(String body) { - RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); + RequestFactory.doPost("/api/v2/projects/"+projectID+"/instances.json", body).prettyPrint(); } - public static Response sendGetInstances(Integer testSetID) + public static Response sendGetInstances(String testSetID) { - return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?set-ids="+testSetID.toString()); + return RequestFactory.doGet("/api/v2/projects/"+projectID+"/instances.json?set-ids="+testSetID); } public static Response sendGetInstanceBytestID(Integer testID) { - return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?test-ids="+testID.toString()); + return RequestFactory.doGet("/api/v2/projects/"+projectID+"/instances.json?test-ids="+testID.toString()); } - public static Response sendGetInstanceByTestIDAndTestSetID(String testID, Integer testSetID) + public static Response sendGetInstanceByTestIDAndTestSetID(String testID, String testSetID) { - return RequestFactory.doGet("/v2/projects/"+projectID+"/instances.json?test-ids="+testID+"&set-ids="+testSetID.toString()); + Response response = RequestFactory.doGet("/api/v2/projects/"+projectID+"/instances.json?test-ids="+testID+"&set-ids="+testSetID); + response.prettyPrint(); + return response; } public static Response sendRemoveInstance(Integer instanceID) { - return RequestFactory.doDelete("/v2/projects/"+projectID+"/instances/"+instanceID.toString()+".json", ""); + return RequestFactory.doDelete("/api/v2/projects/"+projectID+"/instances/"+instanceID.toString()+".json", ""); } /** @@ -112,12 +119,13 @@ public static Response sendRemoveInstance(Integer instanceID) * @param testID * @return */ - public static Response sendCreateInstance(int setID, int testID) + public static Response sendCreateInstance(String setID, int testID) { com.practitest.api.model.instance.Data data = new com.practitest.api.model.instance.Data(); data.setAttributes(new com.practitest.api.model.instance.Attributes(setID, testID)); - - return RequestFactory.doPost("/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); + Response response = RequestFactory.doPost("/api/v2/projects/"+projectID+"/instances.json", new InstanceModel(data)); + response.prettyPrint(); + return response; } /** @@ -127,7 +135,7 @@ public static Response sendCreateInstance(int setID, int testID) */ public static Response sendCreateTestSet(String body) { - return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", body); + return RequestFactory.doPost("/api/v2/projects/"+projectID+"/sets.json", body); } /** @@ -136,7 +144,7 @@ public static Response sendCreateTestSet(String body) */ public static Response sendGetTestSet() { - return RequestFactory.doGet("/v2/projects/"+projectID+"sets.json"); + return RequestFactory.doGet("/api/v2/projects/"+projectID+"/sets.json"); } /** @@ -145,7 +153,7 @@ public static Response sendGetTestSet() */ public static Response sendGetTestSetByName(String nameExect) { - return RequestFactory.doGet("/v2/projects/"+projectID+"sets.json?name_exact="+nameExect); + return RequestFactory.doGet("/api/v2/projects/"+projectID+"/sets.json?name_like="+nameExect); } /** @@ -159,7 +167,9 @@ public static Response sendCreateTestSet(String name, List testIDs) com.practitest.api.model.sets.Data data = new com.practitest.api.model.sets.Data(); data.setInstances(new Instances(testIDs)); data.setAttributes(new com.practitest.api.model.sets.Attributes(name)); - return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", new SetsModel(data)); + Response response = RequestFactory.doPost("/api/v2/projects/"+projectID+"/sets.json", new SetsModel(data)); + response.prettyPrint(); + return response; } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index e36dc51..ea43a62 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -17,7 +17,7 @@ public class PractiTestWriter { private static String getNameForNewRun() { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(); - String testSuite = System.getProperty("test"); + String testSuite = "Google test"; return testSuite + " " + dateFormat.format(date); } @@ -25,16 +25,17 @@ private static String getNameForNewRun() { * * @return set id for the project */ - public static Integer getSetID(String setName) + public static String getSetID(String setName) { Response response = PractiTestAPI.sendGetTestSetByName(setName); + response.prettyPrint(); try { return response.getBody().jsonPath().get("data.id"); } catch (Exception e) { - return 0; + return null; } } @@ -43,9 +44,10 @@ public static Integer getSetID(String setName) * @param testIDs List of tests which should be assigned to this Test Set * @return new TestSetID */ - public static Integer createNewSet(List testIDs) + public static String createNewSet(List testIDs) { Response response = PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs); + response.prettyPrint(); return PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs).getBody().jsonPath().get("data.id") ; } @@ -55,19 +57,19 @@ public static Integer createNewSet(List testIDs) * @param testID TestCase ID which should be used to create new instance * @return new Instance ID */ - public static Integer createNewInstance(Integer setID, Integer testID) + public static String createNewInstance(String setID, Integer testID) { return PractiTestAPI.sendCreateInstance(setID, testID).getBody().jsonPath().get("data.id"); } - public static void createAllInstances(Integer setID, List testID) + public static void createAllInstances(String setID, List testID) { for (Integer aTestID : testID) { createNewInstance(setID, aTestID); } } - public static List getInstancesByTestSetID(Integer testSet) + public static List getInstancesByTestSetID(String testSet) { return PractiTestAPI.sendGetInstances(testSet).body().jsonPath().get("data.id"); } @@ -77,12 +79,12 @@ public static Integer getInstancesByTestID(Integer testID) return PractiTestAPI.sendGetInstanceBytestID(testID).body().jsonPath().get("data.id"); } - public static String getInstancesByTestIDAndTestSetID(String testID, Integer testSetID) + public static List getInstancesByTestIDAndTestSetID(String testID, String testSetID) { return PractiTestAPI.sendGetInstanceByTestIDAndTestSetID(testID, testSetID).body().jsonPath().get("data.id"); } - public static List getTestIDsForTestSetID(Integer testSet) + public static List getTestIDsForTestSetID(String testSet) { return PractiTestAPI.sendGetInstances(testSet).body().jsonPath().get("data.attributes.test-id"); } @@ -103,7 +105,7 @@ public static Integer submitResults(String instanceID, List stepModel return PractiTestAPI.sendCreateRun(instanceID, stepModel).getBody().jsonPath().get("get.id"); } - public static Integer submitResults(String instanceID, Integer statusCode) + public static String submitResults(String instanceID, Integer statusCode) { return PractiTestAPI.sendSubmitResult(instanceID, statusCode).getBody().jsonPath().get("data.id"); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/model/instance/Attributes.java b/api.v2/java.v2/src/main/java/com/practitest/api/model/instance/Attributes.java index 4a07755..0fe0bba 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/model/instance/Attributes.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/model/instance/Attributes.java @@ -13,7 +13,7 @@ public class Attributes { @JsonProperty("set-id") - private int setId; + private String setId; @JsonProperty("test-id") private int testId; @@ -29,19 +29,19 @@ public Attributes() { * @param testId * @param setId */ - public Attributes(int setId, int testId) { + public Attributes(String setId, int testId) { super(); this.setId = setId; this.testId = testId; } @JsonProperty("set-id") - public int getSetId() { + public String getSetId() { return setId; } @JsonProperty("set-id") - public void setSetId(int setId) { + public void setSetId(String setId) { this.setId = setId; } diff --git a/api.v2/java.v2/src/main/resources/project.properties b/api.v2/java.v2/src/main/resources/project.properties index 3d5a92d..3801326 100644 --- a/api.v2/java.v2/src/main/resources/project.properties +++ b/api.v2/java.v2/src/main/resources/project.properties @@ -1,6 +1,5 @@ URI=URI -DEVELOPER_EMAIL=developer@mail.com +DEVELOPER_EMAIL=MAIL API_TOKEN=TOKEN -PROJECT_ID=project_id -test=googleTest -currentSetId= \ No newline at end of file +PROJECT_ID=PROJECT +test=googleTest \ No newline at end of file diff --git a/api.v2/java.v2/src/test/java/RunWithTests.java b/api.v2/java.v2/src/test/java/RunWithTests.java index bbc0071..1016e70 100644 --- a/api.v2/java.v2/src/test/java/RunWithTests.java +++ b/api.v2/java.v2/src/test/java/RunWithTests.java @@ -35,7 +35,7 @@ public final static void main(String[] args) throws Exception{ PractiTestAPI.sendCreateTestSet("new testing", testIDs); //Create new instance - PractiTestAPI.sendCreateInstance(20803, 84149); + PractiTestAPI.sendCreateInstance("20803", 84149); //Setup chromeDriver ChromeDriverManager.getInstance().setup(); @@ -80,7 +80,7 @@ public final static void main(String[] args) throws Exception{ stepModel.add(step3); //Submit results - PractiTestAPI.sendCreateRun(101263, stepModel); + PractiTestAPI.sendCreateRun("101263", stepModel); } } diff --git a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java index 31e0b2d..806356e 100644 --- a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java +++ b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java @@ -10,37 +10,37 @@ public class SampleTest extends BaseTest{ - @Test(description = "1") + @Test(description = "84140") public void googleTest() { Log.info("Just checking"); } - @Test(description = "2") + @Test(description = "84148") public void googleTest1() { Log.info("Just checking"); } - @Test(description = "3") + @Test(description = "84142") public void googleTest2() { Log.info("Just 4"); } - @Test(description = "5") + @Test(description = "84143") public void googleTest3() { Log.info("Just 6"); } - @Test(description = "7") + @Test(description = "84144") public void googleTest4() { Log.info("Just 8"); } - @Test(description = "9") + @Test(description = "84147") public void googleTest5() { Log.info("Just checking"); From dafa0fc635dc4378ea8b22e9db071ee7fb0656f5 Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Thu, 5 Apr 2018 15:18:48 +0300 Subject: [PATCH 07/10] fixes for set creating. execution by groups --- .../main/java/com/Config/GeneralConfig.java | 2 ++ .../TestNGListenerForPractiTest.java | 8 +++-- .../api/example/PractiTestWriter.java | 29 +++++++------------ .../practitest/integration/ExtractTests.java | 7 +++++ .../src/main/resources/project.properties | 10 +++---- .../test/java/TestNG/tests/SampleTest.java | 15 ++++++---- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java index 16ff649..f762af0 100644 --- a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java +++ b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java @@ -12,6 +12,8 @@ public class GeneralConfig { public static final String DEV_MAIL = "DEVELOPER_EMAIL"; public static final String API_TOKEN = "API_TOKEN"; public static final String PROJECT_ID = "PROJECT_ID"; + public static final String SET_NAME = "groups"; + protected static Properties m_props = new Properties(); diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index e14a9f0..0886426 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -40,13 +40,13 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { - String existingTestSetID = PractiTestWriter.getSetID("bla"); + String existingTestSetID = PractiTestWriter.getSetID(System.getProperty("groups")); //extract all tests for current execution List testIDs = ExtractTests.extractAllTestIds(context); if (existingTestSetID == null) { - //Create test run for all tests in current execution - this.setID = PractiTestWriter.createNewSet(testIDs); + //Create test run for all tests in current execution + this.setID = PractiTestWriter.createNewSet(testIDs); } else { @@ -63,6 +63,8 @@ public void onStart(ITestContext context) { } //Create new instances for all tests in TestSet PractiTestWriter.createAllInstances(this.setID, testIDs); + String current = PractiTestWriter.getSetID("test2"); + Log.info(current); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index ea43a62..f6692a7 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -3,24 +3,10 @@ import com.jayway.restassured.response.Response; import com.practitest.api.model.runs.StepModel; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; public class PractiTestWriter { - /** - * - * @return Generates name for new test run with time stamp - */ - private static String getNameForNewRun() { - DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); - Date date = new Date(); - String testSuite = "Google test"; - return testSuite + " " + dateFormat.format(date); - } - /** * * @return set id for the project @@ -31,7 +17,7 @@ public static String getSetID(String setName) response.prettyPrint(); try { - return response.getBody().jsonPath().get("data.id"); + return response.getBody().jsonPath().get("data.id[0]"); } catch (Exception e) { @@ -46,9 +32,9 @@ public static String getSetID(String setName) */ public static String createNewSet(List testIDs) { - Response response = PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs); + Response response = PractiTestAPI.sendCreateTestSet(System.getProperty("groups"), testIDs); // response.prettyPrint(); - return PractiTestAPI.sendCreateTestSet(getNameForNewRun(), testIDs).getBody().jsonPath().get("data.id") ; + return response.getBody().jsonPath().get("data.id") ; } /** @@ -64,9 +50,14 @@ public static String createNewInstance(String setID, Integer testID) public static void createAllInstances(String setID, List testID) { - for (Integer aTestID : testID) { - createNewInstance(setID, aTestID); + for (int i =0; i getInstancesByTestSetID(String testSet) diff --git a/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java b/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java index 3d206e7..82300d8 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java +++ b/api.v2/java.v2/src/main/java/com/practitest/integration/ExtractTests.java @@ -1,5 +1,7 @@ package com.practitest.integration; +import com.webdriver.example.Utils.Log; +import org.testng.Assert; import org.testng.ITestContext; import org.testng.ITestNGMethod; @@ -13,6 +15,11 @@ public static List extractAllTestIds(ITestContext context) { ITestNGMethod[] testMethods = context.getAllTestMethods(); List testIds = new ArrayList(0); + if (testMethods.length == 0) + { + Log.error("PLEASE PROVIDE VALID GROUP NAME"); + Assert.fail("No test methods found for specified group"); + } for (ITestNGMethod testMethod : testMethods) { testIds.add(testMethod.getDescription()); } diff --git a/api.v2/java.v2/src/main/resources/project.properties b/api.v2/java.v2/src/main/resources/project.properties index 3801326..3465075 100644 --- a/api.v2/java.v2/src/main/resources/project.properties +++ b/api.v2/java.v2/src/main/resources/project.properties @@ -1,5 +1,5 @@ -URI=URI -DEVELOPER_EMAIL=MAIL -API_TOKEN=TOKEN -PROJECT_ID=PROJECT -test=googleTest \ No newline at end of file +URI= +DEVELOPER_EMAIL= +API_TOKEN= +PROJECT_ID= +groups= \ No newline at end of file diff --git a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java index 806356e..5b0ed23 100644 --- a/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java +++ b/api.v2/java.v2/src/test/java/TestNG/tests/SampleTest.java @@ -3,6 +3,7 @@ import TestNG.BaseTest; import com.listeners.TestNGListenerForPractiTest; import com.webdriver.example.Utils.Log; +import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -10,37 +11,39 @@ public class SampleTest extends BaseTest{ - @Test(description = "84140") + @Test(description = "84140", groups = "test") public void googleTest() { Log.info("Just checking"); } - @Test(description = "84148") + @Test(description = "84148", groups = "test2") public void googleTest1() { Log.info("Just checking"); + Assert.fail(); } - @Test(description = "84142") + @Test(description = "84142", groups = "test") public void googleTest2() { Log.info("Just 4"); } - @Test(description = "84143") + @Test(description = "84143", groups = "test2") public void googleTest3() { Log.info("Just 6"); } - @Test(description = "84144") + @Test(description = "84144", groups = "test") public void googleTest4() { Log.info("Just 8"); + Assert.fail(); } - @Test(description = "84147") + @Test(description = "84147", groups = "test2") public void googleTest5() { Log.info("Just checking"); From 789ce45a14d5190d63b516d96b662ff2ded9e27e Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Wed, 18 Apr 2018 14:06:49 +0300 Subject: [PATCH 08/10] validation fix --- .../TestNGListenerForPractiTest.java | 5 ++-- .../practitest/api/example/PractiTestAPI.java | 5 ++++ .../api/example/PractiTestWriter.java | 24 ++++++++++--------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index 0886426..62d9c5c 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -1,5 +1,6 @@ package com.listeners; +import com.Config.GeneralConfig; import com.practitest.api.example.PractiTestWriter; import com.practitest.integration.ExtractTests; import com.webdriver.example.Utils.Log; @@ -40,12 +41,12 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { - String existingTestSetID = PractiTestWriter.getSetID(System.getProperty("groups")); + String existingTestSetID = PractiTestWriter.getSetID(GeneralConfig.getConfigurationValue("groups"));//System.getProperty("groups")); //extract all tests for current execution List testIDs = ExtractTests.extractAllTestIds(context); if (existingTestSetID == null) { - //Create test run for all tests in current execution + Log.info("Creating new test set"); this.setID = PractiTestWriter.createNewSet(testIDs); } else diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index 8ec4522..06e256d 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -8,6 +8,7 @@ import com.practitest.api.model.runs.*; import com.practitest.api.model.sets.Instances; import com.practitest.api.model.sets.SetsModel; +import org.testng.Assert; import java.util.List; @@ -153,6 +154,10 @@ public static Response sendGetTestSet() */ public static Response sendGetTestSetByName(String nameExect) { + if(nameExect == null) + { + Assert.fail("Please provide test set name"); + } return RequestFactory.doGet("/api/v2/projects/"+projectID+"/sets.json?name_like="+nameExect); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index f6692a7..3375eca 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -1,5 +1,6 @@ package com.practitest.api.example; +import com.Config.GeneralConfig; import com.jayway.restassured.response.Response; import com.practitest.api.model.runs.StepModel; @@ -13,16 +14,17 @@ public class PractiTestWriter { */ public static String getSetID(String setName) { - Response response = PractiTestAPI.sendGetTestSetByName(setName); - response.prettyPrint(); - try - { - return response.getBody().jsonPath().get("data.id[0]"); - } - catch (Exception e) - { - return null; - } + + Response response = PractiTestAPI.sendGetTestSetByName(setName); + response.prettyPrint(); + try + { + return response.getBody().jsonPath().get("data.id"); + } + catch (Exception e) + { + return null; + } } /** @@ -32,7 +34,7 @@ public static String getSetID(String setName) */ public static String createNewSet(List testIDs) { - Response response = PractiTestAPI.sendCreateTestSet(System.getProperty("groups"), testIDs); // + Response response = PractiTestAPI.sendCreateTestSet(GeneralConfig.getConfigurationValue("groups"), testIDs);//System.getProperty("groups"), testIDs); // response.prettyPrint(); return response.getBody().jsonPath().get("data.id") ; } From 39f2a7ad383f583f48c109c764a3791fe9c16182 Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Wed, 23 May 2018 00:33:08 +0300 Subject: [PATCH 09/10] Implemented proper usage for config load Added description for add/remove tests from Test Set Failed test will contain throwable message. --- .../main/java/com/Config/GeneralConfig.java | 33 ++++++++++++++++++- .../TestNGListenerForPractiTest.java | 12 ++++--- .../practitest/api/example/PractiTestAPI.java | 18 ++++++++++ .../api/example/PractiTestWriter.java | 5 +++ .../practitest/api/model/runs/Attributes.java | 21 ++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java index f762af0..0f3a8e6 100644 --- a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java +++ b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java @@ -16,21 +16,52 @@ public class GeneralConfig { protected static Properties m_props = new Properties(); + private static boolean m_initialized = false; + // SINGLETON CONSTRUCTOR + GeneralConfig() {} - public static String getConfigurationValue(final String configurationName) { + private static ThreadLocal instanceContainer = new ThreadLocal(){ + @Override + protected GeneralConfig initialValue() { + return new GeneralConfig(); + } + }; + + public static GeneralConfig getInstance() { + return instanceContainer.get(); + } + + + public static boolean isInitialized() { + return m_initialized; + } + public static void loadConfig() + { try{ m_props.load(new FileInputStream(PROPERTIES_PATH)); + m_initialized = true; } catch (Exception e) { e.printStackTrace(); Log.error("Unable to load parameters"); } + } + + + + public static String getConfigurationValue(final String configurationName) { + if(!GeneralConfig.isInitialized()) + throw new IllegalStateException("GeneralConfig is not initialized, please call GeneralConfig.loadConfig()"); + return m_props.getProperty(configurationName); } + /* + Should be used if any parameter should be modified during test execution + */ public static boolean setConfigurationValue(final String configurationName, final String value) { return m_props.setProperty(configurationName, value) != null; } diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index 62d9c5c..a185bc7 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -28,7 +28,8 @@ public void onTestSuccess(ITestResult result) { @Override public void onTestFailure(ITestResult result) { List instanceID = PractiTestWriter.getInstancesByTestIDAndTestSetID(result.getMethod().getDescription(), this.setID); - PractiTestWriter.submitResults(instanceID.get(0), 1); + + PractiTestWriter.submitResults(instanceID.get(0), result.getThrowable().getMessage(), 1); } @Override @@ -41,8 +42,11 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) { @Override public void onStart(ITestContext context) { + //Load configuration file + GeneralConfig.loadConfig(); + //Check if Set ID specified is present in the system String existingTestSetID = PractiTestWriter.getSetID(GeneralConfig.getConfigurationValue("groups"));//System.getProperty("groups")); - //extract all tests for current execution + //extract all automated tests for current execution List testIDs = ExtractTests.extractAllTestIds(context); if (existingTestSetID == null) { @@ -54,9 +58,9 @@ public void onStart(ITestContext context) { this.setID = existingTestSetID; //get test id's for existing TestSet List currentTestSetTestIDs = PractiTestWriter.getTestIDsForTestSetID(this.setID); - //Remove existing test IDs from List to get removed + //Remove existing test IDs from List to get List of removed test cases in Practi test currentTestSetTestIDs.removeAll(testIDs); - //Remove Instance IDs + //Remove Instance IDs which are not present for this set IDs for (Integer currentTestSetTestID : currentTestSetTestIDs) { PractiTestWriter.removeInstance(PractiTestWriter.getInstancesByTestID(currentTestSetTestID)); } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java index 06e256d..14dec7c 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestAPI.java @@ -83,6 +83,24 @@ public static Response sendSubmitResult(String instanceID, int exitCode) return response; } + /** + * + * @param instanceID + * @return + */ + public static Response sendSubmitResult(String instanceID, String error, int exitCode) { + Data data = new Data(); + data.setType("instances"); + Attributes attributes = new Attributes(); + attributes.setExitcode(exitCode); + attributes.setInstanceid(instanceID); + attributes.setAutomatedExecutionOutput(error); + data.setAttributes(attributes); + Response response = RequestFactory.doPost("/api/v2/projects/" + projectID + "/runs.json", new RunsModel(data)); + response.prettyPrint(); + return response; + } + /** * * @param body diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java index 3375eca..fa647ab 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/example/PractiTestWriter.java @@ -103,4 +103,9 @@ public static String submitResults(String instanceID, Integer statusCode) return PractiTestAPI.sendSubmitResult(instanceID, statusCode).getBody().jsonPath().get("data.id"); } + public static String submitResults(String instanceID, String error, Integer statusCode) + { + return PractiTestAPI.sendSubmitResult(instanceID, error, statusCode).getBody().jsonPath().get("data.id"); + } + } diff --git a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java index 0deb0e6..61151b8 100644 --- a/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java +++ b/api.v2/java.v2/src/main/java/com/practitest/api/model/runs/Attributes.java @@ -16,6 +16,10 @@ public class Attributes { + @JsonProperty("automated-execution-output") + private String automatedExecutionOutput; + + @JsonProperty("exit-code") private int exitcode; @@ -38,6 +42,13 @@ public Attributes(String instanceId, int exitcode) { this.exitcode = exitcode; } + public Attributes(String instanceId, String automatedExecutionOutput, int exitcode) { + super(); + this.instanceId = instanceId; + this.automatedExecutionOutput = automatedExecutionOutput; + this.exitcode = exitcode; + } + @JsonProperty("instance-id") public String getInstanceid() { return instanceId; @@ -58,4 +69,14 @@ public void setExitcode(int exitcode) { this.exitcode = exitcode; } + @JsonProperty("automated-execution-output") + public String getAutomatedExecutionOutput() { + return automatedExecutionOutput; + } + + @JsonProperty("automated-execution-output") + public void setAutomatedExecutionOutput(String automatedExecutionOutput) { + this.automatedExecutionOutput = automatedExecutionOutput; + } + } From 25642e2ef304f30e5d3fc1c1838b72cab29a1ddb Mon Sep 17 00:00:00 2001 From: vdemkiv Date: Thu, 14 Jun 2018 01:26:46 +0300 Subject: [PATCH 10/10] updated readme. --- api.v2/java.v2/README.md | 29 +++++++++++++++++++ .../main/java/com/Config/GeneralConfig.java | 1 - .../TestNGListenerForPractiTest.java | 2 +- .../src/main/resources/project.properties | 3 +- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/api.v2/java.v2/README.md b/api.v2/java.v2/README.md index 2fbf0ee..a18977b 100644 --- a/api.v2/java.v2/README.md +++ b/api.v2/java.v2/README.md @@ -3,3 +3,32 @@ Each of PractiTest API methods are any of the following HTTP requests: GET,POST, PUT and DELETE. In the PractiTest API V2 documentation, there are curl examples for each method (with one of the request types). If the curl begins with GET, you need to see the get_request file in your language. The same with the rest of the method types. + +TestNG integration + +Required project properties: + +URI= +DEVELOPER_EMAIL= +API_TOKEN= +PROJECT_ID= + + +Supported browsers: Chrome + +TestNG Class structure for PractiTest integration + +Regular TestNG method which should have description and groups specified. +description == Test ID +groups == Set ID + +groups is used for test cases filtering + +Example: +@Test(description = "TEST_ID", groups = "SET_NAME") + public void googleTest() + + +Test execution using command line + +mvn test -Dgroups= \ No newline at end of file diff --git a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java index 0f3a8e6..428e3df 100644 --- a/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java +++ b/api.v2/java.v2/src/main/java/com/Config/GeneralConfig.java @@ -12,7 +12,6 @@ public class GeneralConfig { public static final String DEV_MAIL = "DEVELOPER_EMAIL"; public static final String API_TOKEN = "API_TOKEN"; public static final String PROJECT_ID = "PROJECT_ID"; - public static final String SET_NAME = "groups"; protected static Properties m_props = new Properties(); diff --git a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java index a185bc7..232ba41 100644 --- a/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java +++ b/api.v2/java.v2/src/main/java/com/listeners/TestNGListenerForPractiTest.java @@ -45,7 +45,7 @@ public void onStart(ITestContext context) { //Load configuration file GeneralConfig.loadConfig(); //Check if Set ID specified is present in the system - String existingTestSetID = PractiTestWriter.getSetID(GeneralConfig.getConfigurationValue("groups"));//System.getProperty("groups")); + String existingTestSetID = PractiTestWriter.getSetID(System.getProperty("groups")); //extract all automated tests for current execution List testIDs = ExtractTests.extractAllTestIds(context); if (existingTestSetID == null) diff --git a/api.v2/java.v2/src/main/resources/project.properties b/api.v2/java.v2/src/main/resources/project.properties index 3465075..95bf559 100644 --- a/api.v2/java.v2/src/main/resources/project.properties +++ b/api.v2/java.v2/src/main/resources/project.properties @@ -1,5 +1,4 @@ URI= DEVELOPER_EMAIL= API_TOKEN= -PROJECT_ID= -groups= \ No newline at end of file +PROJECT_ID= \ No newline at end of file