Last active
March 23, 2020 15:21
-
-
Save francislainy/21d39beb5706b671347ca1c8f28856c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment