use of org.openqa.selenium.ElementNotInteractableException in project carina by qaprosoft.
the class ExtendedWebElement method overrideAction.
// single place for all supported UI actions in carina core
private Object overrideAction(ACTION_NAME actionName, Object... inputArgs) {
Object output = executeAction(actionName, new ActionSteps() {
@Override
public void doClick() {
try {
DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));
try {
element.click();
} catch (ElementNotInteractableException e) {
// TODO: investigate if more exceptions should be catched
// not visible so we can't interact using selenium or actions
LOGGER.warn("Trying to do click by JavascriptExecutor because element '" + getNameWithLocator() + "' is not visible...");
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("arguments[0].click();", element);
}
} catch (WebDriverException e) {
if (e != null && (e.getMessage().contains("Other element would receive the click:"))) {
LOGGER.warn("Trying to do click by Actions due to the: " + e.getMessage());
Actions actions = new Actions(getDriver());
actions.moveToElement(element).click().perform();
} else {
throw e;
}
}
}
@Override
public // click for mobile devices
void doTap() {
DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));
element.click();
}
@Override
public void doDoubleClick() {
DriverListener.setMessages(Messager.ELEMENT_DOUBLE_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_DOUBLE_CLICKED.getMessage(getNameWithLocator()));
WebDriver drv = getDriver();
Actions action = new Actions(drv);
action.moveToElement(element).doubleClick(element).build().perform();
}
@Override
public void doHover(Integer xOffset, Integer yOffset) {
DriverListener.setMessages(Messager.ELEMENT_HOVERED.getMessage(getName()), Messager.ELEMENT_NOT_HOVERED.getMessage(getNameWithLocator()));
WebDriver drv = getDriver();
Actions action = new Actions(drv);
if (xOffset != null && yOffset != null) {
action.moveToElement(element, xOffset, yOffset).build().perform();
} else {
action.moveToElement(element).build().perform();
}
}
@Override
public void doSendKeys(Keys keys) {
DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(keys.toString(), getName()), Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(keys.toString(), getNameWithLocator()));
element.sendKeys(keys);
}
@Override
public void doType(String text) {
final String decryptedText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);
/* if (!element.getText().isEmpty()) {
DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()),
Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));
element.clear();
}
*/
DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()), Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));
element.clear();
String textLog = (!decryptedText.equals(text) ? "********" : text);
DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(textLog, getName()), Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(textLog, getNameWithLocator()));
element.sendKeys(decryptedText);
}
@Override
public void doAttachFile(String filePath) {
final String decryptedText = cryptoTool.decryptByPattern(filePath, CRYPTO_PATTERN);
String textLog = (!decryptedText.equals(filePath) ? "********" : filePath);
DriverListener.setMessages(Messager.FILE_ATTACHED.getMessage(textLog, getName()), Messager.FILE_NOT_ATTACHED.getMessage(textLog, getNameWithLocator()));
((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.display = 'block';", element);
((RemoteWebDriver) castDriver(getDriver())).setFileDetector(new LocalFileDetector());
element.sendKeys(decryptedText);
}
@Override
public String doGetText() {
String text = element.getText();
LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Text", text, getName()));
return text;
}
@Override
public Point doGetLocation() {
Point point = element.getLocation();
LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Location", point.toString(), getName()));
return point;
}
@Override
public Dimension doGetSize() {
Dimension dim = element.getSize();
LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Size", dim.toString(), getName()));
return dim;
}
@Override
public String doGetAttribute(String name) {
String attribute = element.getAttribute(name);
LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage(name, attribute, getName()));
return attribute;
}
@Override
public void doRightClick() {
DriverListener.setMessages(Messager.ELEMENT_RIGHT_CLICKED.getMessage(getName()), Messager.ELEMENT_NOT_RIGHT_CLICKED.getMessage(getNameWithLocator()));
WebDriver drv = getDriver();
Actions action = new Actions(drv);
action.moveToElement(element).contextClick(element).build().perform();
}
@Override
public void doCheck() {
DriverListener.setMessages(Messager.CHECKBOX_CHECKED.getMessage(getName()), null);
boolean isSelected = element.isSelected();
if (element.getAttribute("checked") != null) {
isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");
}
if (!isSelected) {
click();
}
}
@Override
public void doUncheck() {
DriverListener.setMessages(Messager.CHECKBOX_UNCHECKED.getMessage(getName()), null);
boolean isSelected = element.isSelected();
if (element.getAttribute("checked") != null) {
isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");
}
if (isSelected) {
click();
}
}
@Override
public boolean doIsChecked() {
boolean res = element.isSelected();
if (element.getAttribute("checked") != null) {
res |= element.getAttribute("checked").equalsIgnoreCase("true");
}
return res;
}
@Override
public boolean doSelect(String text) {
final String decryptedSelectText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);
String textLog = (!decryptedSelectText.equals(text) ? "********" : text);
DriverListener.setMessages(Messager.SELECT_BY_TEXT_PERFORMED.getMessage(textLog, getName()), Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(textLog, getNameWithLocator()));
final Select s = new Select(getCachedElement());
// [VD] do not use selectByValue as modern controls could have only visible value without value
s.selectByVisibleText(decryptedSelectText);
return true;
}
@Override
public boolean doSelectValues(String[] values) {
boolean result = true;
for (String value : values) {
if (!select(value)) {
result = false;
}
}
return result;
}
@Override
public boolean doSelectByMatcher(BaseMatcher<String> matcher) {
DriverListener.setMessages(Messager.SELECT_BY_MATCHER_TEXT_PERFORMED.getMessage(matcher.toString(), getName()), Messager.SELECT_BY_MATCHER_TEXT_NOT_PERFORMED.getMessage(matcher.toString(), getNameWithLocator()));
final Select s = new Select(getCachedElement());
String fullTextValue = null;
for (WebElement option : s.getOptions()) {
if (matcher.matches(option.getText())) {
fullTextValue = option.getText();
break;
}
}
s.selectByVisibleText(fullTextValue);
return true;
}
@Override
public boolean doSelectByPartialText(String partialSelectText) {
DriverListener.setMessages(Messager.SELECT_BY_TEXT_PERFORMED.getMessage(partialSelectText, getName()), Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(partialSelectText, getNameWithLocator()));
final Select s = new Select(getCachedElement());
String fullTextValue = null;
for (WebElement option : s.getOptions()) {
if (option.getText().contains(partialSelectText)) {
fullTextValue = option.getText();
break;
}
}
s.selectByVisibleText(fullTextValue);
return true;
}
@Override
public boolean doSelectByIndex(int index) {
DriverListener.setMessages(Messager.SELECT_BY_INDEX_PERFORMED.getMessage(String.valueOf(index), getName()), Messager.SELECT_BY_INDEX_NOT_PERFORMED.getMessage(String.valueOf(index), getNameWithLocator()));
final Select s = new Select(getCachedElement());
s.selectByIndex(index);
return true;
}
@Override
public String doGetSelectedValue() {
final Select s = new Select(getCachedElement());
return s.getAllSelectedOptions().get(0).getText();
}
@Override
public List<String> doGetSelectedValues() {
final Select s = new Select(getCachedElement());
List<String> values = new ArrayList<String>();
for (WebElement we : s.getAllSelectedOptions()) {
values.add(we.getText());
}
return values;
}
}, inputArgs);
return output;
}
Aggregations