Search in sources :

Example 16 with TechnicalException

use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.

the class ScenarioInitiator method injectWithModel.

private static void injectWithModel(String scenarioName, Class<Model> model) throws TechnicalException {
    try {
        final String[] headers = Context.getDataInputProvider().readLine(0, false);
        if (headers != null) {
            final List<String[]> examples = new ArrayList<>();
            final Constructor<Model> modelConstructor = DataUtils.getModelConstructor(model, headers);
            final Map<String, ModelList> fusionedData = DataUtils.fusionProcessor(model, modelConstructor);
            for (final Entry<String, ModelList> e : fusionedData.entrySet()) {
                examples.add(new String[] { e.getKey(), e.getValue().serialize() });
            }
            GherkinFactory.injectDataInGherkinExamples(scenarioName, examples);
        } else {
            logger.error(Messages.getMessage(SCENARIO_INITIATOR_ERROR_EMPTY_FILE));
        }
    } catch (final Exception te) {
        throw new TechnicalException(Messages.getMessage(SCENARIO_INITIATOR_ERROR_ON_INJECTING_MODEL) + te.getMessage(), te);
    }
}
Also used : ModelList(com.github.noraui.model.ModelList) TechnicalException(com.github.noraui.exception.TechnicalException) ArrayList(java.util.ArrayList) Model(com.github.noraui.model.Model) TechnicalException(com.github.noraui.exception.TechnicalException)

Example 17 with TechnicalException

use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.

the class DBDataProvider method readValue.

/**
 * {@inheritDoc}
 *
 * @throws TechnicalException
 *             is thrown if you have a technical error (IOException on .sql file) in NoraUi.
 */
@Override
public String readValue(String column, int line) throws TechnicalException {
    logger.debug("readValue: column:[{}] and line:[{}] ", column, line);
    String sqlRequest;
    try {
        final Path file = Paths.get(dataInPath + scenarioName + ".sql");
        sqlRequest = new String(Files.readAllBytes(file), Charset.forName(Constants.DEFAULT_ENDODING));
        sqlSanitized4readOnly(sqlRequest);
    } catch (final IOException e) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + e.getMessage(), e);
    }
    try (Connection connection = getConnection();
        PreparedStatement statement = connection.prepareStatement(sqlRequest);
        ResultSet rs = statement.executeQuery()) {
        if (line < 1) {
            return column;
        }
        while (rs.next() && rs.getRow() < line) {
        }
        logger.info("column: {}", column);
        return rs.getString(column);
    } catch (final SQLException e) {
        logger.error("error DBDataProvider.readValue({}, {})", column, line, e);
        return "";
    }
}
Also used : Path(java.nio.file.Path) TechnicalException(com.github.noraui.exception.TechnicalException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 18 with TechnicalException

use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.

the class MailSteps method validActivationEmail.

/**
 * Valid activation email.
 *
 * @param mailHost
 *            example: imap.gmail.com
 * @param mailUser
 *            login of mail box
 * @param mailPassword
 *            password of mail box
 * @param firstCssQuery
 *            the first matching element
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 * @throws TechnicalException
 *             is throws if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with message and with screenshot and with exception if functional error but no screenshot and no exception if technical error.
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
@RetryOnFailure(attempts = 3, delay = 60)
@Conditioned
@Et("Je valide le mail d'activation '(.*)'[\\.|\\?]")
@And("I valid activation email '(.*)'[\\.|\\?]")
public void validActivationEmail(String mailHost, String mailUser, String mailPassword, String senderMail, String subjectMail, String firstCssQuery, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
    RestTemplate restTemplate = createRestTemplate();
    try {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imap");
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect(mailHost, mailUser, mailPassword);
        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        SearchTerm filterA = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        SearchTerm filterB = new FromTerm(new InternetAddress(senderMail));
        SearchTerm filterC = new SubjectTerm(subjectMail);
        SearchTerm[] filters = { filterA, filterB, filterC };
        SearchTerm searchTerm = new AndTerm(filters);
        Message[] messages = inbox.search(searchTerm);
        for (Message message : messages) {
            Document doc = Jsoup.parse(getTextFromMessage(message));
            Element link = doc.selectFirst(firstCssQuery);
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<String> entity = new HttpEntity<>(headers);
            ResponseEntity<String> response = restTemplate.exchange(link.attr("href"), HttpMethod.GET, entity, String.class);
            if (!response.getStatusCode().equals(HttpStatus.OK)) {
                new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
            }
        }
    } catch (Exception e) {
        new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) HttpEntity(org.springframework.http.HttpEntity) Element(org.jsoup.nodes.Element) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Document(org.jsoup.nodes.Document) Result(com.github.noraui.exception.Result) AndTerm(javax.mail.search.AndTerm) FlagTerm(javax.mail.search.FlagTerm) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) MessagingException(javax.mail.MessagingException) FailureException(com.github.noraui.exception.FailureException) IOException(java.io.IOException) TechnicalException(com.github.noraui.exception.TechnicalException) RestTemplate(org.springframework.web.client.RestTemplate) FromTerm(javax.mail.search.FromTerm) Session(javax.mail.Session) Conditioned(com.github.noraui.cucumber.annotation.Conditioned) And(cucumber.api.java.en.And) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Et(cucumber.api.java.fr.Et)

Example 19 with TechnicalException

use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.

the class Step method checkText.

/**
 * Checks if HTML text contains expected value.
 *
 * @param pageElement
 *            Is target element
 * @param textOrKey
 *            Is the new data (text or text in context (after a save))
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_WRONG_EXPECTED_VALUE} message (with screenshot, with exception) or with
 *             {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT} message
 *             (with screenshot, with exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void checkText(PageElement pageElement, String textOrKey) throws TechnicalException, FailureException {
    WebElement webElement = null;
    final String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
    try {
        webElement = Context.waitUntil(ExpectedConditions.presenceOfElementLocated(Utilities.getLocator(pageElement)));
    } catch (final Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT), true, pageElement.getPage().getCallBack());
    }
    final String innerText = webElement == null ? null : webElement.getText();
    logger.info("checkText() expected [{}] and found [{}].", value, innerText);
    if (!value.equals(innerText)) {
        new Result.Failure<>(innerText, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_WRONG_EXPECTED_VALUE), pageElement, value, pageElement.getPage().getApplication()), true, pageElement.getPage().getCallBack());
    }
}
Also used : WebElement(org.openqa.selenium.WebElement) ParseException(java.text.ParseException) FailureException(com.github.noraui.exception.FailureException) TechnicalException(com.github.noraui.exception.TechnicalException) Result(com.github.noraui.exception.Result)

Example 20 with TechnicalException

use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.

the class Step method saveElementValue.

/**
 * Save value in memory.
 *
 * @param field
 *            is name of the field to retrieve.
 * @param targetKey
 *            is the key to save value to
 * @param page
 *            is target page.
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT} message (with screenshot, with exception) or with
 *             {@value com.github.noraui.utils.Messages#FAIL_MESSAGE_UNABLE_TO_RETRIEVE_VALUE} message
 *             (with screenshot, with exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void saveElementValue(String field, String targetKey, Page page) throws TechnicalException, FailureException {
    logger.debug("saveValueInStep: {} to {} in {}.", field, targetKey, page.getApplication());
    String txt = "";
    try {
        final WebElement elem = Utilities.findElement(page, field);
        txt = elem.getAttribute(VALUE) != null ? elem.getAttribute(VALUE) : elem.getText();
    } catch (final Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_FIND_ELEMENT), true, page.getCallBack());
    }
    try {
        Context.saveValue(targetKey, txt);
        Context.getCurrentScenario().write("SAVE " + targetKey + "=" + txt);
    } catch (final Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_RETRIEVE_VALUE), page.getPageElementByKey(field), page.getApplication()), true, page.getCallBack());
    }
}
Also used : WebElement(org.openqa.selenium.WebElement) ParseException(java.text.ParseException) FailureException(com.github.noraui.exception.FailureException) TechnicalException(com.github.noraui.exception.TechnicalException) Result(com.github.noraui.exception.Result)

Aggregations

TechnicalException (com.github.noraui.exception.TechnicalException)39 FailureException (com.github.noraui.exception.FailureException)13 Result (com.github.noraui.exception.Result)12 WebElement (org.openqa.selenium.WebElement)10 IOException (java.io.IOException)9 ParseException (java.text.ParseException)9 Test (org.junit.Test)8 PageElement (com.github.noraui.application.page.Page.PageElement)6 DemoPage (com.github.noraui.application.page.demo.DemoPage)6 ArrayList (java.util.ArrayList)5 Then (cucumber.api.java.en.Then)4 File (java.io.File)4 Path (java.nio.file.Path)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 GherkinStepCondition (com.github.noraui.gherkin.GherkinStepCondition)3 ModelList (com.github.noraui.model.ModelList)3 Conditioned (com.github.noraui.cucumber.annotation.Conditioned)2