【问题标题】:Comparing text extracted from two different loops in selenium and assert whether they are equal or not比较从 selenium 中的两个不同循环中提取的文本并断言它们是否相等
【发布时间】:2015-11-26 08:49:53
【问题描述】:

比较从 selenium 中的两个不同循环中提取的文本并断言它们是否相等。以下是 selenium 代码,我需要将两个字符串作为 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);

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:

    从您的代码中可以看出,您的project_textactualVal 是字符串形式的。比较它们中的值的最佳方法是将它们存储为字符串数组,因为您正在循环许多值,并且您需要将它们存储到 assert 的某个位置。以下是如何将值存储在数组中并进行比较 -

        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]);
    

    但是,如果您不知道数组的大小,请使用 ArrayList 并计算出来。 Here's a sample。 希望这会有所帮助。

    【讨论】:

    • 感谢 Girish 的帮助,我使用了 arraylist 并且它有效..:)
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多