Assert Equal для сравнения строки и списка массивов с текстовыми данными из Excel - PullRequest
0 голосов
/ 11 февраля 2019

Этот код ниже я использовал, если (содержит) Но мне нужно использовать, если (равно), я не могу, потому что данные String [] actualViews поступают из листа Excel.

"Отчетный период, бизнесДень, Управляемая география, Организация бизнеса, Отрасль, управляемая отношениями, Порядок учета, Бизнес-единица, Управляемый сегмент, Страна бронирования, Сектор, Третья сторона / Межфирменная компания, Корректировки показа, Утверждающая единица, Юридическое средство, Страна законного носителя, Тип актива, Охват, IRUCredit Org, IRU Credit Coverage, Регион IRU, Старший менеджер по рискам, CAGID "

выбранный текст Номинал, который не упоминается в тестах Excel, все еще проходит тесты, а List xviews не равен листовым данным, тем не менее мой тестовый проход.Этот код ниже не работает.Мне нужна помощь, как я могу проверить данные из ArrayList и String [] равных обоих данных.

public void dataVisualizationCheck(WebDriver driver, String actualView, String reportingPeriod,
        String summaryReportName) throws InterruptedException {
    String[] actualViews = actualView.split(",");
    List<String> xviews = new ArrayList<>();
    boolean flag = false;

    waitForLoad(driver, 60);
    waitForElementPresent(driver, 30, dataVisualizationHeader);
    int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
    for (int i = 1; i <= size; i++) {
        xviews.add(getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i));    
        //System.out.println(xviews +" New View");
    //  System.out.println(xviews +" New View");
    }
    for (int i1 = 0; i1 < actualViews.length; i1++) {
        //System.out.println(actualViews[i1] + " Actual");

        if (xviews.contains(actualViews[i1].trim().toString())) {
            Add_Log.info("Successfully displayed "+actualViews[i1].trim().toString() + " filter criteria is displayed in Data Visualisation Criteria screen");
            Reporter.log("Successfully displayed "+actualViews[i1].trim().toString() + " filter criteria is displayed in Data Visualisation Criteria screen");
        } else {
            Add_Log.info(actualViews[i1].trim().toString() + " filter criteria is not displayed in Data Visualisation Criteria screen");
            Reporter.log(actualViews[i1].trim().toString() + " filter criteria is not displayed in Data Visualisation Criteria screen");
            flag = true;
        }
    }

    if (flag) {
        TestResultStatus.Testfail = true;
        Assert.fail();
    } else {
        Add_Log.info("Successfully all filter criterias are displayed in Data Visualisation Criteria screen");
        Reporter.log("Successfully all filter criterias are displayed in Data Visualisation Criteria screen");
    }
}

Вывод

    INFO [main] (SeleniumUtils.java:506) - Successfully waited for Summary option expand to be present on page
 INFO [main] (SeleniumUtils.java:469) - Successfully waited for loader to disappear
 INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from  Filter criteria #1
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from  Filter criteria #2
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from  Filter criteria #3
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from  Filter criteria #4
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from  Filter criteria #5
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from  Filter criteria #6
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from  Filter criteria #7
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from  Filter criteria #8
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from  Filter criteria #9
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from  Filter criteria #10
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from  Filter criteria #11
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from  Filter criteria #12
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from  Filter criteria #13
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from  Filter criteria #14
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from  Filter criteria #15
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from  Filter criteria #16
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from  Filter criteria #17
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from  Filter criteria #18
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from  Filter criteria #19
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from  Filter criteria #20
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from  Filter criteria #21
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from  Filter criteria #22
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from  Filter criteria #23
[Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, Denomination, CAGID] New View
 INFO [main] (FrontPage.java:78) - Successfully displayed Reporting Period filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Business Day filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Managed Geography filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Business Organization filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Relationship Managed Industry filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Accounting Treatment filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Business Unit filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Managed Segment filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Booking Country filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Sector filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Third Party / Intercompany filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Show Adjustments filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Approving Unit filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Legal Vehicle filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Legal Vehicle Country filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Asset Type filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Coverage filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed IRU Credit Org filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed IRU Credit Coverage filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed IRU Region filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed Senior Risk Manager filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:78) - Successfully displayed CAGID filter criteria is displayed in Data Visualisation Criteria screen
 INFO [main] (FrontPage.java:91) - Successfully all filter criterias are displayed in Data Visualisation Criteria screen
INFO [main] (Sanity_TC.java:3382) - Sanity_TC002 is PASS


Привет Али CSE

