budget.WebDriverManager.java Source code

Java tutorial

Introduction

Here is the source code for budget.WebDriverManager.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package budget;

import static budget.util.*;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.TextArea;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 *
 * @author aanpilogov
 */
public class WebDriverManager {

    private static WebDriver driver;
    private static WebDriverWait wait;
    private static final int timeout = 30;
    private static Statement stmt;
    private static TextArea taLog;
    private static final int sleepTime = 1000;
    private static Map<String, List<String>> hmCredentials;

    public static void refreshAccounts(Statement statement, TextArea textarea) {
        stmt = statement;
        taLog = textarea;

        trace("begin downloading...", taLog);
        initializeCredentialsMap();
        refreshAccountsAmEx();
        refreshAccountsWF();
        trace("finish downloading", taLog);
    }

    private static void refreshAccountsWF() {
        int attempts = 3;
        int attempt = 1;
        Boolean isDownloaded = false;
        List<String> credentials = getCredentials(4);

        while ((attempt <= attempts) && !isDownloaded) {
            try {
                driver = new HtmlUnitDriver(true);
                wait = new WebDriverWait(driver, timeout);
                java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit")
                        .setLevel(java.util.logging.Level.OFF);
                java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);

                driver.get("Http://www.wellsfargo.com");
                driver.findElement(By.id("userid")).clear();
                driver.findElement(By.id("userid")).sendKeys(credentials.get(0));
                driver.findElement(By.id("password")).clear();
                driver.findElement(By.id("password")).sendKeys(credentials.get(1));
                driver.findElement(By.id("btnSignon")).click();
                waitAndClick(driver, wait, By.xpath("//th[@id='cashAccount1']/a"));

                runQueryWF(driver, wait, 4);
                runQueryWF(driver, wait, 5);
                runQueryWF(driver, wait, 6);

                driver.findElement(By.linkText("Sign Off")).click();
                driver.close();
                driver.quit();

                isDownloaded = true;
            } catch (Exception ex) {
                driver.quit();
                trace(ex.getMessage(), taLog);
                clearBatch(stmt);
                attempt++;
            }
            ;
        }
        trace((isDownloaded ? "Downloaded" : "Not downloaded") + " from " + attempt
                + ((attempt == 1) ? " attempt" : " attemps"), taLog);

