Search in sources :

Example 1 with NgWebElement

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

the class AppTest method testInvitateNewCustomerToOpenAccount.

@Test
public void testInvitateNewCustomerToOpenAccount() throws Exception {
    ngDriver.navigate().to(urlBanking);
    // When I proceed to "Bank Manager Login"
    ngDriver.findElement(NgBy.buttonText("Bank Manager Login")).click();
    // And I proceed to "Add Customer"
    ngDriver.findElement(NgBy.partialButtonText("Add Customer")).click();
    NgWebElement ng_firstName = ngDriver.findElement(NgBy.model("fName"));
    assertThat(ng_firstName.getAttribute("placeholder"), equalTo("First Name"));
    ng_firstName.sendKeys("John");
    NgWebElement ng_lastName = ngDriver.findElement(NgBy.model("lName"));
    assertThat(ng_lastName.getAttribute("placeholder"), equalTo("Last Name"));
    ng_lastName.sendKeys("Doe");
    NgWebElement ng_postCode = ngDriver.findElement(NgBy.model("postCd"));
    assertThat(ng_postCode.getAttribute("placeholder"), equalTo("Post Code"));
    ng_postCode.sendKeys("11011");
    // And I add no accounts
    // NOTE: there are two 'Add Customer' buttons on this form
    WebElement ng_addCustomerButtonElement = ngDriver.findElements(NgBy.partialButtonText("Add Customer")).get(1);
    ng_addCustomerButtonElement.submit();
    try {
        alert = seleniumDriver.switchTo().alert();
        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));
        }
        // confirm alert
        alert.accept();
    } 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;
    }
    // And I switch to "Home" screen
    NgWebElement ng_home = ngDriver.findElement(NgBy.buttonText("Home"));
    highlight(ng_home);
    ng_home.click();
    // And I proceed to "Customer Login"
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    // And I login as "John Doe"
    Enumeration<WebElement> customers = Collections.enumeration(ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers")));
    while (customers.hasMoreElements()) {
        WebElement currentCustomer = customers.nextElement();
        if (currentCustomer.getText().indexOf("John Doe") >= 0) {
            System.err.println(currentCustomer.getText());
            currentCustomer.click();
        }
    }
    actions.moveToElement(ngDriver.findElement(NgBy.model("custId")).getWrappedElement()).build().perform();
    NgWebElement ng_login = ngDriver.findElement(NgBy.buttonText("Login"));
    assertTrue(ng_login.isEnabled());
    actions.moveToElement(ng_login.getWrappedElement()).build().perform();
    highlight(ng_login);
    ng_login.click();
    ngDriver.waitForAngular();
    // Then I am greeted as "John Doe"
    NgWebElement ng_user = ngDriver.findElement(NgBy.binding("user"));
    assertThat(ng_user.getText(), containsString("John"));
    assertThat(ng_user.getText(), containsString("Doe"));
    // And I am invited to open an account
    Object noAccount = ng_user.evaluate("noAccount");
    assertTrue(parseBoolean(noAccount.toString()));
    boolean hasAccounts = !(parseBoolean(noAccount.toString()));
    System.err.println("Has accounts: " + hasAccounts);
    WebElement message = seleniumDriver.findElement(By.cssSelector("span[ng-show='noAccount']"));
    actions.moveToElement(message).build().perform();
    assertTrue(message.isDisplayed());
    highlight(message);
    assertThat(message.getText(), containsString("Please open an account"));
    System.err.println(message.getText());
    // And I see no accounts
    NgWebElement accountNo = ngDriver.findElement(NgBy.binding("accountNo"));
    assertFalse(accountNo.isDisplayed());
    // And I cannot choose an account to view
    List<WebElement> accounts = ngDriver.findElements(NgBy.repeater("account for account in Accounts"));
    assertThat(accounts.size(), equalTo(0));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) NgWebElement(com.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) WebDriverException(org.openqa.selenium.WebDriverException) Test(org.junit.Test)

Example 2 with NgWebElement

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

the class AppTest method testSelectAll.

@Test
public void testSelectAll() throws Exception {
    ngDriver.navigate().to(urlMultiSelect);
    // Given we are using multiselect directive to pick cars
    NgWebElement ng_directive = ngDriver.findElement(NgBy.model("selectedCar"));
    assertThat(ng_directive, notNullValue());
    assertTrue(ng_directive.getTagName().equals("am-multiselect"));
    // When selecting cars
    WebElement toggleSelect = ng_directive.findElement(By.cssSelector("button[ng-click='toggleSelect()']"));
    assertThat(toggleSelect, notNullValue());
    assertTrue(toggleSelect.isDisplayed());
    toggleSelect.click();
    // When selected all cars using 'check all' link
    wait.until(ExpectedConditions.visibilityOf(ng_directive.findElement(By.cssSelector("button[ng-click='checkAll()']"))));
    WebElement checkAll = ng_directive.findElement(By.cssSelector("button[ng-click='checkAll()']"));
    assertThat(checkAll, notNullValue());
    assertTrue(checkAll.isDisplayed());
    highlight(checkAll);
    checkAll.click();
    // Then all cars were selected
    // NOTE: not "c.name for c in cars"
    List<WebElement> cars = ng_directive.findElements(NgBy.repeater("i in items"));
    int selectedCarCount = 0;
    for (WebElement car : cars) {
        assertTrue((boolean) (new NgWebElement(ngDriver, car).evaluate("i.checked")));
        System.err.println("* " + car.getText());
        selectedCarCount++;
    }
    assertThat(cars.size(), equalTo(selectedCarCount));
}
Also used : NgWebElement(com.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) Test(org.junit.Test)

Example 3 with NgWebElement

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

the class AppTest method testSelectOneByOne.

@Test
public void testSelectOneByOne() throws Exception {
    ngDriver.navigate().to(urlMultiSelect);
    // Given we are using multiselect 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();
    // When selecting every car one car at a time
    // find how many cars there are
    List<WebElement> cars = ng_directive.findElements(NgBy.repeater("i in items"));
    // NOTE: not "c.name for c in cars"
    int rowNum = 0;
    for (rowNum = 0; rowNum != cars.size(); rowNum++) {
        // For every row, click on the car name
        NgWebElement ng_car = ngDriver.findElement(NgBy.repeaterElement("i in items", rowNum, "i.label"));
        System.err.println("* " + ng_car.evaluate("i.label").toString());
        highlight(ng_car);
        ng_car.click();
    }
    // Then all cars got selected
    assertThat(ng_directive.findElements(NgBy.repeater("i in items")).size(), equalTo(rowNum));
    // TODO: verify the number of cars on the button
    WebElement button = ngDriver.findElement(By.cssSelector("am-multiselect > div > button"));
    assertTrue(button.getText().matches("There are (\\d+) car\\(s\\) selected"));
    System.err.println(button.getText());
}
Also used : NgWebElement(com.jprotractor.NgWebElement) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) Test(org.junit.Test)

Example 4 with NgWebElement

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

the class AppTest method testCustomerLogin.

/*
	 * 
	 * Feature: Login Feature
	 * 
	 * @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() throws Exception {
    ngDriver.navigate().to(urlBanking);
    // 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);
    Enumeration<WebElement> customers = Collections.enumeration(element.findElements(NgBy.repeater("cust in Customers")));
    while (customers.hasMoreElements()) {
        WebElement currentCustomer = customers.nextElement();
        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());
    actions.moveToElement(element.getWrappedElement()).build().perform();
    actions.moveToElement(login.getWrappedElement()).build().perform();
    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"));
    actions.moveToElement(ng_accountNo.getWrappedElement()).build().perform();
    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());
		 */
    WebElement balance = ngDriver.findElement(NgBy.binding("amount"));
    // actions.moveToElement(balance.getWrappedElement()).build().perform();
    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 any of my accounts
    List<String> avaliableAccounts = new ArrayList<>();
    Enumeration<WebElement> options = Collections.enumeration(ngDriver.findElements(NgBy.options("account for account in Accounts")));
    while (options.hasMoreElements()) {
        String otherAccount = options.nextElement().getText();
        assertTrue(otherAccount.matches("^\\d+$"));
        // And I can not see any other accounts
        /*
			 * pattern = Pattern.compile("^(?!" + StringUtils.join(customerAccounts,
			 * "|") + ").*$"); matcher = pattern.matcher(otherAccount);
			 * assertFalse(matcher.find());
			 */
        avaliableAccounts.add(otherAccount);
    }
    assertTrue(avaliableAccounts.containsAll(new HashSet<String>(Arrays.asList(customerAccounts))));
    Enumeration<WebElement> accounts = Collections.enumeration(ngDriver.findElements(NgBy.options("account for account in Accounts")));
    while (accounts.hasMoreElements()) {
        WebElement currentAccount = accounts.nextElement();
        System.err.println(currentAccount.getText());
        currentAccount.click();
    }
}
Also used : ArrayList(java.util.ArrayList) NgWebElement(com.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with NgWebElement

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

the class AppTest method testDepositAndWithdraw.

// @Ignore
@Test
public void testDepositAndWithdraw() throws Exception {
    ngDriver.navigate().to(urlBanking);
    // customer login
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    // select customer with accounts
    assertThat(ngDriver.findElement(NgBy.input("custId")).getAttribute("id"), equalTo("userSelect"));
    Enumeration<WebElement> customers = Collections.enumeration(ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers")));
    while (customers.hasMoreElements()) {
        WebElement currentCustomer = customers.nextElement();
        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);
    Thread.sleep(500);
    // switch to "Home" screen
    ngDriver.findElement(NgBy.buttonText("Home")).click();
    // customer login
    ngDriver.findElement(NgBy.buttonText("Customer Login")).click();
    // find the same customer / account
    customers = Collections.enumeration(ngDriver.findElement(NgBy.model("custId")).findElements(NgBy.repeater("cust in Customers")));
    while (customers.hasMoreElements()) {
        WebElement currentCustomer = customers.nextElement();
        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()));
    Enumeration<WebElement> accounts2 = Collections.enumeration(ngDriver.findElements(NgBy.options("account for account in Accounts")));
    while (accounts2.hasMoreElements()) {
        WebElement currentAccount = accounts2.nextElement();
        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.jprotractor.NgWebElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WebElement(org.openqa.selenium.WebElement) NgWebElement(com.jprotractor.NgWebElement) Test(org.junit.Test)

Aggregations

NgWebElement (com.jprotractor.NgWebElement)8 WebElement (org.openqa.selenium.WebElement)8 Test (org.junit.Test)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)3 WebDriverException (org.openqa.selenium.WebDriverException)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 IOException (java.io.IOException)1 RuntimeException (java.lang.RuntimeException)1 BindException (java.net.BindException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NoSuchElementException (org.openqa.selenium.NoSuchElementException)1 StaleElementReferenceException (org.openqa.selenium.StaleElementReferenceException)1 Select (org.openqa.selenium.support.ui.Select)1 Test (org.testng.annotations.Test)1