Я пытаюсь это сделать, но не работаю, позволяет мне как исправить этот код.

    public void dataVisualizationCheck1(WebDriver driver, String actualView, String reportingPeriod,
        String summaryReportName) throws InterruptedException {
    waitForLoad(driver, 60);
    waitForElementPresent(driver, 30, dataVisualizationHeader);
    String[] actualViews = actualView.split(",");
    boolean flag = false;
    int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
    String[] xviews = new String[size]; 
    for (int i = 1; i <= size; i++) {
        xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);

        for(int i1=0;i1<actualViews.length;i1++) {
            boolean equal = false;
            for(int j=0;j<xviews.length;j++) {
                if(actualViews[i1].equals(xviews[j])) {
                    equal = true;
                    break;
                }
            }
            if(equal) {
               System.out.println("=> '"+actualViews[i1]+"' is there in the other array...");
            } else {
               System.out.println("=> '"+actualViews[i1]+"' is NOT there in the other array...");
            }
        }
    }

ВЫХОД

INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from  Filter criteria #1
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from  Filter criteria #2
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from  Filter criteria #3
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from  Filter criteria #4
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from  Filter criteria #5
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from  Filter criteria #6
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from  Filter criteria #7
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from  Filter criteria #8
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from  Filter criteria #9
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from  Filter criteria #10
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from  Filter criteria #11
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from  Filter criteria #12
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from  Filter criteria #13
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from  Filter criteria #14
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from  Filter criteria #15
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from  Filter criteria #16
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from  Filter criteria #17
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from  Filter criteria #18
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from  Filter criteria #19
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from  Filter criteria #20
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from  Filter criteria #21
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from  Filter criteria #22
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from  Filter criteria #23
=> 'Reporting Period' is there in the other array...
=> ' Business Day' is NOT there in the other array...
=> ' Managed Geography' is NOT there in the other array...
=> ' Business Organization' is NOT there in the other array...
=> ' Relationship Managed Industry' is NOT there in the other array...
=> ' Accounting Treatment' is NOT there in the other array...
=> ' Business Unit' is NOT there in the other array...
=> ' Managed Segment' is NOT there in the other array...
=> ' Booking Country' is NOT there in the other array...
=> ' Sector' is NOT there in the other array...
=> ' Third Party / Intercompany' is NOT there in the other array...
=> ' Show Adjustments' is NOT there in the other array...
=> ' Approving Unit' is NOT there in the other array...
=> ' Legal Vehicle' is NOT there in the other array...
=> ' Legal Vehicle Country' is NOT there in the other array...
=> ' Asset Type' is NOT there in the other array...
=> ' Coverage' is NOT there in the other array...
=> ' IRU Credit Org' is NOT there in the other array...
=> ' IRU Credit Coverage' is NOT there in the other array...
=> ' IRU Region' is NOT there in the other array...
=> ' Senior Risk Manager' is NOT there in the other array...
=> ' Denomination' is NOT there in the other array...
=> ' CAGID' is NOT there in the other array...
 INFO [main] (Sanity_TC.java:3391) - Sanity_TC002 is 

ВЫХОД С ПЕРЕРЫВОМ

    INFO [main] (SeleniumUtils.java:506) - Successfully waited for Data Visualization Criteria Header to be present on page
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Reporting Period from  Filter criteria #1
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Day from  Filter criteria #2
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Geography from  Filter criteria #3
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Organization from  Filter criteria #4
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Relationship Managed Industry from  Filter criteria #5
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Accounting Treatment from  Filter criteria #6
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Business Unit from  Filter criteria #7
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Managed Segment from  Filter criteria #8
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Booking Country from  Filter criteria #9
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Sector from  Filter criteria #10
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Third Party / Intercompany from  Filter criteria #11
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Show Adjustments from  Filter criteria #12
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Approving Unit from  Filter criteria #13
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle from  Filter criteria #14
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Legal Vehicle Country from  Filter criteria #15
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Asset Type from  Filter criteria #16
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Coverage from  Filter criteria #17
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Org from  Filter criteria #18
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Credit Coverage from  Filter criteria #19
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text IRU Region from  Filter criteria #20
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Senior Risk Manager from  Filter criteria #21
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text Denomination from  Filter criteria #22
 INFO [main] (SeleniumUtils.java:234) - Successfully fetched text CAGID from  Filter criteria #23
=> 'Reporting Period' is there in the other array...
 INFO [main] (Sanity_TC.java:3391) - Sanity_TC002 is 

1 Ответ

0 голосов
/ 11 февраля 2019

Вместо сравнения данных String Array с данными Array List вы можете преобразовать их в одинаковые структуры данных (как String Arrays или Array Lists), отсортировать их и сравнить их просто.

