Search in sources :

Example 16 with NgWebElement

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

the class NgWay2AutomationIntegrationTest method testDepositAndWithdraw.

// @Ignore
@Test
public void testDepositAndWithdraw() {
    if (isCIBuild) {
        return;
    }
    // customer login
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    // select customer with accounts
    assertThat(ngDriver.findElement(NgBy.input("custId")).getAttribute("id"), equalTo("userSelect"));
    for (WebElement currentCustomer : ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf("Harry Potter") >= 0) {
            System.err.println(currentCustomer.getText());
            currentCustomer.click();
        }
    }
    NgWebElement login = ngDriver.findElement(NgBy.buttonText("Login"));
    assertTrue(login.isEnabled());
    highlight(login);
    login.click();
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.options("account for account in Accounts")).getWrappedElement()));
    List<WebElement> accounts = ngDriver.findElements(NgBy.options("account for account in Accounts"));
    // pick a random account
    assertTrue(accounts.size() > 0);
    int account_idx = 1 + (int) (Math.random() * (accounts.size() - 1));
    String targetAccount = accounts.get(account_idx).getText();
    System.err.println(account_idx + " " + targetAccount);
    accounts.get(account_idx).click();
    int initialBalance = Integer.parseInt(ngDriver.findElement(NgBy.binding("amount")).getText());
    WebElement depositButton = ngDriver.findElements(NgBy.partialButtonText("Deposit")).get(0);
    assertTrue(depositButton.isDisplayed());
    depositButton.click();
    // deposit amount
    WebElement depositAmount = ngDriver.findElement(NgBy.model("amount"));
    highlight(depositAmount);
    depositAmount.sendKeys("100");
    // deposit the payment
    depositButton = ngDriver.findElements(NgBy.partialButtonText("Deposit")).get(1);
    assertTrue(depositButton.isDisplayed());
    depositButton.click();
    // Then we see the confirmation message
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.binding("message")).getWrappedElement()));
    NgWebElement message = ngDriver.findElement(NgBy.binding("message"));
    assertThat(message.getText(), containsString("Deposit Successful"));
    highlight(message);
    // And the balance changes
    int finalBalance = Integer.parseInt(ngDriver.findElement(NgBy.binding("amount")).getText());
    assertTrue(finalBalance == 100 + initialBalance);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    // switch to "Home" screen
    ngDriver.findElement(NgBy.buttonText("Home")).click();
    // customer login
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    for (WebElement currentCustomer : ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf("Harry Potter") >= 0) {
            System.err.println(currentCustomer.getText());
            currentCustomer.click();
        }
    }
    ngDriver.findElement(NgBy.buttonText("Login")).click();
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.options("account for account in Accounts")).getWrappedElement()));
    for (WebElement currentAccount : ngDriver.findElements(NgBy.options("account for account in Accounts"))) {
        if (currentAccount.getText().indexOf(targetAccount) >= 0) {
            System.err.println(currentAccount.getText());
            currentAccount.click();
        }
    }
    WebElement withdrawButton = ngDriver.findElement(NgBy.partialButtonText("Withdrawl"));
    assertTrue(withdrawButton.isDisplayed());
    withdrawButton.click();
    // When we withdraw a amount bigger then the balance on the account
    WebElement withdrawAmount = ngDriver.findElement(NgBy.model("amount"));
    highlight(withdrawAmount);
    withdrawAmount.sendKeys(String.format("%d", finalBalance + 10));
    withdrawButton = ngDriver.findElement(NgBy.buttonText("Withdraw"));
    withdrawButton.click();
    // We see warning that transaction failed
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.binding("message")).getWrappedElement()));
    message = ngDriver.findElement(NgBy.binding("message"));
    assertThat(message.getText(), containsString("Transaction Failed."));
    highlight(message);
    withdrawAmount.sendKeys(String.format("%d", finalBalance - 10));
    withdrawButton.click();
    // And the balance does not change
    finalBalance = Integer.parseInt(ngDriver.findElement(NgBy.binding("amount")).getText());
    assertTrue(finalBalance == 10);
}
Also used : NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Example 17 with NgWebElement

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

the class NgWay2AutomationIntegrationTest method testCustomerLogin.

