Search in sources :

Example 21 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testUserDefinedParamAddedToTestVariables.

/**
 * Check that user defined arguments passed through command line (-DmyParam=myValue) is copied to testVariables and added to configuration
 */
@Test(groups = { "ut context" })
public void testUserDefinedParamAddedToTestVariables(final ITestContext testNGCtx) {
    try {
        System.setProperty("myUserDefinedKey", "myUserDefinedValue");
        initThreadContext(testNGCtx);
        SeleniumTestsContext seleniumTestsCtx = SeleniumTestsContextManager.getThreadContext();
        Assert.assertEquals(seleniumTestsCtx.getConfiguration().get("myUserDefinedKey").getValue(), "myUserDefinedValue");
    } finally {
        System.clearProperty("myUserDefinedKey");
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 22 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testPlatformParsingForAndroid.

@Test(groups = { "ut context" })
public void testPlatformParsingForAndroid(final ITestContext testNGCtx, final XmlTest xmlTest) {
    try {
        System.setProperty("platform", "Android 5.0");
        System.setProperty("deviceName", "");
        initThreadContext(testNGCtx);
        SeleniumTestsContext seleniumTestsCtx = SeleniumTestsContextManager.getThreadContext();
        Assert.assertEquals(seleniumTestsCtx.getPlatform(), "Android");
        Assert.assertEquals(seleniumTestsCtx.getMobilePlatformVersion(), "5.0");
    } finally {
        System.clearProperty("platform");
        System.clearProperty("deviceName");
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 23 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testUserDefinedParamOverriesXMLUserDefined.

/**
 * Check that when a user defined parameter is both defined in XML and on command line, the command line one has precedence
 * @param testNGCtx
 */
@Test(groups = { "ut context" })
public void testUserDefinedParamOverriesXMLUserDefined(final ITestContext testNGCtx) {
    try {
        System.setProperty("variable1", "myUserDefinedValue");
        initThreadContext(testNGCtx);
        SeleniumTestsContext seleniumTestsCtx = SeleniumTestsContextManager.getThreadContext();
        Assert.assertEquals(seleniumTestsCtx.getConfiguration().get("variable1").getValue(), "myUserDefinedValue");
    } finally {
        System.clearProperty("variable1");
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 24 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testUserDefinedParamOverridesEnvIni.

/**
 * If parameter is defined in env.ini and as JVM parameter (user defined), the user defined parameter must be used
 */
@Test(groups = { "ut context" })
public void testUserDefinedParamOverridesEnvIni(final ITestContext testNGCtx, final XmlTest xmlTest) {
    try {
        System.setProperty("key1", "userValue");
        initThreadContext(testNGCtx);
        SeleniumTestsContext seleniumTestsCtx = SeleniumTestsContextManager.getThreadContext();
        Assert.assertEquals(seleniumTestsCtx.getConfiguration().get("key1").getValue(), "userValue");
    } finally {
        System.clearProperty("key1");
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 25 with SeleniumTestsContext

use of com.seleniumtests.core.SeleniumTestsContext in project seleniumRobot by bhecquet.

the class CustomReporter method generateTestReport.

/**
 * Generates report for a single test
 * @param testResult
 */
private void generateTestReport(ITestResult testResult, ReportInfo reportInfo) {
    try {
        VelocityEngine ve = createVelocityEngine();
        Template t = ve.getTemplate(reportInfo.getTemplatePath());
        VelocityContext context = new VelocityContext();
        String reportFormat = reportInfo.getExtension().substring(1);
        Long testDuration = 0L;
        Integer errors = 0;
        String failedStep = "";
        List<TestStep> testSteps = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps();
        List<TestStep> newTestSteps = new ArrayList<>();
        if (testSteps != null) {
            for (TestStep step : testSteps) {
                testDuration += step.getDuration();
                if (Boolean.TRUE.equals(step.getFailed()) && !step.isTestEndStep()) {
                    failedStep = step.getName();
                    errors++;
                }
                // encode each step
                if ("xml".equalsIgnoreCase(reportFormat.toLowerCase()) || "json".equalsIgnoreCase(reportFormat.toLowerCase()) || "html".equalsIgnoreCase(reportFormat.toLowerCase()) || "csv".equalsIgnoreCase(reportFormat.toLowerCase())) {
                    newTestSteps.add(step.encode(reportFormat.toLowerCase()));
                } else {
                    newTestSteps.add(step);
                }
            }
        }
        // issue #227: number of errors set to -1 to distinguish skipped tests
        if (testResult.getStatus() == ITestResult.SKIP) {
            errors = -1;
        }
        // because test result published by this report must conform to other results
        if (testResult.getStatus() == ITestResult.SUCCESS) {
            errors = 0;
        }
        List<String> stack = null;
        if (testResult.getThrowable() != null) {
            StringBuilder stackString = new StringBuilder();
            ExceptionUtility.generateTheStackTrace(testResult.getThrowable(), testResult.getThrowable().getMessage(), stackString, reportFormat.toLowerCase());
            stack = Arrays.asList(stackString.toString().split("\n"));
        }
        SeleniumTestsContext seleniumTestsContext = TestNGResultUtils.getSeleniumRobotTestContext(testResult);
        // if adding some information, don't forget to add them to velocity model for integration tests
        context.put("errors", 0);
        context.put("newline", "\n");
        context.put("failures", errors);
        context.put("hostname", testResult.getHost() == null ? "" : testResult.getHost());
        context.put("suiteName", TestNGResultUtils.getUniqueTestName(testResult));
        context.put("className", testResult.getTestClass().getName());
        context.put("tests", newTestSteps == null ? 0 : newTestSteps.size());
        context.put("duration", testDuration / 1000.0);
        context.put("time", testResult.getStartMillis());
        context.put("startDate", new Date(testResult.getStartMillis()));
        context.put("testSteps", newTestSteps);
        context.put("retries", TestNGResultUtils.getRetry(testResult) == null ? 0 : TestNGResultUtils.getRetry(testResult));
        context.put("browser", seleniumTestsContext.getBrowser());
        context.put("mobileApp", StringUtility.encodeString(seleniumTestsContext.getApp(), reportFormat.toLowerCase()));
        context.put("device", StringUtility.encodeString(seleniumTestsContext.getDeviceName() == null ? "" : seleniumTestsContext.getDeviceName(), reportFormat.toLowerCase()));
        if (seleniumTestsContext.getTestType().isMobile()) {
            context.put("platform", seleniumTestsContext.getPlatform() + " " + seleniumTestsContext.getMobilePlatformVersion());
        } else {
            context.put("platform", seleniumTestsContext.getPlatform());
        }
        context.put("version", SeleniumTestsContextManager.getApplicationVersion());
        context.put("coreVersion", SeleniumTestsContextManager.getCoreVersion());
        context.put("parameters", seleniumTestsContext.getContextDataMap());
        context.put("stacktrace", stack);
        context.put("failedStep", StringUtility.encodeString(failedStep, reportFormat.toLowerCase()));
        String logs = SeleniumRobotLogger.getTestLogs().get(getTestName(testResult));
        getTestLogs(context, reportFormat, logs);
        context.put("testInfos", TestNGResultUtils.getTestInfoEncoded(testResult, reportFormat));
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        String fileName = reportInfo.prefix + "-result" + reportInfo.extension;
        generateReport(Paths.get(seleniumTestsContext.getOutputDirectory(), fileName).toFile(), writer.toString());
        generatedFiles.add(fileName);
        TestNGResultUtils.setCustomReportCreated(testResult, true);
    } catch (Exception e) {
        logger.error(String.format("Error generating test result %s: %s", TestNGResultUtils.getUniqueTestName(testResult), e.getMessage()));
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) TestStep(com.seleniumtests.reporter.logger.TestStep) SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) Date(java.util.Date) ScenarioException(com.seleniumtests.customexception.ScenarioException) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter)

Aggregations

SeleniumTestsContext (com.seleniumtests.core.SeleniumTestsContext)54 Test (org.testng.annotations.Test)44 GenericTest (com.seleniumtests.GenericTest)43 XmlTest (org.testng.xml.XmlTest)32 ITestResult (org.testng.ITestResult)12 AndroidCapabilitiesFactory (com.seleniumtests.browserfactory.AndroidCapabilitiesFactory)11 DriverConfig (com.seleniumtests.driver.DriverConfig)11 MutableCapabilities (org.openqa.selenium.MutableCapabilities)11 HashMap (java.util.HashMap)9 TestVariable (com.seleniumtests.core.TestVariable)7 ConnectorsTest (com.seleniumtests.ConnectorsTest)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 TestStep (com.seleniumtests.reporter.logger.TestStep)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 ITestContext (org.testng.ITestContext)4 IOException (java.io.IOException)3 Map (java.util.Map)3