На основеВ приведенной ниже строке вы узнаете фактическую длину представлений, поэтому сохраните их в некоторой переменной и создайте один новый массив строк этой длины:

String[] actualViews = actualView.split(",");
int actualViewsSize = actualViews.length;
String xviews = new String[actualViewsSize];

Или используйте приведенный ниже размер для инициализации массива:

int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
String xviews = new String[actualViewsSize];

Затем выполните цикл до размера полей визуализации данных и сохраните их во вновь созданном массиве:

for (int i = 1; i <= size; i++) {
    xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);    
    //System.out.println(xviews +" New View");
    //  System.out.println(xviews +" New View");
}

И в конце вы можете отсортировать оба String Arrays и сравнитькак показано ниже:

Arrays.sort(actualViews);
Arrays.sort(xviews);
boolean equal = true;
for(int i=0;i<actualViews.length && equal;i++) {
    if(!actualViews[i].equals(xviews[i]) {
        equal = false;
    }
}
if(equal) {
   System.out.println("=> The both String Arrays data is equal...");
} else {
   System.out.println("=> The both String Arrays data is NOT equal...");
}

Но приведенный выше код работает отлично, если длина обоих массивов одинакова, в противном случае вам нужно использовать два цикла для сравнения данных, как показано ниже, и сортировка здесь необязательна:

for(int i=0;i<actualViews.length;i++) {
    boolean equal = false;
    for(int j=0;j<xviews.length;j++) {
        if(actualViews[i].equals(xviews[j])) {
            equal = true;
            break;
        }
    }
    if(equal) {
       System.out.println("=> '"+actualViews[i]+"' is there in the other array...");
    } else {
       System.out.println("=> '"+actualViews[i]+"' is NOT there in the other array...");
    }
}

Обновление 1:

Вы не ставите условия перерыва в коде, который вы изменили, и вам необходимо отделить хранениечасть, попробуйте следующий код:

public void dataVisualizationCheck1(WebDriver driver, String actualView, String reportingPeriod, String summaryReportName) throws InterruptedException {
    waitForLoad(driver, 60);
    waitForElementPresent(driver, 30, dataVisualizationHeader);
    String[] actualViews = actualView.split(",");
    boolean flag = false;
    int size = driver.findElements(By.xpath(DATAVISUALIZATIONFIELDS)).size();
    String[] xviews = new String[size]; 
    for (int i = 1; i <= size; i++) {
        xviews[i-1] = getText(driver, By.xpath("("+ DATAVISUALIZATIONFIELDS +")["+ i +"]"), " Filter criteria #" + i);
    }
    for(int i=0;i<actualViews.length;i++) {
        boolean equal = false;
        for(int j=0;j<xviews.length;j++) {
            if(actualViews[i].equals(xviews[j])) {
                equal = true;
                break;
            }
        }
        if(equal) {
            System.out.println("=> '"+actualViews[i]+"' is there in the other array...");
            break;
        } else {
            System.out.println("=> '"+actualViews[i]+"' is NOT there in the other array...");
        }
    }
}

Обновление 2:

Если вы все еще сталкиваетесь с проблемой, то обратитесь к приведенному ниже примеру кода, который будет проходить через обамассивы и будет пытаться найти совпадающие, несовпадающие значения:

// Suppose, below are the actual views
String actualViewsString = "Ali, Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, CAGID";
String[] actualViews = actualViewsString.split(", ");
for(String actualView : actualViews) {
    System.out.println(actualView);
}

System.out.println("****************************************************************");

// Suppose, below are the xviews
String xviewsString = "Reporting Period, Business Day, Managed Geography, Business Organization, Relationship Managed Industry, Accounting Treatment, Business Unit, Managed Segment, Booking Country, Sector, Third Party / Intercompany, Show Adjustments, Approving Unit, Legal Vehicle, Legal Vehicle Country, Asset Type, Coverage, IRU Credit Org, IRU Credit Coverage, IRU Region, Senior Risk Manager, CAGID";
String[] xviews = xviewsString.split(", ");
for(String xview : xviews) {
    System.out.println(xview);
}

// Comparing both the String arrays
for(int i=0;i<actualViews.length;i++) {
    boolean matching = false;
    for(int j=0;j<xviews.length;j++) {
        if(actualViews[i].equals(xviews[j])) {
            matching = true;
            break;
        }
    }
    if(matching) {
        System.out.println("=> '"+actualViews[i]+"' matching is there in the both arrays...");
    } else {
        System.out.println("\n===> '"+actualViews[i]+"' matching is NOT there in the both arrays...\n");
    }
}

Надеюсь, это поможет ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...