Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestNG Practitest integration implementation #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down Expand Up @@ -32,7 +32,6 @@
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-api:3.3.1" level="project" />
<orderEntry type="library" name="Maven: cglib:cglib-nodep:3.2.4" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-exec:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" name="Maven: net.java.dev.jna:jna-platform:4.1.0" level="project" />
<orderEntry type="library" name="Maven: net.java.dev.jna:jna:4.1.0" level="project" />
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-edge-driver:3.3.1" level="project" />
Expand All @@ -42,7 +41,7 @@
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-safari-driver:3.3.1" level="project" />
<orderEntry type="library" name="Maven: com.codeborne:phantomjsdriver:1.4.0" level="project" />
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:htmlunit-driver:2.24" level="project" />
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-support:3.7.1" level="project" />
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-support:3.8.1" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.7.5" level="project" />
<orderEntry type="library" name="Maven: net.sourceforge.htmlunit:htmlunit:2.24" level="project" />
<orderEntry type="library" name="Maven: xalan:xalan:2.7.2" level="project" />
Expand All @@ -69,5 +68,19 @@
<orderEntry type="library" name="Maven: org.jsoup:jsoup:1.10.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:cucumber-java8:1.2.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:cucumber-java:1.2.5" level="project" />
<orderEntry type="library" name="Maven: info.cukes:cucumber-testng:1.2.5" level="project" />
<orderEntry type="library" name="Maven: info.cukes:cucumber-core:1.2.5" level="project" />
<orderEntry type="library" name="Maven: info.cukes:cucumber-html:0.2.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: info.cukes:cucumber-picocontainer:1.2.5" level="project" />
<orderEntry type="library" name="Maven: info.cukes:cucumber-jvm-deps:1.0.5" level="project" />
<orderEntry type="library" name="Maven: info.cukes:gherkin:2.12.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.picocontainer:picocontainer:2.15" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="Maven: org.testng:testng:6.11" level="project" />
<orderEntry type="library" name="Maven: com.beust:jcommander:1.64" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
</component>
</module>
51 changes: 51 additions & 0 deletions api.v2/java.v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>JAVA_INTEGRATION_SAMPLE</groupId>
<artifactId>JAVA_INTEGRATION_SAMPLE</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>


<dependencies>
Expand All @@ -30,6 +42,45 @@
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.listeners;

public class CucumberJVMListenerForPractiTest {

//TODO: implement Cucumber Listener
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.listeners;

public class JUnitListenerForPractiTest {

//TODO: implement Junit Listener
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure i understand the logic here. Are you going to create a new TestSet every time 'mvn test' runs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand TestSet this is collection of tests which should be executed.
For example if you specify certain test for execution you'll need new TestSet in Practitest to store results for it. Correct me if I'm wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You right, TestSet is a collection of Tests. But subsequent executions are supposed to reuse existing TestSet.
Meaning that when you run your project first time, we should create the Tests and TestSet. When you run the project next time, we should use the TestSet created during first run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time automated and manual tests are grouped to Smoke and Regression test suites or by some other project specific definition.
This means we should have multiple TestSets for them. Including test automation process into consideration we will have only some part automated at the start and number of automated != manual test cases. From this perspective it seam to me logical to create new TestSet each time as collection of tests most of the time will be different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add validation to reuse test set if collection of tests already exist for this group. Please let me know if this will work for you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we should reuse testsets. If tests are added or removed, we should change the existing testset.

}


@Override
public void onFinish(ITestContext context) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the URL starts with 'com/'?

}

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<StepModel> step)
public static Response sendCreateRun(int instanceID, List<StepModel> 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<Integer> testIDs)
public static Response sendCreateTestSet(String name, List<Integer> 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));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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> stepModel)
{
return PractiTestAPI.sendCreateRun(instanceID, stepModel).getBody().jsonPath().get("get.id");

}

}
Original file line number Diff line number Diff line change
@@ -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<Integer> extractAllTestIds(ITestContext context)
{
ITestNGMethod[] testMethods = context.getAllTestMethods();
List<String> testIds = new ArrayList(0);
for (ITestNGMethod testMethod : testMethods) {
testIds.add(testMethod.getDescription());
}
return testIds.stream().map(Integer::parseInt).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -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<WebDriver> 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);
}
}
Loading