简体   繁体   中英

Assert Equal to compare string and Array List with text data from excel

This below code I have used if (contains) But I need to used if (equal) I'm unable to so because String[] actualViews data comes from Excel sheet.

" 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"

fetched text Denomination which is not mention in Excel still test passes and List xviews is not equal to sheet data still my test pass. This below code is not working. I need help how can I validate data from ArrayList and String[] equal both data.

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");
    }
}

Output

    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


Hello Ali CSE

I try this but not working lets me how can I fix this code.

    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...");
            }
        }
    }

OUTPUT

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 

OUTPUT WITH BREAK

    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 

Instead of comparing the String Array data with an Array List data, you can convert them into same data structures(Both as String Arrays or Array Lists), sort them and comparing them is easy.

Based on the below line, you will get to know the actual views length so store them in some variable and create one new String Array of that length :

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

Or use the below size to initialize an array :

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

Then loop through until the data visualization fields size and store them in the newly created array :

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");
}

And at the end, you can sort the both String Arrays and you can compare like below :

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...");
}

But the above code works perfectly if the both array length is same otherwise you need to use two loops for comparing the data like below and sorting is optional here :

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...");
    }
}

Update 1 :

You are not putting break conditions in the code which you have modified and need to separate the storing part, try the below code :

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...");
        }
    }
}

Update 2 :

If you are still facing the problem then refer the below sample code which will iterate through both the arrays and will try to find matching, non-matching values :

// 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");
    }
}

I hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM