Search in sources :

Example 6 with ByChained

use of org.openqa.selenium.support.pagefactory.ByChained in project selenium_java by sergueik.

the class SuvianTest method test5_3.

// origin:
// https://www.programcreek.com/java-api-examples/index.php?api=org.openqa.selenium.support.pagefactory.ByChained
// https://www.javatips.net/api/org.openqa.selenium.support.pagefactory.bychained
@Test(enabled = true)
public void test5_3() {
    // Arrange
    driver.get("http://suvian.in/selenium/1.5married_radio.html");
    String label = "yes";
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector(".container .row .intro-message h3 a"))));
    WebElement formElement = driver.findElement(By.cssSelector(".intro-header .container .row .col-lg-12 .intro-message form"));
    String elementContents = formElement.getAttribute("outerHTML");
    String line = new ArrayList<String>(Arrays.asList(elementContents.split("<br/?>"))).stream().filter(o -> o.toLowerCase().indexOf(label) > -1).findFirst().get();
    Matcher matcher = Pattern.compile("value=\\\"([^\"]*)\\\"").matcher(line);
    String checkboxValue = null;
    if (matcher.find()) {
        checkboxValue = matcher.group(1);
        System.err.println("checkbox value = " + checkboxValue);
    } else {
        System.err.println("checkbox value not found");
    }
    WebElement checkBoxElement = null;
    if (checkboxValue != null) {
        ByChained byChained = new ByChained(By.xpath("//body/*"), By.cssSelector(".intro-header .container .row .col-lg-12 .intro-message form"), By.cssSelector(String.format("input[name='married'][value='%s']", checkboxValue)));
        checkBoxElement = driver.findElements(byChained).get(0);
    }
    // Act
    assertThat(checkBoxElement, notNullValue());
    highlight(checkBoxElement);
    checkBoxElement.click();
    sleep(500);
    // Assert
    assertTrue(checkBoxElement.isSelected());
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Test(org.testng.annotations.Test)

Aggregations

WebElement (org.openqa.selenium.WebElement)6 ByChained (org.openqa.selenium.support.pagefactory.ByChained)6 NgWebElement (com.github.sergueik.jprotractor.NgWebElement)3 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 Enumeration (java.util.Enumeration)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2