比较从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);
答案 0 :(得分:0)
正如我从您的代码中看到的那样,您的project_text
和actualVal
采用字符串形式。比较值中的值的最佳方法是将它们存储为字符串数组,因为您循环遍历许多值,并且需要将它们存储到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。 希望这会有所帮助。