Search in sources :

Example 1 with NgWebElement

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

the class NgDemoTest method testCustomerLogin.

@Test
public void testCustomerLogin() {
    // When I proceed to "Customer Login"
    NgWebElement element = ngDriver.findElement(NgBy.buttonText("Customer Login"));
    highlight(element);
    element.click();
    // And I find myself in the list of existing customers
    NgWebElement custId = ngDriver.findElement(NgBy.input("custId"));
    highlight(custId);
    custId.click();
    WebElement customer = custId.findElements(NgBy.repeater("cust in Customers")).stream().filter(c -> c.getText().indexOf("Hermoine Granger") >= 0).findFirst().get();
    // NOTE: works differently in firefox and chrome
    // customer.sendKeys(Keys.SPACE);
    customer.click();
    ngDriver.waitForAngular();
    // And I log in
    NgWebElement login = ngDriver.findElement(NgBy.buttonText("Login"));
    assertTrue(login.isEnabled());
    highlight(login);
    login.click();
    // Then I am greeted by my first and last name
    assertThat("greeting", ngDriver.findElement(NgBy.binding("user")).getText(), containsString("Hermoine Granger"));
    // And I see balance on one of my accounts
    NgWebElement ng_accountNo = ngDriver.findElement(NgBy.binding("accountNo"));
    assertThat("account number", ng_accountNo.getText(), anyOf(is("1001"), is("1002"), is("1003")));
    highlight(ng_accountNo);
    // And I open account
    ngDriver.findElements(NgBy.options("account for account in Accounts")).stream().filter(account -> account.getText().matches("1001")).collect(Collectors.toList()).get(0).click();
    // And I inspect transactions
    NgWebElement transactions_button = ngDriver.findElement(NgBy.partialButtonText("Transactions"));
    highlight(transactions_button);
    transactions_button.click();
    // wait until transactions are loaded
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("tx in transactions")).getWrappedElement()));
    // Then I can observe few transactions
    // to speed up test, limit to first 4
    int cnt = 0;
    for (WebElement tx : ngDriver.findElements(NgBy.repeater("tx in transactions"))) {
        if (cnt++ > 4) {
            break;
        }
        // view-source:http://www.way2automation.com/angularjs-protractor/banking/listTx.html
        NgWebElement ngTx = new NgWebElement(ngDriver, tx);
        // every transaction would be either Debit or Credit
        String txType = ngTx.evaluate("tx.type").toString();
        assertThat("transaction type", txType, anyOf(containsString("Debit"), containsString("Credit")));
        // it will have non zero amount
        int txAmount = Integer.parseInt(ngTx.evaluate("tx.amount").toString());
        assertThat("it will have non zero amount", txAmount, greaterThan(0));
        highlight(ngTx.findElement(NgBy.binding("tx.amount")));
        // its transaction dates should be the past
        try {
            Date txDate = dateFormat.parse(ngTx.evaluate("tx.date").toString());
            assertThat("transaction date", txDate, sameOrBefore(new Date()));
        } catch (ParseException e) {
        // ignore
        }
    }
    // perform some Credit transaction-specific checks
    // to speed up test, limit to first 3
    ngDriver.findElements(NgBy.repeaterColumn("tx in transactions", "tx.type")).stream().filter(txType -> !txType.getText().isEmpty()).filter(txType -> txType.getText().equalsIgnoreCase("Credit")).limit(3).forEach(txType -> highlight(txType));
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) BeforeClass(org.junit.BeforeClass) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) Date(java.util.Date) WebDriver(org.openqa.selenium.WebDriver) SimpleDateFormat(java.text.SimpleDateFormat) WebElement(org.openqa.selenium.WebElement) Assert.assertThat(org.junit.Assert.assertThat) Is.is(org.hamcrest.core.Is.is) ParseException(java.text.ParseException) Alert(org.openqa.selenium.Alert) Actions(org.openqa.selenium.interactions.Actions) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Dimension(org.openqa.selenium.Dimension) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Keys(org.openqa.selenium.Keys) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) NgBy(com.github.sergueik.jprotractor.NgBy) TimeUnit(java.util.concurrent.TimeUnit) AnyOf.anyOf(org.hamcrest.core.AnyOf.anyOf) NgWebDriver(com.github.sergueik.jprotractor.NgWebDriver) LocalDate(java.time.LocalDate) DateMatchers.sameOrBefore(org.exparity.hamcrest.date.DateMatchers.sameOrBefore) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ParseException(java.text.ParseException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 2 with NgWebElement

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

the class NgDragDropIntegrationTest method testDragAndDrop.

// @Ignore
@Test
public void testDragAndDrop() {
    NgWebElement ng_board = ngDriver.findElement(NgBy.model("kanbanBoard"));
    assertThat(ng_board, notNullValue());
    highlight(ng_board);
    WebElement column3 = ng_board.findElement(By.id("column3"));
    assertThat(column3, notNullValue());
    highlight(column3);
    NgWebElement ng_column3 = new NgWebElement(ngDriver, column3);
    // alternative search
    for (WebElement element : ngDriver.findElements(NgBy.repeater("column in kanbanBoard.columns"))) {
        System.err.println("id: " + element.getAttribute("id"));
        if (element.getAttribute("id").equalsIgnoreCase("column3")) {
            System.err.println("id: " + element.getAttribute("id") + " found");
            ng_column3 = new NgWebElement(ngDriver, element);
            highlight(element);
        }
    }
    WebElement source_card = null;
    WebElement target_card = null;
    for (WebElement card : ng_column3.findElement(NgBy.model("column.cards")).findElements(NgBy.repeater("card in column.cards"))) {
        if (card.getText().isEmpty()) {
            break;
        }
        if (card.getText().equalsIgnoreCase("Test user module")) {
            System.err.println("source card: \"" + card.getText() + "\" found");
            source_card = card;
        }
        if (card.getText().equalsIgnoreCase("CI for user module")) {
            System.err.println("target card: \"" + card.getText() + "\" found");
            target_card = card;
        }
    }
    highlight(source_card);
    Point source_card_location = source_card.getLocation();
    highlight(target_card);
    Point target_card_location = target_card.getLocation();
    actions.moveToElement(source_card).build().perform();
    // does not appear to work
    actions.dragAndDropBy(source_card, 0, target_card_location.y - source_card_location.y).build().perform();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    // works
    actions.clickAndHold(source_card).moveToElement(target_card).release().build().perform();
    ngDriver.waitForAngular();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) Point(org.openqa.selenium.Point) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Example 3 with NgWebElement

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

the class NgIgnoreSyncIntegrationTest method testNonAngular.

// @Ignore
@Test
public void testNonAngular() {
    if (isCIBuild) {
        return;
    }
    ngDriver.navigate().to("http://www.google.com");
    try {
        long startTime = System.currentTimeMillis();
        ngDriver.waitForAngular();
        // exception: window.angular is undefined
        long estimatedTime = System.currentTimeMillis() - startTime;
        System.err.println("waitForAngular: " + estimatedTime);
        NgWebElement element = ngDriver.findElement(By.cssSelector("input#gs_htif0"));
        element.getAttribute("id");
    } catch (TimeoutException exception) {
        System.err.println("TimeoutException thrown:");
        fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(exception);
        System.err.println("Exception thrown: " + fullStackTrace);
        return;
    }
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) TimeoutException(org.openqa.selenium.TimeoutException) Test(org.junit.Test)

Example 4 with NgWebElement

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

the class NgLocalFileIntegrationTest method testOrderByColumnField.

// @Ignore
@Test
public void testOrderByColumnField() {
    // Given there is a sorted data grid
    getPageContent("ng_headers_sort_example1.htm");
    String[] headers = new String[] { "First Name", "Last Name", "Age" };
    Hashtable<String, String> header_columns = new Hashtable<String, String>();
    // String[] sortOrders = new String[]{"descending", "ascending"};
    for (String header : headers) {
        for (int cnt = 0; cnt != 2; cnt++) {
            // When grid is sorted by <header> in <sort order>
            WebElement headerelement = ngDriver.findElement(By.xpath("//th/a[contains(text(),'" + header + "')]"));
            headerelement.click();
            // Then Angular uses the column <binding> to sort rows in <sort order>
            // order
            List<WebElement> rows = ngDriver.findElements(NgBy.repeater("emp in data.employees"));
            WebElement row = rows.get(0);
            String field = row.getAttribute("ng-order-by");
            NgWebElement ng_row = new NgWebElement(ngDriver, row);
            boolean reverseSort = parseBoolean(ng_row.evaluate("reverseSort").toString());
            // Without Protractor:
            // Then the data is sorted by <header> in <sort order>
            // String binding = header_columns.get(header);
            String binding = "emp." + ng_row.evaluate(field);
            System.err.println("Sorted by: " + field + ": " + binding + ", Reverse:	" + reverseSort);
            // Iterate over all rows, selected column
            for (WebElement emp : rows) {
                NgWebElement ng_emp = new NgWebElement(ngDriver, emp);
                try {
                    WebElement empFieldElement = ng_emp.findElement(NgBy.binding(binding));
                    assertThat(empFieldElement, notNullValue());
                    System.err.println(empFieldElement.getText());
                } catch (Exception e) {
                    System.err.println(org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e));
                }
            }
            // Compare selected column of first and last row
            WebElement first_row_cell = ngDriver.findElement(NgBy.repeaterElement("emp in data.employees", 0, binding));
            WebElement last_row_cell = ngDriver.findElement(NgBy.repeaterElement("emp in data.employees", rows.size() - 1, binding));
            System.err.println("First row cell: " + first_row_cell.getText());
            System.err.println("Last row cell: " + last_row_cell.getText());
            if (reverseSort) {
                System.err.println("Order: descending");
                System.err.println("Verified " + (first_row_cell.getText().compareTo(last_row_cell.getText()) > 0));
            } else {
                System.err.println("Order: ascending");
                System.err.println("Verified " + (first_row_cell.getText().compareTo(last_row_cell.getText()) < 0));
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) IOException(java.io.IOException) StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) Test(org.junit.Test)

Example 5 with NgWebElement

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

the class NgLocalFileIntegrationTest method testHover.

// @Ignore
@Test
public void testHover() {
    getPageContent("ng_hover1.htm");
    // Hover over menu
    WebElement element = seleniumDriver.findElement(By.id("hover"));
    highlight(element);
    actions.moveToElement(element).build().perform();
    // Wait for the Angular 'hovering' property to get evaluated to true
    WebDriverWait wait2 = new WebDriverWait(seleniumDriver, 120);
    wait2.pollingEvery(50, TimeUnit.MILLISECONDS);
    wait2.until(new ExpectedCondition<Boolean>() {

        // NOTE: 'until' only prints "hovering: true" and never "hovering: false"
        @Override
        public Boolean apply(WebDriver d) {
            NgWebDriver ngD = new NgWebDriver(d);
            // org.openqa.selenium.WebDriverException: The WebDriver instance must
            // implement JavaScriptExecutor interface.
            NgWebElement ng_element = ngD.findElement(By.id("hover"));
            Object value = ng_element.evaluate("hovering");
            System.err.println("hovering: " + value.toString());
            return parseBoolean(value.toString());
        }
    });
    actions.moveByOffset(0, 300).build().perform();
    // Wait for the tooltip div to get visible
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    actions.moveToElement(element).build().perform();
    try {
        wait.until(ExpectedConditions.visibilityOf(seleniumDriver.findElement(By.cssSelector("div[ng-show='hovering']"))));
    } catch (NoSuchElementException e) {
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) NgWebDriver(com.github.sergueik.jprotractor.NgWebDriver) NgWebDriver(com.github.sergueik.jprotractor.NgWebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) 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