/*
	 * 
	 * Feature: Login Feature
	 * 
	 * @test Scenario: Customer Login Given I go to the home page When I continue
	 * as "Customer Login" Then I should be able to choose my name from the list
	 * of existing customers
	 * 
	 * @test Scenario: Bank Manager Login Given I go to the home page When I
	 * continue as "Bank Manager Login" Then I should see "Add Customer" button
	 * And Ishould see "Open Account" button And I should see "Customers" button
	 * 
	 * @test Scenario Outline: Existing Customer Login Given I go to the home page
	 * When I continue as "Customer Login" And I choose myself "<FirstName>",
	 * "<LastName>" from existing customers And I log in Then I am greeted by
	 * "<FirstName>", "<LastName>" And I see balance on one of my accounts
	 * "<AccountNumbers>" And I can switch to any of my accounts
	 * "<AccountNumbers>" And I can not see any other accounts Examples: |
	 * AccountNumbers | FirstName | LastName | | 1004,1005,1006 | Harry | Potter |
	 */
// @Ignore
@Test
public void testCustomerLogin() {
    if (isCIBuild) {
        return;
    }
    // When I proceed to "Customer Login"
    NgWebElement element = ngDriver.findElement(NgBy.buttonText("Customer Login"));
    highlight(element);
    element.click();
    // And I choose my name from the list of existing customers
    element = ngDriver.findElement(NgBy.input("custId"));
    assertThat(element.getAttribute("id"), equalTo("userSelect"));
    highlight(element);
    for (WebElement currentCustomer : element.findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf("Harry Potter") >= 0) {
            System.err.println(currentCustomer.getText());
            actions.moveToElement(currentCustomer).build().perform();
            currentCustomer.click();
        }
    }
    // And I log in
    NgWebElement login = ngDriver.findElement(NgBy.buttonText("Login"));
    assertTrue(login.isEnabled());
    login.click();
    // Then I am greeted by my first and last name
    String user = ngDriver.findElement(NgBy.binding("user")).getText();
    // NOTE: the binding is {{user}}. It is composed from first and last name
    // surrounded with whitespace
    // confirm the greeting looks like a person's name
    assertTrue(user.matches("^(?:[^ ]+) +(?:[^ ]+)$"));
    // The greeting contains my first name
    assertThat(user, containsString("Harry"));
    // And I see balance on one of my accounts
    NgWebElement ng_accountNo = ngDriver.findElement(NgBy.binding("accountNo"));
    highlight(ng_accountNo);
    assertThat(ng_accountNo, notNullValue());
    // a valid account number
    String allCustomerAccountsAsString = "1004,1005,1006";
    String[] customerAccounts = allCustomerAccountsAsString.split(",");
    Pattern pattern = Pattern.compile("(" + StringUtils.join(customerAccounts, "|") + ")");
    Matcher matcher = pattern.matcher(ng_accountNo.getText());
    assertTrue(matcher.find());
    pattern = Pattern.compile("^(?!" + StringUtils.join(customerAccounts, "|") + ").*$");
    matcher = pattern.matcher(ng_accountNo.getText());
    assertFalse(matcher.find());
    // alternative to java.util.regex
    List<String> oneAcountArray = new ArrayList<>();
    oneAcountArray.add(ng_accountNo.getText());
    assertTrue(CollectionUtils.containsAny(oneAcountArray, new ArrayList<>(Arrays.asList(customerAccounts))));
    oneAcountArray.clear();
    WebElement balance = ngDriver.findElement(NgBy.binding("amount"));
    assertTrue(balance.getText().matches("^\\d+$"));
    highlight(balance);
    WebElement currency = ngDriver.findElement(NgBy.binding("currency"));
    assertTrue(currency.getText().matches("^(?:Dollar|Pound|Rupee)$"));
    highlight(currency);
    // And I can switch to every account owned
    List<String> avaliableAccounts = new ArrayList<>();
    for (WebElement account : ngDriver.findElements(NgBy.options("account for account in Accounts"))) {
        String otherAccountId = account.getText();
        assertTrue(otherAccountId.matches("^\\d+$"));
        // And I can not see any other accounts
        pattern = Pattern.compile("^(?!" + StringUtils.join(customerAccounts, "|") + ").*$");
        matcher = pattern.matcher(otherAccountId);
        assertFalse(matcher.find());
        avaliableAccounts.add(otherAccountId);
    }
    // And I can find every my account
    assertTrue(avaliableAccounts.containsAll(new HashSet<String>(Arrays.asList(customerAccounts))));
    ngDriver.findElements(NgBy.options("account for account in Accounts")).stream().forEach(currentAccount -> {
        System.err.println(currentAccount.getText());
        currentAccount.click();
    });
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with NgWebElement

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

the class NgWay2AutomationIntegrationTest method testOpenAccount.

