Search in sources :

Example 21 with TestCase

use of org.sakuli.datamodel.TestCase in project sakuli by ConSol.

the class TestCaseBuilder method build.

@Override
public TestCase build() {
    TestCase newTestCase = new TestCase(name, id);
    newTestCase.setStartDate(new Date());
    return newTestCase;
}
Also used : TestCase(org.sakuli.datamodel.TestCase) Date(java.util.Date)

Example 22 with TestCase

use of org.sakuli.datamodel.TestCase in project sakuli by ConSol.

the class TestCaseStepHelper method writeCachedStepDefinitions.

public static void writeCachedStepDefinitions(TestSuite testSuite) throws IOException {
    if (testSuite.getTestCases() != null) {
        for (TestCase tc : testSuite.getTestCases().values()) {
            if (tc.getTcFile() != null) {
                Path stepsCacheFile = tc.getTcFile().getParent().resolve(STEPS_CACHE_FILE);
                File output = new File(stepsCacheFile.toUri());
                if (!output.getParentFile().exists()) {
                    output.getParentFile().mkdirs();
                }
                try {
                    String stepNames = getStepName(tc.getSteps());
                    LOGGER.debug("write following step IDs to file '{}':\n{}", stepsCacheFile, stepNames);
                    FileUtils.writeStringToFile(output, stepNames, Charset.forName("UTF-8"), false);
                } catch (IOException e) {
                    throw new IOException(String.format("error during writing into '%s' file ", stepsCacheFile), e);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TestCase(org.sakuli.datamodel.TestCase) IOException(java.io.IOException) File(java.io.File)

Example 23 with TestCase

use of org.sakuli.datamodel.TestCase in project sakuli by ConSol.

the class TestCaseAction method saveResult.

/****************
     * TEST CASE HANDLING
     *********************/
/**
     * save the Result of a test Case
     *
     * @param testCaseId  id of the corresponding test case
     * @param startTime   start time in milliseconds
     * @param stopTime    end time in milliseconds
     * @param lastURL     URL to the last visited page during this test case
     * @param browserInfo detail information about the used browser
     * @throws SakuliException
     */
@LogToResult(message = "save the result of the current test case")
public void saveResult(String testCaseId, String startTime, String stopTime, String lastURL, String browserInfo) throws SakuliException {
    if (!loader.getCurrentTestCase().getId().equals(testCaseId)) {
        handleException("testcaseID '" + testCaseId + "' to save the test case Result ist is not valid!");
    }
    TestSuite testSuite = loader.getTestSuite();
    testSuite.setBrowserInfo(browserInfo);
    //set TestCase vars
    TestCase tc = loader.getCurrentTestCase();
    tc.setLastURL(lastURL);
    try {
        tc.setStartDate(new Date(Long.parseLong(startTime)));
        tc.setStopDate(new Date(Long.parseLong(stopTime)));
        logger.debug("test case duration = " + tc.getDuration());
        tc.refreshState();
    } catch (NumberFormatException | NullPointerException e) {
        handleException("Duration could not be calculated! " + "Check if the warning and critical threshold is set correctly in your test case!  " + "=> START date: " + startTime + "\tSTOP date: " + stopTime + "\n" + e.getMessage());
    }
    //release current test case -> indicates that this case is finished
    loader.setCurrentTestCase(null);
}
Also used : TestSuite(org.sakuli.datamodel.TestSuite) TestCase(org.sakuli.datamodel.TestCase) Date(java.util.Date) LogToResult(org.sakuli.actions.logging.LogToResult)

Example 24 with TestCase

use of org.sakuli.datamodel.TestCase in project sakuli by ConSol.

the class DatabaseResultServiceImpl method saveAllResults.

@Override
public void saveAllResults() {
    LOGGER.info("======= SAVE RESULTS TO DATABASE ======");
    ;
    try {
        daoTestSuite.saveTestSuiteResult();
        daoTestSuite.saveTestSuiteToSahiJobs();
        if (!CollectionUtils.isEmpty(testSuite.getTestCases())) {
            for (TestCase tc : testSuite.getTestCasesAsSortedSet()) {
                //write testcase and steps to DB
                daoTestCase.saveTestCaseResult(tc);
                LOGGER.info("... try to save all STEPS for test case '" + tc.getId() + "'!");
                SortedSet<TestCaseStep> steps = tc.getStepsAsSortedSet();
                if (!steps.isEmpty()) {
                    daoTestCaseStep.saveTestCaseSteps(steps, tc.getDbPrimaryKey());
                    LOGGER.info("all STEPS for '" + tc.getId() + "' saved!");
                } else {
                    LOGGER.info("no STEPS for '\" + tc.getId() +\"'found => no STEPS saved in DB!");
                }
            }
        }
        LOGGER.info("======= FINISHED: SAVE RESULTS TO DATABASE ======");
    } catch (Throwable e) {
        exceptionHandler.handleException(new SakuliForwarderException(e, String.format("error by saving the results to the database [%s]", testSuite.toString())), true);
    }
}
Also used : DaoTestCase(org.sakuli.services.forwarder.database.dao.DaoTestCase) TestCase(org.sakuli.datamodel.TestCase) DaoTestCaseStep(org.sakuli.services.forwarder.database.dao.DaoTestCaseStep) TestCaseStep(org.sakuli.datamodel.TestCaseStep) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException)

Example 25 with TestCase

use of org.sakuli.datamodel.TestCase in project sakuli by ConSol.

the class TestSuiteHelper method loadTestCases.

/**
     * read out the 'testsuite.suite' file and create the corresponding test cases objects.
     *
     * @param properties loaded {@link TestSuiteProperties} of a {@link TestSuite}.
     * @return a map of {@link TestCase}s with her {@link TestCase#id} as key.
     * @throws FileNotFoundException if files are not reachable
     */
public static HashMap<String, TestCase> loadTestCases(TestSuiteProperties properties) throws IOException {
    Path testSuiteFile = properties.getTestSuiteSuiteFile();
    Path testSuiteFolder = properties.getTestSuiteFolder();
    HashMap<String, TestCase> tcMap = new HashMap<>();
    if (!Files.exists(testSuiteFile)) {
        throw new FileNotFoundException("Can not find specified " + TestSuiteProperties.TEST_SUITE_SUITE_FILE_NAME + " file at \"" + testSuiteFolder.toString() + "\"");
    }
    String testSuiteString = prepareTestSuiteFile(testSuiteFile);
    logger.info("\n--- TestSuite initialization: read test suite information of file \"" + testSuiteFile.toAbsolutePath().toString() + "\" ----\n" + testSuiteString + "\n --- End of File \"" + testSuiteFile.toAbsolutePath().toString() + "\" ---");
    //handle each line of the .suite file
    String regExLineSep = System.getProperty("line.separator") + "|\n";
    for (String line : testSuiteString.split(regExLineSep)) {
        if (!line.startsWith("//") && !(line.isEmpty())) {
            //get the start URL from suite
            String startURL = line.substring(line.lastIndexOf(' ') + 1);
            //extract tc file name name and generate new test case
            // get tc file name
            String tcFileName = line.substring(0, line.lastIndexOf(' '));
            Path tcFile = Paths.get(testSuiteFolder.toAbsolutePath().toString() + File.separator + tcFileName.replace("/", File.separator));
            if (Files.exists(tcFile)) {
                TestCase tc = new TestCase(TestCaseHelper.convertFolderPathToName(tcFileName), TestCaseHelper.convertTestCaseFileToID(tcFileName));
                tc.setStartUrl(startURL);
                tc.setTcFile(tcFile);
                tc.setSteps(TestCaseStepHelper.readCachedStepDefinitions(tcFile));
                //set the Map with the test case id as key
                tcMap.put(tc.getId(), tc);
            } else {
                throw new FileNotFoundException("test case path \"" + tcFile.toAbsolutePath().toString() + "\" doesn't exists - check your \"" + TestSuiteProperties.TEST_SUITE_SUITE_FILE_NAME + "\" file");
            }
        }
    }
    return tcMap;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) TestCase(org.sakuli.datamodel.TestCase) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

TestCase (org.sakuli.datamodel.TestCase)49 Test (org.testng.annotations.Test)33 BaseTest (org.sakuli.BaseTest)20 TestCaseExampleBuilder (org.sakuli.builder.TestCaseExampleBuilder)14 Path (java.nio.file.Path)13 TestSuite (org.sakuli.datamodel.TestSuite)13 TestCaseStepExampleBuilder (org.sakuli.builder.TestCaseStepExampleBuilder)11 DateTime (org.joda.time.DateTime)8 TestCaseStep (org.sakuli.datamodel.TestCaseStep)8 TreeSet (java.util.TreeSet)7 IOException (java.io.IOException)5 Date (java.util.Date)5 SakuliException (org.sakuli.exceptions.SakuliException)5 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)5 GearmanProperties (org.sakuli.services.forwarder.gearman.GearmanProperties)4 TestCaseStepBuilder (org.sakuli.datamodel.builder.TestCaseStepBuilder)3 DaoTestCase (org.sakuli.services.forwarder.database.dao.DaoTestCase)2 DaoTestCaseStep (org.sakuli.services.forwarder.database.dao.DaoTestCaseStep)2 SqlLobValue (org.springframework.jdbc.core.support.SqlLobValue)2 File (java.io.File)1