简体   繁体   English

比较从硒中两个不同循环中提取的文本,并断言它们是否相等

[英]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: 以下是硒代码,我需要比较两个字符串作为project_text和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. 正如我从您的代码中看到的那样,您的project_textactualVal是字符串形式。 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 . 比较它们中的值的最佳方法是在遍历许多值时将它们存储为字符串数组,并且需要将它们存储在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. 但是,如果您不知道数组的大小,请使用ArrayList并进行计算。 Here's a sample . 这是一个样本 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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