Search in sources :

Example 1 with InvalidSelectorException

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());
}
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 2 with InvalidSelectorException

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;
    }
}
Also used : InvalidSelectorException(org.openqa.selenium.InvalidSelectorException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) Point(org.openqa.selenium.Point) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.testng.annotations.Test)

Example 3 with InvalidSelectorException

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());
}
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) Test(org.testng.annotations.Test)

Aggregations

CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 InvalidSelectorException (org.openqa.selenium.InvalidSelectorException)3 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)3 Point (org.openqa.selenium.Point)3 WebDriverException (org.openqa.selenium.WebDriverException)3 WebElement (org.openqa.selenium.WebElement)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)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