Java 比较selenium中从两个不同循环中提取的文本,并断言它们是否相等
比较selenium中从两个不同循环中提取的文本,并断言它们是否相等。以下是selenium代码,我需要比较两个字符串,即project_text和actualVal:Java 比较selenium中从两个不同循环中提取的文本,并断言它们是否相等,java,selenium,testng,Java,Selenium,Testng,比较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
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);
driver.findElement(By.xpath(或.getProperty(“项目”))。单击();
睡眠(5000);
System.out.println(“从列表中选择”);
Select project_dropdown=newselect(driver.findElement(By.xpath(或.getProperty(“客户端”)));
project_下拉列表。选择ByVisibleText(“Marketo”);
System.out.println(“验证列表的选择”);
字符串part1=OR.getProperty(“项目启动”);
字符串part2=OR.getProperty(“项目结束”);
字符串assign\u part1=或.getProperty(“assign\u users\u start”);
字符串assign_part2=或.getProperty(“assign_users_end”);
int i=1;
字符串project_text=null;
字符串赋值_text=null;
而(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();
如果(指定文本包含(“aaaa”))
System.out.println(项目文本);
i++;
}
System.out.println(“Project_text=“+Project_text”);
findElement(By.xpath(或.getProperty(“日历”))。单击();
睡眠(5000);
driver.findElement(By.xpath(或.getProperty(“日期链接”))。单击();
睡眠(5000);
Select project_dropdown1=new Select(driver.findElement(By.xpath(或.getProperty(“client_Select”)));
项目下拉列表1.选择可视文本(“Marketo”);
WebElement project_droplist=driver.findElement(By.xpath(或.getProperty(“project_select”));
List-droplist\u-coents=project\u-droplist.findElements(按.tagName(“选项”));
System.out.println(droplist_-compents.size());
//System.out.println(droplist_-compents.get(3.getText());
字符串actualVal=null;
对于(int j=0;j正如我从您的代码中看到的,您的project_text
和actualVal
是字符串形式的。比较它们中的值的最佳方法是将它们存储为字符串数组,因为您正在循环许多值,并且需要将它们存储在某个位置以断言
。下面是如何将值存储在数组中并比较它们-
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]);
inti=0;
String[]project_text=新字符串[100];
字符串[]实际值=新字符串[100];
而(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();
如果(指定文本包含(“aaaa”))
System.out.println(项目文本[i]);
i++;
}
对于(int j=0;jthanks Girish为了获得帮助,我使用了arraylist,它工作了……)