Search in sources :

Example 11 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class TestTasks method executeCommand.

/**
 * Execute a command
 * If test is run locally, you can execute any command, limit is the rights given to the user
 * If test is run through seleniumRobot grid, only commands allowed by grid will be allowed
 * @param program	program to execute
 * @param charset	the charset used to read program output
 * @param timeout	timeout in seconds. After this duration, program will be terminated
 * @param args		arguments to give to program
 */
public static String executeCommand(String program, int timeout, Charset charset, String... args) {
    if (SeleniumTestsContextManager.getThreadContext().getRunMode() == DriverMode.LOCAL) {
        String[] cmd = new String[] { program };
        cmd = ArrayUtils.addAll(cmd, args);
        return OSCommand.executeCommandAndWait(cmd, timeout, charset);
    } else if (SeleniumTestsContextManager.getThreadContext().getRunMode() == DriverMode.GRID) {
        SeleniumGridConnector gridConnector = SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector();
        if (gridConnector != null) {
            return gridConnector.executeCommand(program, timeout, args);
        } else {
            throw new ScenarioException(ERROR_NO_GRID_CONNECTOR_ACTIVE);
        }
    } else {
        throw new ScenarioException("command execution only supported in local and grid mode");
    }
}
Also used : SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 12 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class ScreenshotUtil method captureAllImages.

/**
 * Capture all images and returns them as BufferedImages
 * @param target
 * @param allWindows
 * @return
 */
private List<NamedBufferedImage> captureAllImages(SnapshotTarget target, boolean allWindows, int scrollDelay) {
    List<NamedBufferedImage> capturedImages = new ArrayList<>();
    // capture desktop
    if (target.isScreenTarget() && SeleniumTestsContextManager.isDesktopWebTest()) {
        capturedImages.add(new NamedBufferedImage(captureDesktop(), ""));
    // capture desktop
    } else if (target.isMainScreenTarget() && SeleniumTestsContextManager.isDesktopWebTest()) {
        capturedImages.add(new NamedBufferedImage(captureDesktop(true), ""));
    // capture web with scrolling
    } else if (target.isPageTarget() && SeleniumTestsContextManager.isWebTest()) {
        removeAlert();
        capturedImages.addAll(captureWebPages(allWindows, scrollDelay));
    // capture web without scrolling on the main window
    } else if (target.isViewportTarget() && SeleniumTestsContextManager.isWebTest()) {
        removeAlert();
        target.setSnapshotRectangle(new Rectangle(((CustomEventFiringWebDriver) driver).getScrollPosition(), ((CustomEventFiringWebDriver) driver).getViewPortDimensionWithoutScrollbar()));
        // allow removing of scrollbar (a negative value would not remove it)
        capturedImages.add(new NamedBufferedImage(capturePage(0, 0), ""));
    // capture web with scrolling on the main window
    } else if (target.isElementTarget() && SeleniumTestsContextManager.isWebTest()) {
        removeAlert();
        try {
            target.setSnapshotRectangle(target.getElement().getRect());
        } catch (WebDriverException e) {
            throw new ScenarioException(String.format("Cannot check element %s snapshot as it is not available", target.getElement()));
        }
        capturedImages.addAll(captureWebPages(false, scrollDelay));
    } else if ((target.isPageTarget() || target.isElementTarget() || target.isViewportTarget()) && SeleniumTestsContextManager.isAppTest()) {
        capturedImages.add(new NamedBufferedImage(capturePage(-1, -1), ""));
    } else {
        throw new ScenarioException("Capturing page is only possible for web and application tests. Capturing desktop possible for desktop web tests only");
    }
    // if we want to capture an element only, crop the previous capture
    if (target.isElementTarget() && target.getElement() != null && !capturedImages.isEmpty()) {
        Rectangle elementPosition = target.getElement().getRect();
        NamedBufferedImage wholeImage = capturedImages.remove(0);
        BufferedImage elementImage = ImageProcessor.cropImage(wholeImage.image, elementPosition.x, elementPosition.y, elementPosition.width, elementPosition.height);
        NamedBufferedImage namedElementImage = new NamedBufferedImage(elementImage, "");
        namedElementImage.addElementMetaDataToImage(target.getElement());
        capturedImages.add(0, namedElementImage);
    }
    return capturedImages;
}
Also used : ArrayList(java.util.ArrayList) Rectangle(org.openqa.selenium.Rectangle) ScenarioException(com.seleniumtests.customexception.ScenarioException) BufferedImage(java.awt.image.BufferedImage) WebDriverException(org.openqa.selenium.WebDriverException)

