Java Code Examples for org.testng.ITestContext#setAttribute()

The following examples show how to use org.testng.ITestContext#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlatformInterceptor.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Assemble a list of methods that support the current target platform
 *
 * @param methods list of methods that are about the be run
 * @param context test context
 */
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> result = new ArrayList<>();
    String contextPlatform = getContextPlatform(context);

    // iterate over method list
    for (IMethodInstance thisMethod : methods) {
        Object platformConstant = resolveTargetPlatform(thisMethod);
        
        // if this method supports the current target platform
        if (TargetPlatformHandler.shouldRun(contextPlatform, (PlatformEnum) platformConstant)) {
            addMethodForPlatform(result, thisMethod, (PlatformEnum) platformConstant);
        }
    }

    if (result.isEmpty()) {
        logger.warn("No tests were found for context platform '{}'", contextPlatform);
    }

    // indicate intercept has been invoked
    context.setAttribute(INTERCEPT, Boolean.TRUE);
    return result;
}
 
Example 2
Source File: EverrestJetty.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
public void onStart(ITestContext context) {

        ITestNGMethod[] allTestMethods = context.getAllTestMethods();
        if (allTestMethods == null) {
            return;
        }
        if (httpServer == null && hasEverrestJettyListenerTestHierarchy(allTestMethods)) {
            httpServer = new JettyHttpServer();

            context.setAttribute(JETTY_PORT, httpServer.getPort());
            context.setAttribute(JETTY_SERVER, httpServer);

            try {
                httpServer.start();
                httpServer.resetFactories();
                httpServer.resetFilter();
                RestAssured.port = httpServer.getPort();
                RestAssured.basePath = JettyHttpServer.UNSECURE_REST;
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
                throw new RuntimeException(e.getLocalizedMessage(), e);
            }
        }
    }
 
Example 3
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 6 votes vote down vote up
/**
 * Suppress duplicated configuration method events
 */
