Skip to content

Instantly share code, notes, and snippets.

@francislainy
Last active June 14, 2024 18:15
Show Gist options
  • Select an option

  • Save francislainy/21d39beb5706b671347ca1c8f28856c0 to your computer and use it in GitHub Desktop.

Select an option

Save francislainy/21d39beb5706b671347ca1c8f28856c0 to your computer and use it in GitHub Desktop.
package selenium_demo.hooks;
import selenium_demo.test_rail.APIClient;
import selenium_demo.test_rail.APIException;
import selenium_demo.DriverFactory;
import static selenium_demo.test_rail.TestRailAccount.testRailApiClient;
import static selenium_demo.utils.Util.getProperties;
import cucumber.api.Result;
import io.cucumber.core.api.Scenario;
import io.cucumber.java.Before;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import io.cucumber.java.After;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openqa.selenium.WebDriver;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Hooks {
private static APIClient client = null;
private static String runId = getProperties().getProperty("runIdTestRail" );
private static final int FAIL_STATE = 5;
private static final int SUCCESS_STATE = 1;
private static final String SUCCESS_COMMENT = "This test passed with Selenium";
private static final String FAILED_COMMENT = "This test failed with Selenium";
@Rule
public TestName testName = new TestName();
public static WebDriver driver;
@Before
public void initializeTest() {
driver = DriverFactory.startDriver();
}
@After()
public void tearDown(Scenario scenario) {
driver.close();
driver.quit();
logResultToTestRail(scenario);
}
private void logResultToTestRail(Scenario scenario) {
String caseId = "";
System.out.println(scenario.getSourceTagNames());
for (String s : scenario.getSourceTagNames()) {
if (s.contains("TestRail" )) {
String[] res = s.split("(\\(.*?)" );
caseId = res[1].substring(0, res[1].length() - 1); // Removing the last parenthesis
}
}
Map<String, Serializable> data = new HashMap<>();
if (!scenario.isFailed()) {
data.put("status_id", SUCCESS_STATE);
data.put("comment", SUCCESS_COMMENT);
} else {
data.put("status_id", FAIL_STATE);
data.put("comment", logError(scenario));
}
if (!caseId.equals("" )) {
try {
if (System.getenv("runIdTestRail" ) != null && System.getenv("runTestRailId" ).equals("" )) {
runId = System.getenv("runIdTestRail" );
}
client = testRailApiClient();
client.sendPost("add_result_for_case/" + runId + "/" + caseId, data);
} catch (IOException | APIException e) {
e.printStackTrace();
}
}
}
// As per https://stackoverflow.com/a/58506614/6654475
private static String logError(Scenario scenario) {
try {
Class clasz = ClassUtils.getClass("cucumber.runtime.java.JavaHookDefinition$ScenarioAdaptor" );
Field fieldScenario = FieldUtils.getField(clasz, "scenario", true);
if (fieldScenario != null) {
fieldScenario.setAccessible(true);
Object objectScenario = fieldScenario.get(scenario);
Field fieldStepResults = objectScenario.getClass().getDeclaredField("stepResults" );
fieldStepResults.setAccessible(true);
ArrayList<Result> results = (ArrayList<Result>) fieldStepResults.get(objectScenario);
for (Result result : results) {
if (result.getError() != null) {
return FAILED_COMMENT + "\n" + result.getErrorMessage();
}
}
}
return FAILED_COMMENT;
} catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
return FAILED_COMMENT;
}
}
}
@Ramitmoni
Copy link

I am not able to get the exception that was thrown when a Cucumber test failed in Java using the above method logError

@francislainy
Copy link
Author

Sorry about that, but I worked on this code over four years ago and wouldn't be able to assist you try to find why it's not working for you. Maybe AI such as Chat GPT or stack overflow may be a good place to get help on it, or perhaps TestRail's api documentation.

I am not able to get the exception that was thrown when a Cucumber test failed in Java using the above method logError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment