Search in sources :

Example 1 with TechnicalException

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

the class Step method uploadFile.

/**
 * Updates a html file input with the path of the file to upload.
 *
 * @param pageElement
 *            Is target element
 * @param fileOrKey
 *            Is the file path (text or text in context (after a save))
 * @param args
 *            list of arguments to format the found selector with
 * @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_UPLOADING_FILE} message (with screenshot, no exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void uploadFile(PageElement pageElement, String fileOrKey, Object... args) throws TechnicalException, FailureException {
    final String path = Context.getValue(fileOrKey) != null ? Context.getValue(fileOrKey) : System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + fileOrKey;
    if (!"".equals(path)) {
        try {
            final WebElement element = Context.waitUntil(ExpectedConditions.presenceOfElementLocated(Utilities.getLocator(pageElement, args)));
            element.clear();
            if (DriverFactory.IE.equals(Context.getBrowser())) {
                final String javascript = "arguments[0].value='" + path + "';";
                ((JavascriptExecutor) getDriver()).executeScript(javascript, element);
            } else {
                element.sendKeys(path);
            }
        } catch (final Exception e) {
            new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_UPLOADING_FILE), path), true, pageElement.getPage().getCallBack());
        }
    } else {
        logger.debug("Empty data provided. No need to update file upload path. If you want clear data, you need use: \"I clear text in ...\"");
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) 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 2 with TechnicalException

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

the class Step method runAllStepsInLoop.

/**
 * Runs a bunch of steps for a Gherkin loop.
 *
 * @param loopedSteps
 *            GherkinConditionedLoopedStep steps to run
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 */
protected void runAllStepsInLoop(List<GherkinConditionedLoopedStep> loopedSteps) throws TechnicalException {
    for (final GherkinConditionedLoopedStep loopedStep : loopedSteps) {
        final List<GherkinStepCondition> stepConditions = new ArrayList<>();
        final String[] expecteds = loopedStep.getExpected().split(";");
        final String[] actuals = loopedStep.getActual().split(";");
        if (actuals.length != expecteds.length) {
            throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_EXPECTED_ACTUAL_SIZE_DIFFERENT));
        }
        for (int i = 0; i < expecteds.length; i++) {
            stepConditions.add(new GherkinStepCondition(loopedStep.getKey(), expecteds[i], actuals[i]));
        }
        boolean found = false;
        for (final Entry<String, Method> elem : Context.getCucumberMethods().entrySet()) {
            final Matcher matcher = Pattern.compile("value=(.*)\\)").matcher(elem.getKey());
            if (matcher.find()) {
                final Matcher matcher2 = Pattern.compile(matcher.group(1)).matcher(loopedStep.getStep());
                if (matcher2.find()) {
                    Object[] tab;
                    if (elem.getValue().isAnnotationPresent(Conditioned.class)) {
                        tab = new Object[matcher2.groupCount() + 1];
                        tab[matcher2.groupCount()] = stepConditions;
                    } else {
                        tab = new Object[matcher2.groupCount()];
                    }
                    for (int i = 0; i < matcher2.groupCount(); i++) {
                        final Parameter param = elem.getValue().getParameters()[i];
                        if (param.getType() == int.class) {
                            final int ii = Integer.parseInt(matcher2.group(i + 1));
                            tab[i] = ii;
                        } else if (param.getType() == boolean.class) {
                            tab[i] = Boolean.parseBoolean(matcher2.group(i + 1));
                        } else {
                            tab[i] = matcher2.group(i + 1);
                        }
                    }
                    try {
                        found = true;
                        elem.getValue().invoke(NoraUiInjector.getNoraUiInjectorSource().getInstance(elem.getValue().getDeclaringClass()), tab);
                        break;
                    } catch (final Exception e) {
                        throw new TechnicalException("\"" + loopedStep.getStep() + "\"", e.getCause());
                    }
                }
            }
        }
        if (!found) {
            throw new TechnicalException(String.format(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_STEP_UNDEFINED), loopedStep.getStep()));
        }
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) Matcher(java.util.regex.Matcher) GherkinConditionedLoopedStep(com.github.noraui.gherkin.GherkinConditionedLoopedStep) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) ParseException(java.text.ParseException) FailureException(com.github.noraui.exception.FailureException) TechnicalException(com.github.noraui.exception.TechnicalException) Parameter(java.lang.reflect.Parameter)