@SuppressWarnings("unchecked")
private synchronized boolean isSuppressConfigEvent(ITestResult iTestResult) {
    Set<String> methodNames;
    ITestContext context = iTestResult.getTestContext();
    String configType = getConfigMethodType(iTestResult).getName();
    if (context.getAttribute(configType) == null) {
        methodNames = new HashSet<>();
        methodNames.add(iTestResult.getName());
        context.setAttribute(configType, methodNames);
        return false;
    } else {
        methodNames = (Set<String>) context.getAttribute(configType);
        if (!methodNames.contains(iTestResult.getName())) {
            methodNames.add(iTestResult.getName());
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: TestCaseTestListener.java    From agent with MIT License 5 votes vote down vote up
/**
 * 每个device开始测试调用的方法,这里可能有多个线程同时调用
 *
 * @param testContext
 */
@Override
public void onStart(ITestContext testContext) {
    TestDescription testDesc = new TestDescription(testContext.getAllTestMethods()[0].getDescription());
    log.info("[{}]onStart, deviceTestTaskId:{}, recordVideo: {}", testDesc.getDeviceId(), testDesc.getDeviceTestTaskId(), testDesc.getRecordVideo());
    testContext.setAttribute(TEST_DESCRIPTION, testDesc);

    DeviceTestTask deviceTestTask = new DeviceTestTask();
    deviceTestTask.setId(testDesc.getDeviceTestTaskId());
    deviceTestTask.setStartTime(new Date());
    deviceTestTask.setStatus(DeviceTestTask.RUNNING_STATUS);

    ServerClient.getInstance().updateDeviceTestTask(deviceTestTask);
}
 
Example 5
Source File: CustomTestNgListener.java    From heat with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source File: CustomTestNgListener.java    From heat with Apache License 2.0 5 votes vote down vote up
/**
 * This method is useful to print the output console log in case of test success or test skipped.
 * 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 onTestSuccess(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);
        if (testContext.getAttributeNames().contains(testCaseCompleteID)
                && TestBaseRunner.STATUS_SKIPPED.equals(testContext.getAttribute(testCaseCompleteID))) {
            logger.info("[{}][{}][{}] -- SKIPPED", testCaseCompleteID,
                    testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
                    testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());

            if (testContext.getAttribute(SKIPPED_TEST_CASES) == null) {
                skippedTc = new ArrayList();
            } else {
                skippedTc = (List<ITestResult>) testContext.getAttribute(SKIPPED_TEST_CASES);
            }
            skippedTc.add(tr);
            testContext.setAttribute(SKIPPED_TEST_CASES, skippedTc);

        } else {
            logger.info("[{}][{}][{}] -- PASSED", testCaseCompleteID,
                    testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
                    testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());

            if (testContext.getAttribute(PASSED_TEST_CASES) == null) {
                passedTc = new ArrayList();
            } else {
                passedTc = (List<ITestResult>) testContext.getAttribute(PASSED_TEST_CASES);
            }
            passedTc.add(tr);
            testContext.setAttribute(PASSED_TEST_CASES, passedTc);

        }
    } else {
        super.onTestSuccess(tr);
    }
}
 
Example 7
Source File: CustomTestNgListener.java    From heat with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart(ITestContext testContext) {
    String testName = testContext.getCurrentXmlTest().getName();
    MDC.put("testName", testName);
    List<ITestResult> localSkippedTests = new ArrayList();
    testContext.setAttribute(SKIPPED_TEST_CASES, localSkippedTests);
}
 
Example 8
Source File: BaseStageTest.java    From helix with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void beforeTest(Method testMethod, ITestContext testContext){
  long startTime = System.currentTimeMillis();
  System.out.println("START " + testMethod.getName() + " at " + new Date(startTime));
  testContext.setAttribute("StartTime", System.currentTimeMillis());
  setup();
}
 
Example 9
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 5 votes vote down vote up
/**
 * Package private. Used in unit test.
 *
 * @return UID for the current suite
 */
String getSuiteUid(ITestContext iTestContext) {
    String uid;
    if (iTestContext.getAttribute(SUITE_UID) != null) {
        uid = (String) iTestContext.getAttribute(SUITE_UID);
    } else {
        uid = UUID.randomUUID().toString();
        iTestContext.setAttribute(SUITE_UID, uid);
    }
    return uid;
}
 
Example 10
Source File: AbstractSeleniumTest.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Before method.
 *
 * @param currentMethod
 *            the current method
 * @param testArgs
 *            the test args
 */
@BeforeMethod ( alwaysRun = true)
public void beforeMethod( Method currentMethod, Object[] testArgs, ITestContext testContext )
{
    try
    {
        TestContainer tC = ((TestContainer) testArgs[0]);
        if ( testContext.getAttribute( "testContainer" ) == null )
            testContext.setAttribute( "testContainer", tC );

        TestPackage testPackage = tC.getTestPackage( currentMethod, true, xmlMode );

        testPackageContainer.set( testPackage );

        TestName testName = testPackage.getTestName();
        
        
        
        if ( !xmlMode )
        {
            //
            // Stub out the test and the execution context
            //
            ExecutionContextTest eC = new ExecutionContextTest();
            eC.setxFID( tC.getxFID() );
            eC.setTestName( currentMethod.getName() );
            
            CloudDescriptor cD = CloudRegistry.instance(tC.getxFID()).getCloud();
            if ( testPackage.getDevice().getCloud() != null && !testPackage.getDevice().getCloud().trim().isEmpty() )
                cD = CloudRegistry.instance(tC.getxFID()).getCloud( testPackage.getDevice().getCloud() );
            
            KeyWordTest kwt = new KeyWordTest( currentMethod.getName(), true, null, null, false, null, null, 0, currentMethod.getName() + " from " + currentMethod.getClass().getName(), null, null, null, ExecutionContext.instance(tC.getxFID()).getConfigProperties(), 0, null, null, null, null, 0, 0, TRACE.OFF.name() );
            eC.setTest( kwt );
            eC.setAut( ApplicationRegistry.instance(tC.getxFID()).getAUT() );
            eC.setCloud( cD );
            eC.setDevice( testPackage.getConnectedDevice().getPopulatedDevice() );
            testPackage.getConnectedDevice().getWebDriver().setExecutionContext( eC );
            
            testName.setTestName( currentMethod.getName() );
            
            testName.setTest( eC );
            
            File artifactFolder = new File( testPackage.getConnectedDevice().getDevice().getEnvironment(), testName.getTestName() );
            testPackage.getConnectedDevice().getWebDriver().setArtifactFolder( artifactFolder );
            
        }

        
        ConnectedDevice connectedDevice = testPackage.getConnectedDevice();

        String contentKey = testName.getContentKey();

        if ( (contentKey != null) && (contentKey.length() > 0) )
        {
            ContentManager.instance(tC.getxFID()).setCurrentContentKey( contentKey );
        }
        else
        {
            ContentManager.instance(tC.getxFID()).setCurrentContentKey( null );
        }

        if ( connectedDevice != null )
        {
            if ( testName.getTestName() == null || testName.getTestName().isEmpty() )
                testName.setTestName( currentMethod.getDeclaringClass().getSimpleName() + "." + currentMethod.getName() );

            testName.setFullName( testArgs[0].toString() );

            if ( testFlow.isInfoEnabled() )
                testFlow.info( Thread.currentThread().getName() + ": acquired for " + currentMethod.getName() );
        }

        
    }
    catch ( Exception e )
    {
        testFlow.fatal( Thread.currentThread().getName() + ": Fatal error configuring test", e );
    }
}
 
Example 11
Source File: DBServerListener.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void onStart(ITestContext context) {
  context.setAttribute(DB_SERVER_URL_ATTRIBUTE_NAME, DB_SERVER_URL);
}
 
Example 12
Source File: TestP2PMessages.java    From helix with Apache License 2.0 4 votes vote down vote up
@BeforeMethod  // just to overide the per-test setup in base class.
public void beforeTest(Method testMethod, ITestContext testContext) {
  long startTime = System.currentTimeMillis();
  System.out.println("START " + testMethod.getName() + " at " + new Date(startTime));
  testContext.setAttribute("StartTime", System.currentTimeMillis());
}
 
Example 13
Source File: ZkTestBase.java    From helix with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void beforeTest(Method testMethod, ITestContext testContext) {
  long startTime = System.currentTimeMillis();
  System.out.println("START " + testMethod.getName() + " at " + new Date(startTime));
  testContext.setAttribute("StartTime", System.currentTimeMillis());
}