Search in sources :

Example 6 with TechnicalException

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

the class CommonSteps method doUntil.

/**
 * Does steps execution until a given condition is unverified.
 *
 * @param actual
 *            actual value for global condition.
 * @param expected
 *            expected value for global condition.
 * @param key
 *            key of 'expected' values ('actual' values)
 * @param breakCondition
 *            'expected' values
 * @param tries
 *            number of max tries (no infinity loop).
 * @param conditions
 *            list of steps run in a loop.
 */
@Lorsque("Si '(.*)' vérifie '(.*)', je fais jusqu'à '(.*)' respecte '(.*)' avec '(.*)' essais maxi:")
@Then("If '(.*)' matches '(.*)', I do until '(.*)' respects '(.*)' with '(.*)' max tries:")
public void doUntil(String actual, String expected, String key, String breakCondition, int tries, List<GherkinConditionedLoopedStep> conditions) {
    try {
        if (new GherkinStepCondition("doUntilKey", expected, actual).checkCondition()) {
            int i = 0;
            do {
                i++;
                runAllStepsInLoop(conditions);
            } while (!Pattern.compile(breakCondition).matcher(Context.getValue(key) == null ? "" : Context.getValue(key)).find() && i <= tries);
        }
    } catch (final TechnicalException e) {
        throw new AssertError(Messages.getMessage(TechnicalException.TECHNICAL_SUBSTEP_ERROR_MESSAGE) + e.getMessage());
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) AssertError(com.github.noraui.exception.AssertError) Lorsque(cucumber.api.java.fr.Lorsque) Then(cucumber.api.java.en.Then)

Example 7 with TechnicalException

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

the class CommonSteps method whileDo.

/**
 * While a given condition is verified, does steps execution.
 *
 * @param actual
 *            actual value for global condition.
 * @param expected
 *            expected value for global condition.
 * @param key
 *            key of 'expected' values ('actual' values)
 * @param breakCondition
 *            'expected' values
 * @param tries
 *            number of max tries (no infinity loop).
 * @param conditions
 *            list of steps run in a loop.
 */
@Lorsque("Si '(.*)' vérifie '(.*)', Tant que '(.*)' respecte '(.*)' je fais avec '(.*)' essais maxi:")
@Then("If '(.*)' matches '(.*)', While '(.*)' respects '(.*)' I do with '(.*)' max tries:")
public void whileDo(String actual, String expected, String key, String breakCondition, int tries, List<GherkinConditionedLoopedStep> conditions) {
    try {
        if (new GherkinStepCondition("whileDoKey", expected, actual).checkCondition()) {
            int i = 0;
            while (!Pattern.compile(breakCondition).matcher(Context.getValue(key) == null ? "" : Context.getValue(key)).find() && i <= tries) {
                i++;
                runAllStepsInLoop(conditions);
            }
        }
    } catch (final TechnicalException e) {
        throw new AssertError(Messages.getMessage(TechnicalException.TECHNICAL_SUBSTEP_ERROR_MESSAGE) + e.getMessage());
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) AssertError(com.github.noraui.exception.AssertError) Lorsque(cucumber.api.java.fr.Lorsque) Then(cucumber.api.java.en.Then)

Example 8 with TechnicalException

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

the class CommonSteps method clickOnAndSwitchWindow.

/**
 * Click on html element and switch window when the scenario contain more one windows (one more application for example), if all 'expected' parameters equals 'actual' parameters in conditions.
 *
 * @param page
 *            The concerned page of toClick
 * @param toClick
 *            html element
 * @param windowKey
 *            the key of window (popup, ...) Example: 'demo.Popup1DemoPage'.
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 * @throws FailureException
 *             if the scenario encounters a functional error
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 */
@Conditioned
@Quand("Je clique sur '(.*)-(.*)' et passe sur '(.*)' de type fenêtre[\\.|\\?]")
@When("I click on '(.*)-(.*)' and switch to '(.*)' window[\\.|\\?]")
public void clickOnAndSwitchWindow(String page, String toClick, String windowKey, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
    final String wKey = Page.getInstance(page).getApplication() + Page.getInstance(windowKey).getPageKey();
    final String handleToSwitch = Context.getWindows().get(wKey);
    if (handleToSwitch != null) {
        Context.getDriver().switchTo().window(handleToSwitch);
        // As a workaround: NoraUi specify window size manually, e.g. window_size: 1920 x 1080 (instead of .window().maximize()).
        Context.getDriver().manage().window().setSize(new Dimension(1920, 1080));
        Context.setMainWindow(windowKey);
    } else {
        try {
            final Set<String> initialWindows = getDriver().getWindowHandles();
            clickOn(Page.getInstance(page).getPageElementByKey('-' + toClick));
            final String newWindowHandle = Context.waitUntil(WindowManager.newWindowOpens(initialWindows));
            Context.addWindow(wKey, newWindowHandle);
            getDriver().switchTo().window(newWindowHandle);
            // As a workaround: NoraUi specify window size manually, e.g. window_size: 1920 x 1080 (instead of .window().maximize()).
            Context.getDriver().manage().window().setSize(new Dimension(1920, 1080));
            Context.setMainWindow(newWindowHandle);
        } catch (final Exception e) {
            new Result.Failure<>(e.getMessage(), Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_SWITCH_WINDOW), windowKey), true, Page.getInstance(page).getCallBack());
        }
        if (!Page.getInstance(windowKey).checkPage()) {
            new Result.Failure<>(windowKey, Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_UNABLE_TO_SWITCH_WINDOW), windowKey), true, Page.getInstance(page).getCallBack());
        }
    }
}
Also used : Dimension(org.openqa.selenium.Dimension) FailureException(com.github.noraui.exception.FailureException) TechnicalException(com.github.noraui.exception.TechnicalException) Result(com.github.noraui.exception.Result) When(cucumber.api.java.en.When) Conditioned(com.github.noraui.cucumber.annotation.Conditioned) Quand(cucumber.api.java.fr.Quand)

Example 9 with TechnicalException

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

the class DriverFactory method generateGoogleChromeDriver.

/**
 * Generates a chrome webdriver.
 *
 * @return
 *         A chrome webdriver
 * @throws TechnicalException
 *             if an error occured when Webdriver setExecutable to true.
 */
private WebDriver generateGoogleChromeDriver() throws TechnicalException {
    final String pathWebdriver = DriverFactory.getPath(Driver.CHROME);
    if (!new File(pathWebdriver).setExecutable(true)) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
    }
    logger.info("Generating Chrome driver ({}) ...", pathWebdriver);
    System.setProperty(Driver.CHROME.getDriverName(), pathWebdriver);
    final ChromeOptions chromeOptions = new ChromeOptions();
    final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
    setLoggingLevel(capabilities);
    if (Context.isHeadless()) {
        chromeOptions.addArguments("--headless");
    }
    // Proxy configuration
    if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
        capabilities.setCapability(CapabilityType.PROXY, Context.getProxy());
    }
    setChromeOptions(capabilities, chromeOptions);
    final String withWhitelistedIps = Context.getWebdriversProperties("withWhitelistedIps");
    if (withWhitelistedIps != null && !"".equals(withWhitelistedIps)) {
        final ChromeDriverService service = new ChromeDriverService.Builder().withWhitelistedIps(withWhitelistedIps).withVerbose(false).build();
        return new ChromeDriver(service, capabilities);
    } else {
        return new ChromeDriver(capabilities);
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriverService(org.openqa.selenium.chrome.ChromeDriverService) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File)

Example 10 with TechnicalException

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

the class DriverFactory method getDriver.

/**
 * Get selenium driver. Drivers are lazy loaded.
 *
 * @return selenium driver
 */
public WebDriver getDriver() {
    // Driver's name is retrieved by system properties
    String driverName = Context.getBrowser();
    driverName = driverName != null ? driverName : DEFAULT_DRIVER;
    WebDriver driver = null;
    if (!drivers.containsKey(driverName)) {
        try {
            driver = generateWebDriver(driverName);
        } catch (final TechnicalException e) {
            logger.error("error DriverFactory.getDriver()", e);
        }
    } else {
        driver = drivers.get(driverName);
    }
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) TechnicalException(com.github.noraui.exception.TechnicalException)

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