Search in sources :

Example 11 with NgWebElement

use of com.github.sergueik.jprotractor.NgWebElement in project selenium_java by sergueik.

the class NgLocalFileIntegrationTest method testFindSelectedtOptionWithAlert.

@Ignore
@Test
public void testFindSelectedtOptionWithAlert() {
    getPageContent("ng_selected_option.htm");
    List<WebElement> elements = ngDriver.findElements(NgBy.selectedOption("countSelected"));
    try {
        assertThat(elements.size(), equalTo(1));
    } catch (AssertionError e) {
        if (isCIBuild) {
            System.err.println("Skipped processing exception " + e.toString());
            return;
        } else {
            throw e;
        }
    }
    WebElement element = elements.get(0);
    ngDriver.waitForAngular();
    assertThat(element, notNullValue());
    assertTrue(element.isDisplayed());
    System.err.println("Selected option: " + element.getText() + "\n" + element.getAttribute("outerHTML"));
    assertThat(element.getText(), containsString("One"));
    for (WebElement option : ngDriver.findElements(NgBy.options("count.id as count.name for count in countList"))) {
        System.err.println("Available option: " + option.getText());
        if (option.getText().isEmpty()) {
            break;
        }
        if (option.getText().equalsIgnoreCase("two")) {
            System.err.println("Selecting option: " + option.getText());
            option.click();
            if (!isCIBuild) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }
            }
            if (!isCIBuild) {
                try {
                    alert = seleniumDriver.switchTo().alert();
                    String alert_text = alert.getText();
                    System.err.println("Accepted alert: " + alert_text);
                    alert.accept();
                } catch (NoAlertPresentException ex) {
                    System.err.println(ex.getStackTrace());
                    return;
                }
            }
        }
    }
    ngDriver.waitForAngular();
    elements = ngDriver.findElements(NgBy.selectedOption("countSelected"));
    try {
        assertThat(elements.size(), equalTo(1));
    } catch (AssertionError e) {
        if (isCIBuild) {
            System.err.println("Skipped processing exception " + e.toString());
            return;
        } else {
            throw e;
        }
    }
    element = elements.get(0);
    assertThat(element, notNullValue());
    System.err.println("Selected option: " + element.getText() + "\n" + element.getAttribute("outerHTML"));
    assertThat(element.getText(), containsString("Two"));
    WebElement countSelected = ngDriver.findElement(NgBy.binding("countSelected"));
    assertThat(countSelected, notNullValue());
    // System.err.println(countSelected.getText() );
    int valueOfCountSelected = Integer.parseInt(new NgWebElement(ngDriver, countSelected).evaluate("countSelected").toString());
    System.err.println("countSelected = " + valueOfCountSelected);
    assertThat(valueOfCountSelected, equalTo(2));
    if (!isCIBuild) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
    }
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with NgWebElement

use of com.github.sergueik.jprotractor.NgWebElement in project selenium_java by sergueik.

the class NgMultiSelectIntegrationTest method testSelectCarsOneByOne.

