[英]Assert Equal to compare string and Array List with text data from excel
[英]Comparing text extracted from two different loops in selenium and assert whether they are equal or not
比較從硒中兩個不同循環中提取的文本,並斷言它們是否相等。 以下是硒代碼,我需要比較兩個字符串作為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);
正如我從您的代碼中看到的那樣,您的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並進行計算。 這是一個樣本 。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.