简体   繁体   中英

Comparing text extracted from two different loops in selenium and assert whether they are equal or not

Comparing text extracted from two different loops in selenium and assert whether they are equal or not. Following is the selenium code, I need to compare two strings as project_text and actualVal:

driver.findElement(By.xpath(OR.getProperty("projects"))).click();
    Thread.sleep(5000);
    System.out.println("Selecting from list");

    Select project_dropdown = new Select(driver.findElement(By.xpath(OR.getProperty("client"))));
    project_dropdown.selectByVisibleText("Marketo");            

    System.out.println("Verifying the selection of the list");
    String part1=OR.getProperty("project_start");
    String part2=OR.getProperty("project_end");

    String assign_part1=OR.getProperty("assign_users_start");
    String assign_part2=OR.getProperty("assign_users_end");

    int i=1;
    String project_text = null;
    String assign_text = null;
    while(isElementPresent(part1+i+part2)){     
        project_text = driver.findElement(By.xpath(part1+i+part2)).getText();   
        assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
        if (assign_text.contains("aaaa"))
        System.out.println(project_text);
        i++;
    }
    System.out.println("Project_text = " + project_text);

    driver.findElement(By.xpath(OR.getProperty("calender"))).click();
    Thread.sleep(5000);
    driver.findElement(By.xpath(OR.getProperty("date_link"))).click();
    Thread.sleep(5000);
    Select project_dropdown1 = new Select(driver.findElement(By.xpath(OR.getProperty("client_select"))));
    project_dropdown1.selectByVisibleText("Marketo");

    WebElement project_droplist= driver.findElement(By.xpath(OR.getProperty("project_select"))); 
    List<WebElement> droplist_cotents = project_droplist.findElements(By.tagName("option"));
    System.out.println(droplist_cotents.size());
    //System.out.println(droplist_cotents.get(3).getText());
    String actualVal=null;
    for(int j=0;j<droplist_cotents.size();j++){
        actualVal = droplist_cotents.get(j).getText();  
        System.out.println(actualVal);
        }
    System.out.println("actualVal = " + actualVal);
    Assert.assertEquals(project_text, actualVal);

As i can see from your code, your project_text and actualVal are in the form of strings. The best way to compare values in them is to store them as string array's as you are looping through many values and you need to store them somewhere to assert . Here's how to store the values in an array and compare them -

    int i = 0;
    String[] project_text = new String[100];
    String[] actualVal = new String[100];

    while(isElementPresent(part1+i+part2)){     
        project_text[i] = driver.findElement(By.xpath(part1+i+part2)).getText();   
        assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
        if (assign_text.contains("aaaa"))
            System.out.println(project_text[i]);
        i++;
    }

    for(int j=0;j<droplist_cotents.size();j++){
        actualVal[j] = droplist_cotents.get(j).getText();  
        System.out.println(actualVal[j]);
    }

    for(int i = 0;i<project_text.length();i++)
        Assert.assertEquals(project_text[i], actualVal[i]);

However if you don't know the size of the array, use ArrayList and work it out. Here's a sample . Hope this 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