// @Ignore
@Test
public void testSelectCarsOneByOne() {
    // Given we are using multi select directive to pick cars
    NgWebElement ng_directive = ngDriver.findElement(NgBy.model("selectedCar"));
    assertThat(ng_directive, notNullValue());
    assertThat(ng_directive.getTagName(), equalTo("am-multiselect"));
    WebElement toggleSelect = ng_directive.findElement(NgBy.buttonText("Select Some Cars"));
    assertThat(toggleSelect, notNullValue());
    assertTrue(toggleSelect.isDisplayed());
    toggleSelect.click();
    // After selecting every car one car at a time
    List<WebElement> cars = ng_directive.findElements(NgBy.repeater("i in items"));
    // NOTE: not "c.name for c in cars" that one finds in the am-multiselect tag
    int selectedCarCount = 0;
    for (int rowNum = 0; rowNum != cars.size(); rowNum++) {
        NgWebElement ng_car = ngDriver.findElement(NgBy.repeaterElement("i in items", rowNum, "i.label"));
        System.err.println("* " + ng_car.evaluate("i.label").toString());
        CommonFunctions.highlight(ng_car);
        ng_car.click();
        selectedCarCount++;
    }
    // Until all cars are selected
    assertThat(ng_directive.findElements(NgBy.repeater("i in items")).size(), equalTo(selectedCarCount));
    // Then the button text shows the total number of cars selected
    WebElement button = ngDriver.findElement(By.cssSelector("am-multiselect > div > button"));
    String buttonTextPattern = "There are (\\d+) car\\(s\\) selected";
    String buttonText = button.getText();
    assertTrue(buttonText.matches(buttonTextPattern));
    System.err.println(buttonText);
    int carCount = 0;
    Pattern pattern = Pattern.compile(buttonTextPattern);
    Matcher matcher = pattern.matcher(buttonText);
    if (matcher.find()) {
        carCount = Integer.parseInt(matcher.group(1));
    }
    assertThat(carCount, equalTo(selectedCarCount));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Example 13 with NgWebElement

use of com.github.sergueik.jprotractor.NgWebElement in project selenium_java by sergueik.

the class NgQualityShepherdIntegrationTest method testAddFriend.

@Test
public void testAddFriend() {
    // When we add a friend
    String friendName = "John Doe";
    int friendCount = ngDriver.findElements(NgBy.repeater("row in rows")).size();
    NgWebElement addnameBox = ngDriver.findElement(NgBy.model("addName"));
    assertThat(addnameBox, notNullValue());
    highlight(addnameBox);
    addnameBox.sendKeys(friendName);
    NgWebElement addButton = ngDriver.findElement(NgBy.buttonText("+ add"));
    assertThat(addButton, notNullValue());
    highlight(addButton);
    addButton.click();
    ngDriver.waitForAngular();
    // Then there the total number of friends is increased by one
    assertThat(ngDriver.findElements(NgBy.repeater("row in rows")).size() - friendCount, equalTo(1));
    // And we can find the new friend using search
    WebElement addedFriendElement = ngDriver.findElements(NgBy.cssContainingText("td.ng-binding", friendName)).get(0);
    assertThat(addedFriendElement, notNullValue());
    highlight(addedFriendElement);
    System.err.println("Added friend name: " + addedFriendElement.getText());
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Example 14 with NgWebElement

use of com.github.sergueik.jprotractor.NgWebElement in project selenium_java by sergueik.

the class NgQualityShepherdIntegrationTest method testSearchAndDeleteFriend.

@Test
public void testSearchAndDeleteFriend() {
    // Given we pick friend to delete
    List<WebElement> friendNames = ngDriver.findElements(NgBy.repeaterColumn("row in rows", "row"));
    WebElement friendName = friendNames.get(0);
    highlight(friendName);
    String deleteFriendName = friendName.getText();
    assertFalse(deleteFriendName.isEmpty());
    // When we delete every friend with the chosen name
    for (WebElement currentfriendRow : ngDriver.findElements(NgBy.repeater("row in rows"))) {
        WebElement currentfriendName = new NgWebElement(ngDriver, currentfriendRow).findElement(NgBy.binding("row"));
        if (currentfriendName.getText().indexOf(deleteFriendName) >= 0) {
            System.err.println("Delete: " + currentfriendName.getText());
            WebElement deleteButton = currentfriendRow.findElement(By.cssSelector("i.icon-trash"));
            highlight(deleteButton);
            deleteButton.click();
            ngDriver.waitForAngular();
        }
    }
    // That the deleted friend's name cannot be found through search
    System.err.println("Search name: " + deleteFriendName);
    NgWebElement searchBox = ngDriver.findElement(NgBy.model("search"));
    searchBox.sendKeys(deleteFriendName);
    List<WebElement> rows = ngDriver.findElements(NgBy.repeater("row in rows"));
    assertTrue(rows.size() == 0);
    // clear search
    WebElement clearSearchBox = searchBox.findElement(By.xpath("..")).findElement(By.cssSelector("i.icon-remove"));
    assertThat(clearSearchBox, notNullValue());
    System.err.println("Clear Search");
    clearSearchBox.click();
    ngDriver.waitForAngular();
    // And the deleted friend cannot be found by looking at the remaining friend
    // names
    List<WebElement> elements = ngDriver.findElements(NgBy.cssContainingText("td.ng-binding", deleteFriendName));
    assertTrue(elements.size() == 0);
    // examine remaining friends
    for (WebElement currentFriendRow : ngDriver.findElements(NgBy.repeater("row in rows"))) {
        highlight(currentFriendRow);
        String currentFriendName = new NgWebElement(ngDriver, currentFriendRow).evaluate("row").toString();
        System.err.println("Found name: " + currentFriendName);
        assertTrue(currentFriendName.indexOf(deleteFriendName) == -1);
    }
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Example 15 with NgWebElement

use of com.github.sergueik.jprotractor.NgWebElement in project selenium_java by sergueik.

the class NgSvgIntegrationTest method testCircles.

@Test
public void testCircles() {
    // if (isCIBuild) { // Alert not handled by PhantomJS
    // return;
    // }
    getPageContent("ng_svg_ex1.htm");
    for (WebElement circle : ngDriver.findElements(NgBy.repeater("circle in circles"))) {
        // if (circle.getText().isEmpty()){
        // break;
        // }
        Object x = new NgWebElement(ngDriver, circle).evaluate("circle.x");
        Object y = new NgWebElement(ngDriver, circle).evaluate("circle.y");
        Object r = new NgWebElement(ngDriver, circle).evaluate("circle.r");
        highlight(circle);
        formatter.format("x = %1$2d y = %2$2d r = %3$2d\n", x, y, r);
        formatter.format("Location: x = %3d", circle.getLocation().x);
        formatter.format(" y = %3d", circle.getLocation().y);
        System.err.println(sb.toString());
        sb.setLength(0);
        try {
            WebElement element = seleniumDriver.findElement(By.xpath(xpath_of(circle)));
            System.err.println("Located by xpath " + xpath_of(circle));
            formatter.format("Location: x = %3d", element.getLocation().x);
            formatter.format(" y = %3d", element.getLocation().y);
            System.err.println(sb.toString());
            sb.setLength(0);
            highlight(element);
        } catch (NoSuchElementException ex) {
            // at <anonymous class>.FirefoxDriver.prototype.findElementInternal_
            System.err.println("Cannot locate by xpath: " + xpath_of(circle));
        }
        try {
            WebElement element = seleniumDriver.findElement(By.cssSelector(css_selector_of(circle)));
            System.err.println("Located by cssSelector " + css_selector_of(circle));
            highlight(element);
            System.err.println("innerHTML:" + element.getAttribute("innerHTML"));
            // WebDriverException: cannot forward the request Software caused
            // connection abort
            System.err.println("Fill: " + CommonFunctions.executeScript("return arguments[0].getAttribute('fill')", element));
        } catch (NoSuchElementException ex) {
            System.err.println("Cannot locate by cssSelector: " + css_selector_of(circle));
        }
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException) Test(org.junit.Test)

Aggregations

NgWebElement (com.github.sergueik.jprotractor.NgWebElement)44 Test (org.junit.Test)41 WebElement (org.openqa.selenium.WebElement)39 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)14 IOException (java.io.IOException)10 Pattern (java.util.regex.Pattern)10 Matcher (java.util.regex.Matcher)9 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)9 WebDriverException (org.openqa.selenium.WebDriverException)9 NgWebDriver (com.github.sergueik.jprotractor.NgWebDriver)8 Ignore (org.junit.Ignore)7 StaleElementReferenceException (org.openqa.selenium.StaleElementReferenceException)7 WebDriver (org.openqa.selenium.WebDriver)7 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)7 NoSuchElementException (org.openqa.selenium.NoSuchElementException)5 Dimension (org.openqa.selenium.Dimension)4 NgBy (com.github.sergueik.jprotractor.NgBy)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LocalDate (java.time.LocalDate)3