3
\$\begingroup\$

The code uses a string key to find the relevant element on the DOM, then the second switch uses the string key to query the DB with certain filter and where clause (or not in case of a date filter).

This is mind boggling, I probably should have created an individual method per filter type.

public void EnterFilterData(string filterName, string data = null)
{
    //TODO: Need to break this method up per section
    _filterButton.Click();

    var filterInput = SelectFilter(filterName);

    string colName = "";
    string whereClause = "";

    if (filterName.ToLower().Contains("date"))
    {
        var filterInputTwo = SelectFilter(filterName.Replace("From", "To"));
        filterInput.EnterData(DateTime.Now.AddDays(-2).ToString("dd/MM/yyy"));
        filterInputTwo.EnterData(DateTime.Now.ToString("dd/MM/yyy"));
        data = DateTime.Now.AddDays(-2).ToString("dd/MM/yyy");
    }
    else if (filterName.ToLower().Contains("range"))
    {
        var filterInputTwo = SelectFilter(filterName.Replace("From", "To"));
        filterInput.EnterData("5");
        filterInputTwo.EnterData("10");
        data = "5";
    }
    else
    {
        if (string.IsNullOrEmpty(data))
        {
            switch (filterName.ToLower())
            {
                case "person reference":
                    colName = "t.person_reference";
                    whereClause = "WHERE b.deal_ref IS NOT NULL";
                    break;
                case "person name":
                    colName = "t.person_name";
                    break;
                case "client type reference":
                    colName = "c.party_type_ref";
                    break;
                case "party reference":
                case "client reference":
                    colName = "c.party_ref";
                    whereClause = "WHERE b.deal_ref IS NOT NULL";
                    break;
                case "party":
                case "client name":
                    colName = "c.party_name";
                    break;
                case "external reference":
                    colName = "substring(sql select string)";
                    break;
                case "status":
                    colName = "cs.name";
                    break;
                case "dealing allowed":
                    colName = "c.is_dealing_allowed";
                    break;
                case "portfolio reference":
                    colName = "p.portfolio_ref";
                    whereClause = "WHERE b.deal_ref IS NOT NULL";
                    break;
                case "portfolio name":
                    colName = "p.portfolio_name";
                    break;
                case "deal reference":
                    colName = "b.deal_ref";
                    break;
                case "outstanding":
                    colName = "b.outstanding";
                    whereClause = $"WHERE {colName} = 1";
                    break;
                case "isin":
                    colName = "b.isin";
                    break;
                case "epic":
                    colName = "b.instrument_epic";
                    break;
                case "deal type":
                    colName = "bt.deal_type_ref";
                    break;
                case "account reference":
                    colName = "ta.account_ref";
                    whereClause = "WHERE ta.account_ref IS NOT NULL AND b.deal_ref IS NOT NULL";
                    break;
                case "depot reference":
                    colName = "b.depot_ref";
                    break;
                case "deal status":
                    colName = "bs.deal_status_ref";
                    break;
            }

            var sql = $"SELECT top 1 {colName} " +
                      $"FROM SQLTABLE
                          $"{whereClause} " +
                          $"ORDER BY NEWID() desc";

            data = Utilities.SendSql("DB", sql);
        }

        if (filterInput.TagName.Equals("select"))
        {
            switch (data)
            {
                case "0":
                case "False":
                    data = "No";
                    break;
                case "1":
                case "True":
                    data = "Yes";
                    break;
            }
            filterInput.SelectDropDownItem(_driver, data);
        }
        else
        {
            filterInput.EnterData(data);
        }
    }

    _filterOkayButton.Click();
    _driver.SyncPage(1);

    if (filterName.ToLower().Contains("deal status"))
    {
        ScenarioContext.Current["FilterData"] = Utilities.SendSql("broker",
            "select deal_status_name from wins_trading.deal_status " +
            $"where deal_status_ref = '{data}'").ToArray()[0];
    }
    else if (filterName.ToLower().Contains("deal type"))
    {
        ScenarioContext.Current["FilterData"] = Utilities.SendSql("broker",
            "select deal_type_name from wins_trading.deal_type " +
            $"where deal_type_ref = '{data}'").ToArray()[0];
    }
    else if (filterName.ToLower().Contains("outstanding"))
    {
        ScenarioContext.Current["FilterData"] = data.Equals("Yes") ? "Outstanding" : "Matched";
    }
    else
    {
        ScenarioContext.Current["FilterData"] = data;
    }
}

The second switch (its actually the first one that's called) is SelectFilter()

private IWebElement SelectFilter(string filterName)
{
    var type = "";
    switch (filterName.ToLower())
    {
        case "person reference":
            filterName = "person Reference";
            type = "div/input";
            break;
        case "client type reference":
            filterName = "Client Type Reference";
            type = "div/input";
            break;
        case "client reference":
            filterName = "Client Reference";
            type = "div/input";
            break;
        case "portfolio reference":
            filterName = "Portfolio Reference";
            type = "div/input";
            break;
        case "client name":
            filterName = "Client Name";
            type = "input";
            break;
        case "external reference":
            filterName = "External Reference";
            type = "input";
            break;
        case "status":
        case "deal status":
            filterName = "Status";
            type = "select";
            break;
        case "dealing allowed":
            filterName = "Dealing Allowed";
            type = "select";
            break;
        case "party":
            filterName = "party";
            type = "input";
            break;
        case "portfolio name":
            filterName = "Portfolio Name";
            type = "input";
            break;
        case "person name":
            filterName = "person Name";
            type = "input";
            break;
        case "deal reference":
            filterName = "deal Reference";
            type = "input";
            break;
        case "outstanding":
            filterName = "Outstanding";
            type = "select";
            break;
        case "deal type":
            filterName = "deal Type";
            type = "input";
            break;
        case "isin":
            filterName = "ISIN";
            type = "input";
            break;
        case "epic":
            filterName = "Epic";
            type = "input";
            break;
        case "from deal date":
            filterName = "From Deal Date";
            type = "input";
            break;
        case "to deal date":
            filterName = "To Deal Date";
            type = "input";
            break;
        case "from settlement date":
            filterName = "From Settlement Date";
            type = "input";
            break;
        case "to settlement date":
            filterName = "To Settlement Date";
            type = "input";
            break;
        case "quantity range to":
            filterName = "Quantity range to";
            type = "input";
            break;
        case "quantity range from":
            filterName = "Quantity range From";
            type = "input";
            break;
        case "depot reference":
            filterName = "Depot"
            type = "input";
            break;
        case "account reference":
            filterName = "Account Reference";
            type = "input";
            break;
    }
    IWebElement e;
    try
    {
        e = _filterSectionLabel.FirstOrDefault(x => x.Text.Equals(filterName))
            .FindElement(By.XPath($"../{type}"));
    }
    catch (Exception)
    {
        Reporter.ReportStatus("Ignore above exception", Status.Done);
        e = _filterSectionLabel.FirstOrDefault(x => x.Text.Equals(filterName))
            .FindElement(By.XPath($"../div/input"));
    }
    return e;
}
\$\endgroup\$
  • \$\begingroup\$ What are you asking? Consider strategy pattern. \$\endgroup\$ – Nkosi Jan 23 '18 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.