        executeBatch(stmt, taLog);
    }

    private static void runQueryWF(WebDriver driver, WebDriverWait wait, int account) {
        String date;
        String Analytics;
        String Category;
        Double amount;
        String accountName = getAccountName(stmt, account);

        //Selecting and wait for certain account page
        if (account != 4) {
            new Select(driver.findElement(By.id("accountDropdown"))).selectByIndex(account - 4);
            driver.findElement(By.name("accountselection")).click();
        }
        wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.className("moremarginbottom")),
                accountName));

        //Gatherng total amount
        if (account == 6) {
            String limitStr = driver.findElement(By.xpath("//table[@id='balancedetailstable']/tbody/tr/td"))
                    .getText();
            String balanceStr = driver.findElement(By.xpath("//table[@id='balancedetailstable']/tbody/tr[3]/td"))
                    .getText();
            //amount = convertStringAmountToDouble(balanceStr) - convertStringAmountToDouble(limitStr);
            amount = convertStringAmountToDouble(balanceStr); //just for SECURE CARD
        } else {
            amount = convertStringAmountToDouble(
                    driver.findElement(By.className("availableBalanceTotalAmount")).getText());
        }
        addTotal(stmt, getTimestamp(), account, amount, taLog);

        //Gathering transactinos
        if (account == 6) {
            String strAmount;
            driver.findElement(By.id("a.creditcardtempauth")).click();
            waitForElement(wait, By.xpath("//tbody[@id='tempAuthSection']/tr"));
            List<WebElement> rows = driver.findElements(By.xpath("//tbody[@id='tempAuthSection']/tr"));
            for (WebElement row : rows) {
                if (!"You have no temporary authorizations for this account.".equals(row.getText())) {
                    date = rotateDate(row.findElement(By.className("rowhead")).getText(), "mm/dd/yy");
                    Analytics = prepareTextForQuery(row.findElement(By.className("text")).getText());
                    strAmount = row.findElements(By.className("amount")).get(0).getText();
                    if ("+".equals(strAmount.substring(0, 1))) {
                        amount = convertStringAmountToDouble(strAmount);
                    } else {
                        amount = -convertStringAmountToDouble(strAmount);
                    }
                    addTransaction(true, stmt, date, account, "", Analytics, amount, true, taLog);
                }
            }
            rows = driver.findElements(By.xpath("//table[@id='CreditCardTransactionTable']/tbody[4]/tr"));
            for (WebElement row : rows) {
                if (row.findElements(By.className("rowhead")).size() == 1) {
                    if (!"".equals(row.findElement(By.className("rowhead")).getText())) {
                        date = rotateDate(row.findElement(By.className("rowhead")).getText(), "mm/dd/yy");
                        WebElement cell = row.findElement(By.className("text"));
                        Analytics = prepareTextForQuery(cell.findElement(By.xpath("span")).getText());
                        strAmount = row.findElements(By.className("amount")).get(0).getText();
                        if ("+".equals(strAmount.substring(0, 1))) {
                            amount = convertStringAmountToDouble(strAmount);
                        } else {
                            amount = -convertStringAmountToDouble(strAmount);
                        }
                        if (amount > 0) {
                            Category = "";
                        } else {
                            cell.findElement(By.className("detailsSection")).click();
                            waitForElement(wait, By.xpath("//table[@id='expandCollapseTable']/tbody/tr[2]/td[2]"));
                            Category = cell
                                    .findElement(By.xpath("//table[@id='expandCollapseTable']/tbody/tr[2]/td[2]"))
                                    .getText();
                        }
                        addTransaction(true, stmt, date, account, Category, Analytics, amount, false, taLog);
                    }
                }
            }
        } else {
            driver.findElement(By.id("timeFilter")).click();
            new Select(driver.findElement(By.id("timeFilter"))).selectByVisibleText("Date Range");
            waitAndClick(driver, wait, By.name("Submit"));

            int itemsOnPage;
            if (driver.findElements(By.className("emdisclosure")).size() == 1) {
                itemsOnPage = Integer.parseInt(driver.findElement(By.className("emdisclosure")).getText()
                        .replace("(", "").replace(" transactions per page)", ""));
            } else {
                itemsOnPage = 0;
            }
            int itemsCounter;
            Boolean isPending = false;
            List<WebElement> lstAmounts;
            do {
                itemsCounter = 0;
                List<WebElement> rows = driver
                        .findElements(By.xpath("//table[@id='DDATransactionTable']/tbody/tr"));
                for (WebElement row : rows) {
                    if (row.findElement(By.className("text")).getText().contains("Pending transactions")) {
                        isPending = true;
                    }
                    if (row.findElement(By.className("text")).getText().contains("Posted Transactions")) {
                        isPending = false;
                    }

                    if (row.findElements(By.className("rowhead")).size() == 1) {
                        date = rotateDate(row.findElement(By.className("rowhead")).getText(), "mm/dd/yy");
                        Analytics = prepareTextForQuery(row.findElement(By.className("text")).getText());
                        lstAmounts = row.findElements(By.className("amount"));
                        if (!" ".equals(lstAmounts.get(0).getText())) {
                            amount = convertStringAmountToDouble(lstAmounts.get(0).getText());
                        }
                        if (!" ".equals(lstAmounts.get(1).getText())) {
                            amount = -convertStringAmountToDouble(lstAmounts.get(1).getText());
                        }

                        addTransaction(true, stmt, date, account, "", Analytics, amount, isPending, taLog);
                        itemsCounter++;
                    }
                }
                if (itemsCounter == itemsOnPage) {
                    driver.findElement(By.linkText("Next >")).click();
                }
            } while (itemsCounter == itemsOnPage);
        }
    }

    private static void refreshAccountsAmEx() {
        int attempts = 3;
        int attempt = 1;
        Boolean isDownloaded = false;
        List<String> credentials = getCredentials(7);

        while ((attempt <= attempts) && !isDownloaded) {
            try {
                driver = new FirefoxDriver();
                driver.manage().window().setPosition(new Point(-2000, 0));
                wait = new WebDriverWait(driver, timeout);
                java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit")
                        .setLevel(java.util.logging.Level.OFF);
                java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);

                driver.get("Http://www.americanexpress.com");
                driver.findElement(By.id("Username")).clear();
                driver.findElement(By.id("Username")).sendKeys(credentials.get(0));
                driver.findElement(By.id("Password")).clear();
                driver.findElement(By.id("Password")).sendKeys(credentials.get(1));
                driver.findElement(By.id("loginLink")).click();

                //Total amount       
                waitAndClick(driver, wait, By.id("avlbleCreditAsterick"));
                waitForElement(wait, By.id("availCreditValue"));
                String balanceStr = driver.findElement(By.id("availCreditValue")).getText();
                String limitStr = driver.findElement(By.id("ttlCreditValue")).getText();
                double amount = convertStringAmountToDouble(balanceStr) - convertStringAmountToDouble(limitStr);
                addTotal(stmt, getTimestamp(), 7, amount, taLog);

                //Transactions
                waitAndClick(driver, wait, By.id("MYCA_PC_Statements2"));
                waitAndClick(driver, wait, By.id("expandTP"));
                waitAndClick(driver, wait, By.id("anon_7"));//January 1 to Present
                wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("expandTP"))));

                //Pending transactions
                driver.findElement(By.id("recentTrans")).click();
                waitForElement(wait, By.className("pendingetd-helptxt"));
                runQueryAmEx(driver, wait, true);

                //Posted transactions
                runQueryAmEx(driver, wait, false);

                driver.findElement(By.id("iNLogBtn")).click();
                driver.quit();

                isDownloaded = true;
            } catch (Exception ex) {
                driver.quit();
                trace(ex.getMessage(), taLog);
                clearBatch(stmt);
                attempt++;
            }
            ;
        }
        trace((isDownloaded ? "Downloaded" : "Not downloaded") + " from " + attempt
                + ((attempt == 1) ? " attempt" : " attemps"), taLog);

        //Execution
        executeBatch(stmt, taLog);
    }

    private static void runQueryAmEx(WebDriver driver, WebDriverWait wait, Boolean isPending) {
        String date;
        String Analytics;
        String Category;
        Double amount;

        List<WebElement> rows = driver.findElements(By.className((isPending) ? "pending-trans" : "posted-trans"));
        for (WebElement row : rows) {
            date = rotateDate(row.findElement(By.className("colDate")).getText().replace("\n*", ""), "MMMM\ndd");
            Analytics = prepareTextForQuery(row.findElement(By.className("desc-trans")).getText());
            amount = -convertStringAmountToDouble(row.findElement(By.className("colAmmount")).getText());
            if (amount < 0) {
                row.findElement(By.className("colPlus")).click();
                sleep(sleepTime);
                wait.until(ExpectedConditions.elementToBeClickable(row.findElement(By.className("printLink"))));
                Category = prepareTextForQuery(row.findElement(By.className("mobl_spanAddress")).getText());
                row.findElement(By.className("colPlus")).click();
            } else {
                Category = "";
            }
            addTransaction(true, stmt, date, 7, Category, Analytics, amount, isPending, taLog);
        }
    }

    private static List<String> getCredentials(int account) {

        return hmCredentials.get(String.valueOf(account));

    }

    private static void initializeCredentialsMap() {
        ResultSet resultSet;

        hmCredentials = new HashMap<String, List<String>>();

        try {
            resultSet = stmt.executeQuery("SELECT * FROM CREDENTIALS");
            if (resultSet.next()) {
                List<String> cred = new ArrayList<>();
                cred.add(resultSet.getString("Login"));
                cred.add(resultSet.getString("Password"));
                hmCredentials.put(resultSet.getString("AccountID"), cred);
            }
        } catch (SQLException ex) {
        }
        ;

    }

    public static void unitTest(Statement statement, TextArea textarea) {
        stmt = statement;
        taLog = textarea;

        List<String> l1;
        List<String> l2;

        initializeCredentialsMap();

        l1 = getCredentials(4);
        trace("list 1. size: " + l1.size(), taLog);
        trace("list 1. 1st: " + l1.get(0), taLog);

        /* l2 = getCredentials(7);
         trace("list 2. size: " + l2.size(), taLog);
         trace("list 2. 1st: " + l2.get(0), taLog);                */
    }

    public static void HTMLUnitTest(Statement statement, TextArea textarea) {
        stmt = statement;
        taLog = textarea;

        final WebClient webClient = new WebClient();
        final HtmlPage page;
        try {
            page = webClient.getPage("http://www.americanexpress.com");
            trace(page.getTitleText(), taLog);
            /*page.getElementById("Username").se
            driver.findElement(By.id("Username")).clear();
            driver.findElement(By.id("Username")).sendKeys(credentials.get(0));
            driver.findElement(By.id("Password")).clear();
            driver.findElement(By.id("Password")).sendKeys(credentials.get(1));
            driver.findElement(By.id("loginLink")).click();*/
        } catch (IOException ex) {
            Logger.getLogger(WebDriverManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FailingHttpStatusCodeException ex) {
            Logger.getLogger(WebDriverManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}