Search in sources :

Example 51 with ScenarioException

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

the class PageObject method assertNotChecked.

@GenericStep
public <T extends PageObject> T assertNotChecked(String fieldName) {
    Element element = getElement(fieldName);
    if (element instanceof CheckBoxElement || element instanceof RadioButtonElement) {
        Assert.assertTrue(((HtmlElement) element).isElementPresent(0), String.format(ERROR_ELEMENT_NOT_PRESENT, fieldName));
        Assert.assertFalse(((HtmlElement) element).isSelected(), String.format("Element %s is checked", fieldName));
    } else {
        throw new ScenarioException(String.format("Element %s is not an CheckBoxElement/RadioButtonElement", fieldName));
    }
    return (T) this;
}
Also used : 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) RadioButtonElement(com.seleniumtests.uipage.htmlelements.RadioButtonElement) CheckBoxElement(com.seleniumtests.uipage.htmlelements.CheckBoxElement) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 52 with ScenarioException

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

the class PageObject method uploadFile.

/**
 * Method to handle file upload through robot class
 * /!\ This should only be used as the last option when uploading file cannot be done an other way as explained below
 * https://saucelabs.com/resources/articles/best-practices-tips-selenium-file-upload
 * <code>
 * driver.setFileDetector(new LocalFileDetector());
 * driver.get("http://sso.dev.saucelabs.com/test/guinea-file-upload");
 *   WebElement upload = driver.findElement(By.id("myfile"));
 *   upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");
 *   </code>
 *
 * To use this method, first click on the upload file button / link, then call this method.
 *
 * /!\ on firefox, clicking MUST be done through 'clickAction'. 'click()' is not supported by browser.
 * /!\ on firefox, using the uploadFile method several times without other actions between usage may lead to error. Firefox will never click to the button the second time, probably due to focus problems
 *
 * @param filePath
 */
@GenericStep
public <T extends PageObject> T uploadFile(String filePath) {
    try {
        byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(new File(filePath)));
        CustomEventFiringWebDriver.uploadFile(new File(filePath).getName(), new String(encoded), SeleniumTestsContextManager.getThreadContext().getRunMode(), SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector());
        Alert alert = waitForAlert(5);
        if (alert != null) {
            alert.accept();
        }
    } catch (IOException e) {
        throw new ScenarioException(String.format("could not read file to upload %s: %s", filePath, e.getMessage()));
    }
    return (T) this;
}
Also used : Alert(org.openqa.selenium.Alert) IOException(java.io.IOException) File(java.io.File) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 53 with ScenarioException

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

the class PageObject method selectNewWindow.

/**
 * Selects the first unknown window. To use immediately after an action creates a new window or tab
 * Each time we do a click, but just before it (selenium click, JS click or action click), we record the list of windows.
 * I a new window or tab is displayed, we select it.
 * @param waitMs	wait for N milliseconds before raising error
 * @return
 */
public final String selectNewWindow(int waitMs) {
    // app test are not compatible with window
    if (SeleniumTestsContextManager.getThreadContext().getTestType().family() == TestType.APP) {
        throw new ScenarioException("Application are not compatible with Windows");
    }
    // Keep the name of the current window handle before switching
    // sometimes, our action made window disappear
    String mainWindowHandle;
    try {
        mainWindowHandle = driver.getWindowHandle();
    } catch (Exception e) {
        mainWindowHandle = "";
    }
    internalLogger.debug("Current handle: " + mainWindowHandle);
    // wait for window to be displayed
    Instant end = systemClock.instant().plusMillis(waitMs + 250L);
    Set<String> handles = new TreeSet<>();
    boolean found = false;
    while (end.isAfter(systemClock.instant()) && !found) {
        handles = driver.getWindowHandles();
        internalLogger.debug("All handles: " + handles.toString());
        for (String handle : handles) {
            // we already know this handle
            if (getCurrentHandles().contains(handle)) {
                continue;
            }
            selectWindow(handle);
            // wait for a valid address
            String address = "";
            Instant endLoad = systemClock.instant().plusMillis(5000);
            while (address.isEmpty() && endLoad.isAfter(systemClock.instant())) {
                address = driver.getCurrentUrl();
            }
            // TODO: reactivate feature
            try {
            // Point windowPosition  = driver.manage().window().getPosition();
            // org.openqa.selenium.interactions.Mouse mouse = ((HasInputDevices) driver).getMouse();
            // mouse.click();
            // Mouse mouse = new DesktopMouse();
            // mouse.click(new DesktopScreenRegion(Math.max(0, windowPosition.x) + driver.manage().window().getSize().width / 2, Math.max(0, windowPosition.y) + 5, 2, 2).getCenter());
            } catch (Exception e) {
                internalLogger.warn("error while giving focus to window");
            }
            found = true;
            break;
        }
        WaitHelper.waitForMilliSeconds(300);
    }
    // check window has changed
    if (waitMs > 0 && mainWindowHandle.equals(driver.getWindowHandle())) {
        throw new CustomSeleniumTestsException("new window has not been found. Handles: " + handles);
    }
    return mainWindowHandle;
}
Also used : TreeSet(java.util.TreeSet) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) Instant(java.time.Instant) ScenarioException(com.seleniumtests.customexception.ScenarioException) ScenarioException(com.seleniumtests.customexception.ScenarioException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) TimeoutException(org.openqa.selenium.TimeoutException) NotCurrentPageException(com.seleniumtests.customexception.NotCurrentPageException) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriverException(org.openqa.selenium.WebDriverException) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) IOException(java.io.IOException) UnsupportedCommandException(org.openqa.selenium.UnsupportedCommandException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException)

Example 54 with ScenarioException

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

the class PageObject method assertChecked.

@GenericStep
public <T extends PageObject> T assertChecked(String fieldName) {
    Element element = getElement(fieldName);
    if (element instanceof CheckBoxElement || element instanceof RadioButtonElement) {
        Assert.assertTrue(((HtmlElement) element).isElementPresent(0), String.format(ERROR_ELEMENT_NOT_PRESENT, fieldName));
        Assert.assertTrue(((HtmlElement) element).isSelected(), String.format("Element %s is unchecked", fieldName));
    } else {
        throw new ScenarioException(String.format("Element %s is not an CheckBoxElement/RadioButtonElement", fieldName));
    }
    return (T) this;
}
Also used : 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) RadioButtonElement(com.seleniumtests.uipage.htmlelements.RadioButtonElement) CheckBoxElement(com.seleniumtests.uipage.htmlelements.CheckBoxElement) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 55 with ScenarioException

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

the class PageObject method assertSelectedOption.

@GenericStep
public <T extends PageObject> T assertSelectedOption(String fieldName, String value) {
    Element element = getElement(fieldName);
    if (element instanceof SelectList) {
        try {
            WebElement selectedOption = ((SelectList) element).getFirstSelectedOption();
            Assert.assertNotNull(selectedOption, "No selected option found");
            Assert.assertEquals(selectedOption.getText(), value, "Selected option is not the expected one");
        } catch (WebDriverException e) {
            Assert.assertTrue(false, String.format(ERROR_ELEMENT_NOT_PRESENT, fieldName));
        }
    } else {
        throw new ScenarioException(String.format("Element %s is not an SelectList subclass", fieldName));
    }
    return (T) this;
}
Also used : SelectList(com.seleniumtests.uipage.htmlelements.SelectList) 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) WebElement(org.openqa.selenium.WebElement) ScenarioException(com.seleniumtests.customexception.ScenarioException) WebDriverException(org.openqa.selenium.WebDriverException)

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