下面列出了org.testng.annotations.BeforeSuite#org.testng.ITestResult 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Init only once per configuration.
*
* @return Instance of MobileSetupManager.
*/
public static WebSetupManager init() {
WebSetupManager webSetupManager;
if (WebSetupManager.initSettings == null) {
WebSetupManager.initSettings = new HashMap<>();
}
if (!WebSetupManager.initSettings.containsKey(Settings.getAppConfig())) {
webSetupManager = new WebSetupManager();
webSetupManager.context.settings = new WebSettings();
webSetupManager.driver = new ChromeDriver(new ChromeDriverOptions().loadChromeDriverOptions(webSetupManager.context.settings));
webSetupManager.context.driver = webSetupManager.driver;
webSetupManager.context.log = new Log();
webSetupManager.context.locators = new Locators();
webSetupManager.context.lastTestResult = ITestResult.SUCCESS;
webSetupManager.context.find = new Find(webSetupManager.driver);
WebSetupManager.initSettings.put(getAppConfig(), webSetupManager);
} else {
webSetupManager = WebSetupManager.initSettings.get(getAppConfig());
}
return webSetupManager;
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.getTestMethod().isBeforeClassConfiguration()
| method.getTestMethod().isBeforeGroupsConfiguration()
| method.getTestMethod().isBeforeMethodConfiguration()
| method.getTestMethod().isBeforeSuiteConfiguration()
| method.getTestMethod().isBeforeTestConfiguration()) {
afterSetup(method, testResult);
} else if (method.getTestMethod().isAfterClassConfiguration()
| method.getTestMethod().isAfterGroupsConfiguration()
| method.getTestMethod().isAfterMethodConfiguration()
| method.getTestMethod().isAfterSuiteConfiguration()
| method.getTestMethod().isAfterTestConfiguration()) {
afterTeardown(method, testResult);
}
}
private void onTestFinished(ITestResult iTestResult, TestResult.ResultType resultType) {
Object testId;
TestStartEvent startEvent = null;
synchronized (lock) {
testId = tests.remove(iTestResult);
if (testId == null) {
// This can happen when a method fails which this method depends on
testId = idGenerator.generateId();
Object parentId = testMethodToSuiteMapping.get(iTestResult.getMethod());
startEvent = new TestStartEvent(iTestResult.getStartMillis(), parentId);
}
}
if (startEvent != null) {
// Synthesize a start event
resultProcessor.started(new DefaultTestMethodDescriptor(testId, iTestResult.getTestClass().getName(), iTestResult.getName()), startEvent);
}
if (resultType == TestResult.ResultType.FAILURE) {
resultProcessor.failure(testId, iTestResult.getThrowable());
}
resultProcessor.completed(testId, new TestCompleteEvent(iTestResult.getEndMillis(), resultType));
}
@Override
public boolean retry(ITestResult iTestResult) {
int maxRetryCount;
RetryCount annotation = iTestResult.getMethod().getConstructorOrMethod().getMethod()
.getAnnotation(RetryCount.class);
if (annotation != null) {
maxRetryCount = annotation.maxRetryCount();
} else {
try {
maxRetryCount = MAX_RETRY_COUNT.getInt();
} catch (Exception e) {
maxRetryCount = 0;
}
}
return !iTestResult.isSuccess() && COUNTER.incrementAndGet() < maxRetryCount;
}
/**
* get Test Method name
*
* @param result ITestResult
* @return String method name
*/
public static String getMethodName(ITestResult result) {
// adjust testName using pattern
ITestNGMethod m = result.getMethod();
String name = Configuration.get(Configuration.Parameter.TEST_NAMING_PATTERN);
name = name.replace(SpecialKeywords.METHOD_NAME, m.getMethodName());
name = name.replace(SpecialKeywords.METHOD_PRIORITY, String.valueOf(m.getPriority()));
name = name.replace(SpecialKeywords.METHOD_THREAD_POOL_SIZE, String.valueOf(m.getThreadPoolSize()));
if (m.getDescription() != null) {
name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, m.getDescription());
} else {
name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, "");
}
return name;
}
private void logUrl(ITestResult tr) {
TestContext testContext;
Object[] parameters = tr.getParameters();
if (parameters == null || parameters.length == 0) {
return;
}
try {
testContext = (TestContext) parameters[0];
} catch (ClassCastException e) {
return;
}
Iterable<Searchable> searchables = Iterables.filter(testContext.getResources().values(), Searchable.class);
List<Searchable> listOfSearchables = StreamSupport.stream(searchables.spliterator(), false).collect(Collectors.toList());
if (listOfSearchables.size() == 0) {
return;
}
SearchUrl searchUrl = new KibanaSearchUrl();
tr.getTestContext().setAttribute(tr.getName() + SEARCH_URL,
searchUrl.getSearchUrl(listOfSearchables, new Date(tr.getStartMillis()), new Date(tr.getEndMillis())));
}
protected String getFailureReason(ITestResult result) {
String errorMessage = "";
String message = "";
if (result.getThrowable() != null) {
Throwable thr = result.getThrowable();
errorMessage = getFullStackTrace(thr);
message = thr.getMessage();
result.getTestContext().setAttribute(SpecialKeywords.TEST_FAILURE_MESSAGE, message);
}
// handle in case of failed config (exclusion of expected skip)
if (errorMessage.isEmpty()) {
String methodName;
Collection<ITestResult> results = result.getTestContext().getSkippedConfigurations().getAllResults();
for (ITestResult resultItem : results) {
methodName = resultItem.getMethod().getMethodName();
if (methodName.equals(SpecialKeywords.BEFORE_TEST_METHOD)) {
errorMessage = getFullStackTrace(resultItem.getThrowable());
}
}
}
return errorMessage;
}
private List<ITestResult> doWrap(final List<ITestResult> raw) {
final List<ITestResult> wrapped = new ArrayList<>(raw.size());
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
for (final ITestResult result : raw) {
wrapped.add(ITestResult.class.cast(
Proxy.newProxyInstance(loader, API,
new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (method.getName().equals("getParameters")) {
return new Object[method.getParameterTypes().length];
}
return method.invoke(result, args);
}
})
));
}
return wrapped;
}
/**
* This method is useful to print the output console log in case of test failed.
* We are assuming that we put in the context an attribute whose name is the complete test case ID (example: TEST_SUITE.001) and whose value is
* 'PASSED' or 'SKIPPED' or 'FAILED'.
* @param tr test case result - testNG handling
*/
@Override
public void onTestFailure(ITestResult tr) {
if (tr.getParameters().length > 0) {
Map<String, String> paramMap = (HashMap<String, String>) tr.getParameters()[0];
ITestContext testContext = tr.getTestContext();
//testCaseCompleteID - Example: TEST_SUITE.001
String testCaseCompleteID = testContext.getName() + TestBaseRunner.TESTCASE_ID_SEPARATOR + testContext.getAttribute(TestBaseRunner.ATTR_TESTCASE_ID);
logger.error("[{}][{}][{}] -- FAILED", testCaseCompleteID,
testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());
if (testContext.getAttribute(FAILED_TEST_CASES) == null) {
failedTc = new ArrayList();
} else {
failedTc = (List<ITestResult>) testContext.getAttribute(FAILED_TEST_CASES);
}
failedTc.add(tr);
testContext.setAttribute(FAILED_TEST_CASES, failedTc);
} else {
super.onTestFailure(tr);
}
}
@Override
public void onTestSkipped(ITestResult result) {
ReportiumClient client = getReportClient();
if (null != client) {
// By default all the skipped tests will be failed, if you want
if (ConfigurationManager.getBundle().getString("skippedTests", "fail").equalsIgnoreCase("pass")) {
client.testStop(TestResultFactory.createSuccess());
} else {
String failureMessage = result.getThrowable().getMessage();
failureMessage = (failureMessage.isEmpty() || failureMessage == null) ? "This test was skipped"
: result.getThrowable().getMessage();
client.testStop(TestResultFactory.createFailure(failureMessage, result.getThrowable()));
}
logTestEnd(result);
}
}
@Override
public void onConfigurationFailure(ITestResult iTestResult) {
if (isSuppressConfigEvent(iTestResult)) {
return;
}
String suiteUid = getSuiteUid(iTestResult.getTestContext());
if (isAfterSuiteConfigMethod(iTestResult)) {
String suiteTitle = getCurrentSuiteTitle(iTestResult.getTestContext());
getLifecycle().fire(new TestSuiteStartedEvent(suiteUid, suiteTitle).withTitle(suiteTitle));
}
Throwable throwable = iTestResult.getThrowable();
createConfigEvent(iTestResult);
getLifecycle().fire(new TestCaseFailureEvent().withThrowable(throwable));
fireFinishTest();
if (isAfterSuiteConfigMethod(iTestResult)) {
getLifecycle().fire(new TestSuiteFinishedEvent(suiteUid));
}
}
/**
* getTestDescription method
*
* @param tr
* @return String
*/
public String getTestDescription(ITestResult tr) {
String message = "";
try {
if ( tr.getParameters().length > 0 ) {
message = ": " + tr.getParameters()[1].toString();
}
}
catch(Exception e) {
// do nothing...
}
return message;
}
@Override
public void run(IHookCallBack hookCallBack, ITestResult testResult) {
TestExecutionContext testExecutionContext = buildTestExecutionContext(testResult);
if(testExecutionContext.isQuickPerfDisabled()) {
hookCallBack.runTestMethod(testResult);
return;
}
if(SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
executeTestMethodInNewJvmAndRecordPerformance(testResult, testExecutionContext);
return;
}
JvmOrTestIssue jvmOrTestIssue =
executeTestMethodAndRecordPerformance(hookCallBack, testResult, testExecutionContext);
SetOfAnnotationConfigs testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
Collection<PerfIssuesToFormat> groupOfPerfIssuesToFormat = perfIssuesEvaluator.evaluatePerfIssuesIfNoJvmIssue(testAnnotationConfigs
, testExecutionContext
, jvmOrTestIssue);
testExecutionContext.cleanResources();
try {
quickPerfReporter.report(jvmOrTestIssue, groupOfPerfIssuesToFormat, testExecutionContext);
} catch (Throwable throwable) {
testResult.setThrowable(throwable);
testResult.setStatus(ITestResult.FAILURE);
}
}
@Override
public void onTestFailure(ITestResult result) {
if (videoDisabled(result) || !shouldIntercept(result)) {
return;
}
String fileName = getFileName(result);
File file = stopRecording(fileName);
doVideoProcessing(false, file);
}
private void deleteSuccessVideos(ITestResult result, String className) {
if (result.isSuccess()
&& (null != System.getenv("KEEP_ALL_VIDEOS"))
&& !(System.getenv("KEEP_ALL_VIDEOS").equalsIgnoreCase("true"))) {
File videoFile = new File(System.getProperty("user.dir")
+ FileLocations.ANDROID_SCREENSHOTS_DIRECTORY
+ AppiumDeviceManager.getAppiumDevice().getDevice().getUdid() + "/"
+ className + "/" + result.getMethod().getMethodName()
+ "/" + result.getMethod().getMethodName() + ".mp4");
if (videoFile.exists()) {
videoFile.delete();
}
}
}
@Test
@TestPriority(Priority.P2)
@TestTag(name = TAG_NAME2, value = TAG_VALUE2)
@TestTag(name = TAG_NAME, value = TAG_VALUE)
@TestTag(name = FORBIDDEN_KEY_PRIORITY, value = "P0")
public void testZafiraGetTagsMethod() {
ITestResult result = Reporter.getCurrentTestResult();
Map<String, String> tags = TagManager.getTags(result);
String priority = PriorityManager.getPriority(result);
Assert.assertEquals(priority, "P2");
Assert.assertFalse(tags.containsKey(FORBIDDEN_KEY_PRIORITY));
Assert.assertTrue(tags.containsKey(TAG_NAME));
Assert.assertEquals(tags.get(TAG_NAME), TAG_VALUE);
Assert.assertTrue(tags.containsKey(TAG_NAME2));
Assert.assertEquals(tags.get(TAG_NAME2), TAG_VALUE2);
Assert.assertEquals(tags.size(), 2);
Set<TagType> tagsTypes = getTestTags(result);
Assert.assertEquals(tagsTypes.size(), 3);
for (TagType entry : tagsTypes) {
if (entry.getName().equals(SpecialKeywords.TEST_PRIORITY_KEY)) {
Assert.assertEquals(entry.getValue(), "P2");
}
}
tagsTypes.stream().forEachOrdered((entry) -> {
Object currentKey = entry.getName();
Object currentValue = entry.getValue();
LOGGER.info(currentKey + "=" + currentValue);
});
}
private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {
ITestNGMethod[] result = new ITestNGMethod[results.size()];
int i = 0;
for (ITestResult tr : results) {
result[i++] = tr.getMethod();
}
return result;
}
public static void setSeleniumLog(ITestResult tr, String log) {
String id = StringUtil.createRandomString("sLog");
Reporter.log("<a href=\"javascript:toggleElement('" + id + "', 'block')\">View SeleniumLog</a>" + "\n");
Reporter.log("<div id=\"" + id
+ "\" style=\"display:none;position:absolute;overflow: auto; opacity: 0.95;filter:alpha(opacity=95);background-color:#eeeeee;height:300px;z-lineNo: 9002;border:1px outset;\">"
+ log + "</div><br>");
}
@AfterMethod
public void printError(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
String clsName = result.getTestClass().getName();
clsName = clsName.substring(clsName.lastIndexOf(".") + 1);
System.out.println("Test " + clsName + "." +
result.getName() + " FAILED");
}
}
@Override
public void onTestSkipped(ITestResult result) {
LOGGER.debug("CarinaListener->onTestSkipped");
String errorMessage = getFailureReason(result);
takeScreenshot(result, "TEST FAILED - " + errorMessage);
super.onTestSkipped(result);
onTestFinish(result);
}
@Test(dependsOnMethods="testDefaultMethodOwner")
@MethodOwner(owner = ANDROID_OWNER, platform = SpecialKeywords.ANDROID)
@MethodOwner(owner = DEFAULT_OWNER)
@MethodOwner(owner = IOS_OWNER, platform = SpecialKeywords.IOS)
public void testAndroidMethodOwner() {
R.CONFIG.put(SpecialKeywords.PLATFORM, "android");
ITestResult result = Reporter.getCurrentTestResult();
String ownerName = Ownership.getMethodOwner(result);
Assert.assertEquals(ownerName, ANDROID_OWNER);
}
private static int getFailures(List<ITestResult> results) {
int retval = 0;
for (ITestResult result : results) {
if (result != null && result.getStatus() == ITestResult.FAILURE)
retval++;
}
return retval;
}
/**
* Delegates to the {@link IHookCallBack#runTestMethod(ITestResult) test
* method} in the supplied {@code callback} to execute the actual test
* and then tracks the exception thrown during test execution, if any.
*
* @see org.testng.IHookable#run(org.testng.IHookCallBack,
* org.testng.ITestResult)
*/
@Override
public void run(IHookCallBack callBack, ITestResult testResult) {
callBack.runTestMethod(testResult);
Throwable testResultException = testResult.getThrowable();
if (testResultException instanceof InvocationTargetException) {
testResultException = ((InvocationTargetException) testResultException).getCause();
}
this.testException = testResultException;
}
private void endTestcaseWithSkipStatus( ITestResult testResult, ITestContext context ) {
//Check if the test was successfully started, if not - make it started and then end it with failure
String testName = testResult.getMethod().toString();
if (!testName.equals(currentTestcaseName)) {
startTestcase(testResult);
}
sendTestEndEventToAgents();
if (configurationError(context)) {
// test is skipped due to configuration error
logger.info(MSG__TEST_SKIPPED_CONFIGURATION, testResult.getThrowable());
} else if (dependencyError(testResult, context)) {
// test is skipped due to dependency error
logger.info(MSG__TEST_SKIPPED_DEPENDENCY, testResult.getThrowable());
} else {
// we do not know the exact problem
logger.fatal(MSG__TEST_SKIPPED_UNRECOGNIZED_REASON, testResult.getThrowable());
}
currentTestcaseName = null;
lastTestcaseResult = TestCaseResult.SKIPPED.toInt();
// end test case
logger.endTestcase(TestCaseResult.SKIPPED);
}
@AfterMethod
public void after(ITestResult result) {
if (!result.isSuccess()) {
List<Object> list = new ArrayList<>();
Collections.addAll(list, result.getParameters());
list.add(context.toString());
result.setParameters(list.toArray(new Object[list.size()]));
}
}
@AfterMethod
public void after(ITestResult result) {
if (!result.isSuccess()) {
List<Object> list = new ArrayList<>();
Collections.addAll(list, result.getParameters());
list.add(context.toString());
result.setParameters(list.toArray(new Object[list.size()]));
}
}
/**
* {@inheritDoc}
*/
@Override
public byte[] getArtifact(final ITestResult result) {
// ensure current test result is set
Reporter.setCurrentTestResult(result);
Optional<WebDriver> optDriver = DriverManager.nabDriver(result.getInstance());
return ScreenshotUtils.getArtifact(optDriver, result.getThrowable(), LOGGER);
}
@AfterMethod
public void printCaseError(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
Hierarchy hs = (Hierarchy)result.getParameters()[0];
System.out.println("Separate compilation case " + hs);
printCaseDetails(hs);
}
}
@Test
@MethodOwner(owner = DEFAULT_OWNER)
@MethodOwner(owner = ANDROID_OWNER, platform = SpecialKeywords.ANDROID)
@MethodOwner(owner = IOS_OWNER, platform = SpecialKeywords.IOS)
public void testDefaultMethodOwner() {
ITestResult result = Reporter.getCurrentTestResult();
String ownerName = Ownership.getMethodOwner(result);
Assert.assertEquals(ownerName, DEFAULT_OWNER);
}
@AfterMethod
public void tearDown(ITestResult result) {
try {
if (runType.equals(RunType.SAUCE)) {
((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
}
}
finally {
webDriver.get().quit();
}
}