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
6 changes: 4 additions & 2 deletions api.v2/java.v2/JAVA_INTEGRATION_SAMPLE.iml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
<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.8.1" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.7.5" level="project" />
<orderEntry type="library" name="Maven: org.seleniumhq.selenium:selenium-support:3.9.1" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.7.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.9.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.13.0" 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" />
<orderEntry type="library" name="Maven: xalan:serializer:2.7.2" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,17 +11,21 @@

public class TestNGListenerForPractiTest implements ITestListener {

protected Integer instanceID = null;
Copy link
Member

Choose a reason for hiding this comment

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

Why just one instanceID? There should be one instance for every test in the test set



@Override
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
Expand All @@ -33,12 +38,20 @@ 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);
Integer existingTestID = PractiTestWriter.getSetID();
if (existingTestID == 0)
{
List<Integer> 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());
Copy link
Member

Choose a reason for hiding this comment

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

If the testset exists (and going to be reused), you still have to synchronize the test in that testset. For example, a new test might be added, or some test might be removed. You should retrieve the current list of instances for the existing testset, add/remove instances as needed, and then create the runs and upload results

}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,125 @@

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("com/v2/projects/"+projectID+"/steps.json?test-ids=" +id);
return RequestFactory.doGet("/v2/projects/"+projectID+"/steps.json?test-ids=" +id);
}

/**
*
* @param body
*/
public static void sendCreateRun(String body)
{
RequestFactory.doPost("com/v2/projects/"+projectID+"/runs.json", body);
RequestFactory.doPost("/v2/projects/"+projectID+"/runs.json", body);
}

/**
*
* @param instanceID
* @param step
* @return
*/
public static Response sendCreateRun(int instanceID, List<StepModel> 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("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");
}

/**
*
* @param instanceID
* @return
*/
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");
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("com/v2/projects/"+projectID+"/instances.json", body).prettyPrint();
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();
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));
}

/**
*
* @param body
* @return
*/
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);
}

/**
*
* @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<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));
return RequestFactory.doPost("com/v2/projects/"+projectID+"/sets.json", new SetsModel(data));
return RequestFactory.doPost("/v2/projects/"+projectID+"/sets.json", new SetsModel(data));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,75 @@

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 = System.getProperty("test");
return testSuite + " " + dateFormat.format(date);
}

/**
*
* @return set id for the project
*/
public static Integer getSetID()
{
Response response = PractiTestAPI.sendGetTestSet();
Copy link
Member

Choose a reason for hiding this comment

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

The PractiTestAPI.sendGetTestSet() returns all testsets in the given project, right? You need to use name_exact parameter to return specific testset (see https://www.practitest.com/api-v2/#get-all-testsets-in-your-project)

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<Integer> 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<Integer> testID)
{
return PractiTestAPI.sendCreateInstance(setID, testID.get(0)).getBody().jsonPath().get("data.id");
Copy link
Member

Choose a reason for hiding this comment

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

Aren't you creating instance just for first test here?

}

/**
*
* @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> 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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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")
Expand All @@ -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;
}

}