Search in sources :

Example 1 with ByChained

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

the class NgScrollableIntegrationTest method testRowColumnsWithByChained.

@Ignore
@Test
public void testRowColumnsWithByChained() {
    // Wait page to load
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(By.className("table-container"))));
    List<WebElement> nameRows = ngDriver.findElements(new ByChained(NgBy.repeater("row in rowCollection"), NgBy.binding("row.firstName")));
    rowCnt = 0;
    for (WebElement rowElement : nameRows) {
        if (rowCnt++ > maxRows) {
            break;
        }
        try {
            if (!rowElement.isDisplayed()) {
                break;
            }
            highlight(rowElement);
            System.err.println(String.format("First name in row %d: %s ", rowCnt, rowElement.getText()));
        } catch (StaleElementReferenceException e) {
            System.err.println("Exception(ignored) " + e.toString());
        }
    }
}
Also used : StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ByChained

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

the class NgScrollableIntegrationTest method testCountRows.

// @Ignore
@Test
public void testCountRows() {
    // Wait page to load
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(By.className("table-container"))));
    List<WebElement> rows = ngDriver.findElements(NgBy.repeater("row in rowCollection"));
    List<WebElement> lastNameRows = ngDriver.findElements(new ByChained(By.className("table-container"), NgBy.repeater("row in rowCollection"), NgBy.binding("row.lastName")));
    System.err.println("Rows: " + rows.size());
    System.err.println("Last names: " + lastNameRows.size());
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Test(org.junit.Test)

Example 3 with ByChained

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

the class SuvianTest method test41.

// debugging map inspired by the ByChained
@Test(enabled = true)
public void test41() {
    // Arrange
    driver.get("https://www.virtuosoft.eu/code/bootstrap-autohidingnavbar/");
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("[class = 'table table-striped table-bordered docs']"))));
    WebElement element = driver.findElements(By.cssSelector("[class = 'table table-striped table-bordered docs']")).get(1);
    actions.moveToElement(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
    actions.moveToElement(element).sendKeys(Keys.ARROW_UP).sendKeys(Keys.ARROW_UP).sendKeys(Keys.ARROW_UP).sendKeys(Keys.ARROW_UP).build().perform();
    // Act
    element = driver.findElements(By.cssSelector("li")).get(12);
    wait.until(ExpectedConditions.elementToBeClickable(element));
    highlight(element);
    // Assert
    assertThat(element.getText(), equalTo("BLOG"));
    element = driver.findElements(By.cssSelector("div.navbar-fixed-top div.navbar-collapse li a[href='/blog/']")).get(0);
    wait.until(ExpectedConditions.elementToBeClickable(element));
    highlight(element);
    // Assert
    assertThat(element.getText(), equalTo("BLOG"));
    // Assert
    element = driver.findElements(new ByChained(By.cssSelector("body"), By.cssSelector("div.navbar"), By.cssSelector("div.navbar-collapse"), By.cssSelector("li"), By.xpath(String.format("//a[contains(text()[normalize-space()],'%s')]", "Blog")))).get(0);
    wait.until(ExpectedConditions.elementToBeClickable(element));
    highlight(element);
    // Assert
    assertThat(element.getText(), equalTo("BLOG"));
    element = driver.findElements(new ByChained(By.cssSelector("body"), By.cssSelector("div.navbar-fixed-top"), By.cssSelector("div.navbar-collapse"), By.cssSelector("li"))).stream().filter(o -> o.findElements(By.cssSelector("a")).size() > 0).map(o -> {
        System.err.println(o.getAttribute("innerHTML"));
        return o;
    }).filter(o -> o.findElements(By.xpath(String.format("//a[contains(text()[normalize-space()],'%s')]", "Blog"))).size() > 0).map(o -> o.findElements(By.xpath(String.format("//a[contains(text()[normalize-space()],'%s')]", "Blog"))).get(0)).collect(Collectors.toList()).get(0);
    wait.until(ExpectedConditions.elementToBeClickable(element));
    highlight(element);
    // Assert
    assertThat(element.getText(), equalTo("BLOG"));
    element.click();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) WebElement(org.openqa.selenium.WebElement) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) StringUtils(org.apache.commons.lang3.StringUtils) Locatable(org.openqa.selenium.internal.Locatable) Coordinates(org.openqa.selenium.interactions.internal.Coordinates) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Matcher(java.util.regex.Matcher) Point(org.openqa.selenium.Point) Map(java.util.Map) Method(java.lang.reflect.Method) HasInputDevices(org.openqa.selenium.interactions.HasInputDevices) FindBy(org.openqa.selenium.support.FindBy) SoftAssert(org.testng.asserts.SoftAssert) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Predicate(java.util.function.Predicate) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Mouse(org.openqa.selenium.interactions.Mouse) Collectors(java.util.stream.Collectors) FindBys(org.openqa.selenium.support.FindBys) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) ITestResult(org.testng.ITestResult) ArrayList(java.util.ArrayList) Select(org.openqa.selenium.support.ui.Select) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Iterator(java.util.Iterator) Keys(org.openqa.selenium.Keys) By(org.openqa.selenium.By) Consumer(java.util.function.Consumer) Assert.assertTrue(org.testng.Assert.assertTrue) Comparator(java.util.Comparator) Collections(java.util.Collections) WebElement(org.openqa.selenium.WebElement) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Test(org.testng.annotations.Test)