Example 3 with TechnicalException

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

the class Step method updateText.

/**
 * Update a html input text with a text.
 *
 * @param pageElement
 *            Is target element
 * @param textOrKey
 *            Is the new data (text or text in context (after a save))
 * @param keysToSend
 *            character to send to the element after {@link org.openqa.selenium.WebElement#sendKeys(CharSequence...) sendKeys} with textOrKey
 * @param args
 *            list of arguments to format the found selector with
 * @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_ERROR_ON_INPUT} message (with screenshot, no exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void updateText(PageElement pageElement, String textOrKey, CharSequence keysToSend, Object... args) throws TechnicalException, FailureException {
    final String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
    if (!"".equals(value)) {
        try {
            final WebElement element = Context.waitUntil(ExpectedConditions.elementToBeClickable(Utilities.getLocator(pageElement, args)));
            element.clear();
            if (DriverFactory.IE.equals(Context.getBrowser())) {
                final String javascript = "arguments[0].value='" + value + "';";
                ((JavascriptExecutor) getDriver()).executeScript(javascript, element);
            } else {
                element.sendKeys(value);
            }
            if (keysToSend != null) {
                element.sendKeys(keysToSend);
            }
        } catch (final Exception e) {
            new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_ERROR_ON_INPUT), pageElement, pageElement.getPage().getApplication()), true, pageElement.getPage().getCallBack());
        }
    } else {
        logger.debug("Empty data provided. No need to update text. If you want clear data, you need use: \"I clear text in ...\"");
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) 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 4 with TechnicalException

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

the class Step method expectText.

/**
 * Expects that an element contains expected value.
 *
 * @param pageElement
 *            Is target element
 * @param textOrKey
 *            Is the expected data (text or text in context (after a save))
 * @throws FailureException
 *             if the scenario encounters a functional error
 * @throws TechnicalException
 */
protected void expectText(PageElement pageElement, String textOrKey) throws FailureException, TechnicalException {
    WebElement element = null;
    final String value = Context.getValue(textOrKey) != null ? Context.getValue(textOrKey) : textOrKey;
    try {
        element = 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());
    }
    try {
        Context.waitUntil(ExpectSteps.textToBeEqualsToExpectedValue(Utilities.getLocator(pageElement), value));
    } catch (final Exception e) {
        logger.error("error in expectText. element is [{}]", element == null ? null : element.getText());
        new Result.Failure<>(element == null ? null : element.getText(), 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 5 with TechnicalException

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

the class Step method clearText.

/**
 * Update a html input text with "".
 *
 * @param pageElement
 *            Is target element
 * @param keysToSend
 *            character to send to the element after {@link org.openqa.selenium.WebElement#sendKeys(CharSequence...) sendKeys} with textOrKey
 * @param args
 *            list of arguments to format the found selector with
 * @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_ERROR_CLEAR_ON_INPUT} message (with screenshot, no exception)
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
protected void clearText(PageElement pageElement, CharSequence keysToSend, Object... args) throws TechnicalException, FailureException {
    try {
        final WebElement element = Context.waitUntil(ExpectedConditions.presenceOfElementLocated(Utilities.getLocator(pageElement, args)));
        element.clear();
        if (keysToSend != null) {
            element.sendKeys(keysToSend);
        }
    } catch (final Exception e) {
        new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_ERROR_CLEAR_ON_INPUT), pageElement, 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)

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