// @Ignore
@Test
public void testOpenAccount() throws InterruptedException {
    if (isCIBuild) {
        return;
    }
    // bank manager login
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    ngDriver.findElement(NgBy.partialButtonText("Open Account")).click();
    // wait for customers info get loaded
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers")).getWrappedElement()));
    NgWebElement selectCustomer = ngDriver.findElement(NgBy.model("custId"));
    assertThat(selectCustomer.getAttribute("id"), containsString("userSelect"));
    List<WebElement> customers = new NgWebElement(ngDriver, selectCustomer).findElements(NgBy.repeater("cust in Customers"));
    // pick random customer to log in
    int random_customer_index = 1 + (int) (Math.random() * (customers.size() - 1));
    WebElement customer = customers.get(random_customer_index);
    String customerName = customer.getText();
    System.err.println(customerName);
    customer.click();
    NgWebElement ng_selectCurrencies = ngDriver.findElement(NgBy.model("currency"));
    actions.moveToElement(ng_selectCurrencies.getWrappedElement()).build().perform();
    highlight(ng_selectCurrencies.getWrappedElement());
    // use core Selenium
    Select selectCurrencies = new Select(ng_selectCurrencies.getWrappedElement());
    List<WebElement> accountCurrencies = selectCurrencies.getOptions();
    // select "Dollars"
    selectCurrencies.selectByVisibleText("Dollar");
    // add the account
    WebElement submitButton = ngDriver.getWrappedDriver().findElement(By.xpath("/html/body//form/button[@type='submit']"));
    assertThat(submitButton.getText(), containsString("Process"));
    actions.moveToElement(submitButton).build().perform();
    highlight(submitButton);
    submitButton.click();
    String newAccount = null;
    try {
        alert = seleniumDriver.switchTo().alert();
        String alert_text = alert.getText();
        assertThat(alert_text, containsString("Account created successfully with account Number"));
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(alert_text);
        if (matcher.find()) {
            newAccount = matcher.group(1);
            System.err.println("New account id " + newAccount);
        }
        // confirm the alert
        alert.accept();
    } catch (NoAlertPresentException ex) {
        // Alert not present
        System.err.println("NoAlertPresentException: " + ex.getStackTrace());
    // observed in Chrome. Ignore
    // return;
    } catch (WebDriverException ex) {
        // fullStackTrace =
        // org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(ex);
        // System.err.println("Alert was not handled by PhantomJS: " +
        // fullStackTrace);
        System.err.println("Alert was not handled by PhantomJS: " + ex.getStackTrace().toString());
        return;
    }
    // And I switch to "Home" screen
    NgWebElement ng_home = ngDriver.findElement(NgBy.buttonText("Home"));
    highlight(ng_home);
    ng_home.click();
    // And I proceed to "Bank Manager Login"
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    ngDriver.findElement(NgBy.partialButtonText("Customers")).click();
    // wait for customers info get loaded
    ngDriver.waitForAngular();
    // I can find the Customers Account I just Added
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers")).getWrappedElement()));
    // find the customer by name
    for (WebElement currentCustomer : ngDriver.findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf(customerName) >= 0) {
            // System.err.println("Current customer: " + currentCustomer.getText());
            highlight(currentCustomer);
            NgWebElement ng_currentCustomer = new NgWebElement(ngDriver, currentCustomer);
            // find the account we just added
            for (WebElement currentAccount : ng_currentCustomer.findElements(NgBy.repeater("account in cust.accountNo"))) {
                if (currentAccount.getText().indexOf(newAccount) >= 0) {
                    highlight(currentAccount);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) Select(org.openqa.selenium.support.ui.Select) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 19 with NgWebElement

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

the class NgWay2AutomationIntegrationTest method testAddCustomer.

// @Ignore
@SuppressWarnings("unchecked")
@Test
public void testAddCustomer() {
    if (isCIBuild) {
        return;
    }
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    ngDriver.findElement(NgBy.partialButtonText("Add Customer")).click();
    NgWebElement firstName = ngDriver.findElement(NgBy.model("fName"));
    assertThat(firstName.getAttribute("placeholder"), equalTo("First Name"));
    firstName.sendKeys("John");
    NgWebElement lastName = ngDriver.findElement(NgBy.model("lName"));
    assertThat(lastName.getAttribute("placeholder"), equalTo("Last Name"));
    lastName.sendKeys("Doe");
    NgWebElement postCode = ngDriver.findElement(NgBy.model("postCd"));
    assertThat(postCode.getAttribute("placeholder"), equalTo("Post Code"));
    postCode.sendKeys("11011");
    // NOTE: there are two 'Add Customer' buttons on this form
    Object[] addCustomerButtonElements = ngDriver.findElements(NgBy.partialButtonText("Add Customer")).toArray();
    WebElement addCustomerButtonElement = (WebElement) addCustomerButtonElements[1];
    addCustomerButtonElement.submit();
    try {
        alert = seleniumDriver.switchTo().alert();
    } catch (NoAlertPresentException ex) {
        // Alert not present
        System.err.println(ex.getStackTrace());
        return;
    } catch (WebDriverException ex) {
        // Alert not handled by PhantomJS
        System.err.println("Alert was not handled by PhantomJS: " + ex.getStackTrace());
        return;
    }
    Pattern pattern = Pattern.compile("Customer added successfully with customer id :(\\d+)");
    Matcher matcher = pattern.matcher(alert.getText());
    if (matcher.find()) {
        System.err.println("New Customer id: " + matcher.group(1));
    }
    // Actually add the customer
    alert.accept();
    // switch to "Customers" screen
    ngDriver.findElement(NgBy.partialButtonText("Customers")).click();
    // block and wait the customers list to get populated
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers"))));
    for (WebElement firstNameElement : ngDriver.findElements(NgBy.repeaterColumn("cust in Customers", "cust.fName"))) {
        actions.moveToElement(firstNameElement).build().perform();
        highlight(firstNameElement);
        System.err.println("Customer's First Name: " + firstNameElement.getText());
        // no need for explicit findElements
        Object objLastName = new NgWebElement(ngDriver, firstNameElement).evaluate("cust.lName");
        assertThat(objLastName, notNullValue());
        System.err.println("Customer's Last Name: " + objLastName.toString());
        // fName, lName, accountNo, postCD, id, date
        List<Long> accounts = (List<Long>) new NgWebElement(ngDriver, firstNameElement).evaluate("cust.accountNo");
        System.err.println(accounts == null ? "No accounts" : "Account No: " + Arrays.toString(accounts.toArray()));
    }
    NgWebElement ng_deleteCustomer = null;
    for (WebElement currentCustomer : ngDriver.findElements(NgBy.repeater("cust in Customers"))) {
        if (currentCustomer.getText().indexOf("John Doe") >= 0) {
            ng_deleteCustomer = new NgWebElement(ngDriver, currentCustomer);
            break;
        }
    }
    assertThat(ng_deleteCustomer.getWrappedElement(), notNullValue());
    actions.moveToElement(ng_deleteCustomer.getWrappedElement()).build().perform();
    // delete the customer we added
    highlight(ng_deleteCustomer);
    NgWebElement deleteCustomerButton = ng_deleteCustomer.findElement(NgBy.buttonText("Delete"));
    assertThat(deleteCustomerButton, notNullValue());
    assertThat(deleteCustomerButton.getText(), containsString("Delete"));
    highlight(deleteCustomerButton);
    // .. in slow motion
    actions.moveToElement(deleteCustomerButton.getWrappedElement()).clickAndHold().build().perform();
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
    }
    actions.release().build().perform();
    // let the customers reload
    wait.until(ExpectedConditions.visibilityOf(ngDriver.findElement(NgBy.repeater("cust in Customers"))));
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
    }
// TODO: assert the customers.count change
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) ArrayList(java.util.ArrayList) List(java.util.List) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 20 with NgWebElement

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

the class NgButtonIntegrationTest method testButtonStateText.

@Test
public void testButtonStateText() {
    // if (isCIBuild) { // Alert not handled by PhantomJS
    // return;
    // }
    getPageContent("ng_watch_ng_if.htm");
    WebElement button = seleniumDriver.findElement(By.cssSelector("button.btn"));
    ngDriver.waitForAngular();
    CommonFunctions.highlight(button);
    NgWebElement ng_status = ngDriver.findElement(NgBy.binding("house.frontDoor.isOpen"));
    CommonFunctions.highlight(ng_status);
    assertTrue(ng_status.getText().matches("The door is closed"));
    System.err.println("Initially: " + ng_status.getText());
    button.click();
    try {
        // confirm alert
        seleniumDriver.switchTo().alert().accept();
    } catch (NoAlertPresentException ex) {
    // Alert not present - ignore
    } catch (WebDriverException ex) {
        System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
        return;
    }
    ng_status = ngDriver.findElement(NgBy.binding("house.frontDoor.isOpen"));
    CommonFunctions.highlight(ng_status);
    System.err.println("Finally: " + ng_status.getText());
    assertThat(ng_status.getText(), matchesPattern(".+open"));
}
Also used : NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) NgWebElement(com.github.sergueik.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) 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