Example 4 with ByChained

use of org.openqa.selenium.support.pagefactory.ByChained 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());
}
Also used : WebDriver(org.openqa.selenium.WebDriver) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) WebElement(org.openqa.selenium.WebElement) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) StringUtils(org.apache.commons.lang3.StringUtils) Locatable(org.openqa.selenium.internal.Locatable) Coordinates(org.openqa.selenium.interactions.internal.Coordinates) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Matcher(java.util.regex.Matcher) Point(org.openqa.selenium.Point) Map(java.util.Map) Method(java.lang.reflect.Method) HasInputDevices(org.openqa.selenium.interactions.HasInputDevices) FindBy(org.openqa.selenium.support.FindBy) SoftAssert(org.testng.asserts.SoftAssert) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Predicate(java.util.function.Predicate) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Mouse(org.openqa.selenium.interactions.Mouse) Collectors(java.util.stream.Collectors) FindBys(org.openqa.selenium.support.FindBys) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) ITestResult(org.testng.ITestResult) ArrayList(java.util.ArrayList) Select(org.openqa.selenium.support.ui.Select) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Iterator(java.util.Iterator) Keys(org.openqa.selenium.Keys) By(org.openqa.selenium.By) Consumer(java.util.function.Consumer) Assert.assertTrue(org.testng.Assert.assertTrue) Comparator(java.util.Comparator) Collections(java.util.Collections) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriverException(org.openqa.selenium.WebDriverException) InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Test(org.testng.annotations.Test)

Example 5 with ByChained

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

the class NgScrollableIntegrationTest method testEvaluateRowData.

@Ignore
@Test
public void testEvaluateRowData() {
    // Wait page to load
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(By.className("table-container"))));
    List<WebElement> nameRows = ngDriver.findElements(new ByChained(NgBy.repeater("row in rowCollection"), NgBy.binding("row.firstName")));
    rowCnt = 0;
    for (WebElement rowElement : nameRows) {
        if (rowCnt++ > maxRows) {
            break;
        }
        NgWebElement ngRowElement = new NgWebElement(ngDriver, rowElement);
        System.err.println(String.format("Row %d Name: %s %s", rowCnt, (String) ngRowElement.evaluate("row.firstName"), (String) ngRowElement.evaluate("row.lastName")));
    }
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) ByChained(org.openqa.selenium.support.pagefactory.ByChained) Ignore(org.junit.Ignore) Test(org.junit.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