use of org.openqa.selenium.InvalidSelectorException in project selenium_java by sergueik.
the class SuvianTest method test6_4.
// Selecting check boxes by their sibling labels, ByChained
@Test(enabled = true)
public void test6_4() {
// Arrange
List<String> hobbies = new ArrayList<>(Arrays.asList("Singing", "Dancing"));
driver.get("http://suvian.in/selenium/1.6checkbox.html");
try {
WebElement checkElement = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver _driver) {
return _driver.findElements(By.cssSelector("div.container div.row div.intro-message h3")).stream().filter(_element -> _element.getText().toLowerCase().indexOf("select your hobbies") > -1).findFirst().get();
}
});
System.err.println("element check: " + checkElement.getAttribute("innerHTML"));
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
// Act
WebElement formElement = driver.findElement(By.cssSelector("input[id]")).findElement(By.xpath(".."));
assertThat(formElement, notNullValue());
highlight(formElement, 1000);
List<WebElement> inputElements = formElement.findElements(By.cssSelector("label[for]")).stream().filter(_element -> hobbies.contains(_element.getText())).collect(Collectors.toList());
// C#: dataMap = elements.ToDictionary(x => x.GetAttribute("for"), x =>
// x.Text);
Map<String, String> dataMap = inputElements.stream().map(_element -> {
System.err.println("input element id: " + _element);
System.err.println("input element text: " + _element.getText());
System.err.println("input element 'for' attribute: " + _element.getAttribute("for"));
System.err.println("input element HTML: " + _element.getAttribute("outerHTML"));
System.err.println("input element XPath: " + xpathOfElement(_element));
System.err.println("input element CSS: " + cssSelectorOfElement(_element));
return _element;
}).collect(Collectors.toMap(_element -> _element.getText(), _element -> _element.getAttribute("for")));
List<WebElement> checkboxes = new ArrayList<>();
for (String hobby : hobbies) {
try {
System.err.println("finding: " + dataMap.get(hobby));
checkboxes.add(driver.findElements(new ByChained(By.cssSelector("input[id]"), By.xpath(".."), By.cssSelector(String.format("input#%s", dataMap.get(hobby))))).get(0));
} catch (InvalidSelectorException e) {
System.err.println("ignored: " + e.toString());
}
try {
checkboxes.add(formElement.findElement(// will not throw exception
By.xpath(String.format("input[@id='%s']", dataMap.get(hobby)))));
} catch (Exception e) {
System.err.println("ignored: " + e.toString());
}
}
Consumer<WebElement> act = _element -> {
highlight(_element);
_element.click();
};
checkboxes.stream().forEach(act);
// Assert
assertTrue(formElement.findElements(By.cssSelector("input[type='checkbox']")).stream().filter(o -> o.isSelected()).count() == hobbies.size());
}
use of org.openqa.selenium.InvalidSelectorException in project selenium_java by sergueik.
the class SuvianTest method test13_1.
// This test appears to find the button even though it is inside iframe
// without frame switch
@Test(enabled = true)
public void test13_1() {
// Arrange
driver.get("http://suvian.in/selenium/2.3frame.html");
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".container .row .intro-message iframe")));
// Act
WebElement buttonElement = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("h3 button"))));
assertThat(buttonElement, notNullValue());
// Assert
WebElement currentElement = buttonElement;
for (int cnt = 0; cnt != 4; cnt++) {
try {
WebElement containerElement = currentElement.findElement(By.xpath(".."));
String elementHTML = containerElement.getAttribute("outerHTML");
System.err.println("Parent element: " + (elementHTML.length() > 120 ? elementHTML.substring(0, 120) + "..." : elementHTML));
currentElement = containerElement;
} catch (InvalidSelectorException e) {
// ignore - reached top level
break;
}
}
buttonElement.click();
// Assert
try {
alert = driver.switchTo().alert();
String alertText = alert.getText();
assertTrue(alertText.contains("You clicked a button within a frame"));
// confirm alert
alert.accept();
} catch (NoAlertPresentException e) {
// Alert not present - ignore
} catch (WebDriverException e) {
System.err.println("Alert was not handled : " + e.getStackTrace().toString());
return;
}
}
use of org.openqa.selenium.InvalidSelectorException in project selenium_java by sergueik.
the class SuvianTest method test6_1.
// Selecting check boxes by their sibling labels
@Test(enabled = true)
public void test6_1() {
// Arrange
List<String> hobbies = new ArrayList<>(Arrays.asList("Singing", "Dancing"));
driver.get("http://suvian.in/selenium/1.6checkbox.html");
try {
WebElement checkElement = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver _driver) {
return _driver.findElements(By.cssSelector("div.container div.row div.intro-message h3")).stream().filter(_element -> _element.getText().toLowerCase().indexOf("select your hobbies") > -1).findFirst().get();
}
});
System.err.println("element check: " + checkElement.getAttribute("innerHTML"));
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
// Act
WebElement formElement = driver.findElement(By.cssSelector("input[id]")).findElement(By.xpath(".."));
assertThat(formElement, notNullValue());
highlight(formElement, 1000);
List<WebElement> inputElements = formElement.findElements(By.cssSelector("label[for]")).stream().filter(_element -> hobbies.contains(_element.getText())).collect(Collectors.toList());
// C#: dataMap = elements.ToDictionary(x => x.GetAttribute("for"), x =>
// x.Text);
Map<String, String> dataMap = inputElements.stream().map(_element -> {
System.err.println("input element id: " + _element);
System.err.println("input element text: " + _element.getText());
System.err.println("input element 'for' attribute: " + _element.getAttribute("for"));
System.err.println("input element HTML: " + _element.getAttribute("outerHTML"));
System.err.println("input element XPath: " + xpathOfElement(_element));
System.err.println("input element CSS: " + cssSelectorOfElement(_element));
return _element;
}).collect(Collectors.toMap(_element -> _element.getText(), _element -> _element.getAttribute("for")));
List<WebElement> checkboxes = new ArrayList<>();
for (String hobby : hobbies) {
try {
System.err.println("finding: " + dataMap.get(hobby));
checkboxes.add(formElement.findElement(// will throw exception
By.cssSelector(String.format("input#%s", dataMap.get(hobby)))));
} catch (InvalidSelectorException e) {
System.err.println("ignored: " + e.toString());
}
try {
checkboxes.add(formElement.findElement(// will not throw exception
By.xpath(String.format("input[@id='%s']", dataMap.get(hobby)))));
} catch (Exception e) {
System.err.println("ignored: " + e.toString());
}
}
Consumer<WebElement> act = _element -> {
highlight(_element);
_element.click();
};
checkboxes.stream().forEach(act);
// Assert
assertTrue(formElement.findElements(By.cssSelector("input[type='checkbox']")).stream().filter(o -> o.isSelected()).count() == hobbies.size());
}
Aggregations