Example 13 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class TestWebUIDriver method testDriverCreationWithVideoInError.

@Test(groups = { "ut" })
public void testDriverCreationWithVideoInError() throws Exception {
    SeleniumTestsContextManager.getThreadContext().setVideoCapture("true");
    SeleniumTestsContextManager.getThreadContext().setBrowser("htmlunit");
    PowerMockito.mockStatic(CustomEventFiringWebDriver.class);
    PowerMockito.doThrow(new ScenarioException("error")).when(CustomEventFiringWebDriver.class, "startVideoCapture", eq(DriverMode.LOCAL), eq(null), any(File.class), eq("videoCapture.avi"));
    WebDriver driver = WebUIDriver.getWebDriver(true);
    Assert.assertTrue(driver instanceof CustomEventFiringWebDriver);
    Assert.assertNull(WebUIDriver.getVideoRecorder().get());
}
Also used : NLWebDriver(com.neotys.selenium.proxies.NLWebDriver) WebDriver(org.openqa.selenium.WebDriver) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) File(java.io.File) ScenarioException(com.seleniumtests.customexception.ScenarioException) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class PictureElement method isDriverCreated.

private WebUIDriver isDriverCreated() {
    WebUIDriver uiDriver = WebUIDriver.getWebUIDriver(false);
    if (uiDriver == null) {
        throw new ScenarioException("Driver has not already been created");
    }
    CustomEventFiringWebDriver driver = (CustomEventFiringWebDriver) uiDriver.getDriver();
    if (driver == null) {
        throw new ScenarioException("Driver has not already been created");
    }
    return uiDriver;
}
Also used : WebUIDriver(com.seleniumtests.driver.WebUIDriver) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 15 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class PageObject method assertForValue.

@GenericStep
public <T extends PageObject> T assertForValue(String fieldName, String value) {
    Element element = getElement(fieldName);
    if (element instanceof HtmlElement) {
        Assert.assertTrue(((HtmlElement) element).isElementPresent(0), String.format(ERROR_ELEMENT_NOT_PRESENT, fieldName));
        Assert.assertTrue(((HtmlElement) element).getText().equals(value) || ((HtmlElement) element).getValue().equals(value), String.format("Value of element %s is not %s", fieldName, value));
    } else {
        throw new ScenarioException(String.format(ELEMENT_S_IS_NOT_AN_HTML_ELEMENT_SUBCLASS, fieldName));
    }
    return (T) this;
}
Also used : HtmlElement(com.seleniumtests.uipage.htmlelements.HtmlElement) WebElement(org.openqa.selenium.WebElement) HtmlElement(com.seleniumtests.uipage.htmlelements.HtmlElement) GenericPictureElement(com.seleniumtests.uipage.htmlelements.GenericPictureElement) RadioButtonElement(com.seleniumtests.uipage.htmlelements.RadioButtonElement) Element(com.seleniumtests.uipage.htmlelements.Element) CheckBoxElement(com.seleniumtests.uipage.htmlelements.CheckBoxElement) LinkElement(com.seleniumtests.uipage.htmlelements.LinkElement) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Aggregations

ScenarioException (com.seleniumtests.customexception.ScenarioException)68 ArrayList (java.util.ArrayList)17 WebElement (org.openqa.selenium.WebElement)14 UnirestException (kong.unirest.UnirestException)13 GenericPictureElement (com.seleniumtests.uipage.htmlelements.GenericPictureElement)12 IOException (java.io.IOException)12 JSONObject (kong.unirest.json.JSONObject)12 CheckBoxElement (com.seleniumtests.uipage.htmlelements.CheckBoxElement)11 Element (com.seleniumtests.uipage.htmlelements.Element)11 HtmlElement (com.seleniumtests.uipage.htmlelements.HtmlElement)11 LinkElement (com.seleniumtests.uipage.htmlelements.LinkElement)11 RadioButtonElement (com.seleniumtests.uipage.htmlelements.RadioButtonElement)11 File (java.io.File)10 List (java.util.List)9 HashMap (java.util.HashMap)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 AWTException (java.awt.AWTException)7 Robot (java.awt.Robot)6 Map (java.util.Map)5 TestStep (com.seleniumtests.reporter.logger.TestStep)4