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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations