尝试使用 selenium 将 assert 命令与 arraylist 结合使用,但如果元素的顺序不正确,它会中断
Trying to use an assert command in conjuntion with arraylist using selenium, but it breaks if the elements are not in the proper order
我有一个字符串需要与网站上的名称进行比较。所以我做的第一件事就是获取行数(因为有些数组中的人数多于或少于 2 人),然后将该大小放入一个 int 中。 String[]
names come from names that selenium is supposed find when it goes to the website to execute this statement assertTrue(assertion.getText().contains(names[i-1]));
问题是:如果名字不对按照它们在它打破的数组中出现的顺序出现。换句话说,如果 Mick Jagger 在 li[1]
中,而 Keith Richards 在 li[2]
中,则一切都按预期进行。但是如果 Keith Richards 出现在 li[1]
中,它就会崩溃。此外,我应该使用 assertTrue 命令来执行此操作。我尝试过排序,将网络上的内容推送到新的 ArrayList 中,但我不断收到错误。有人知道确保顺序不重要并且仍然使用 assertTrue 命令的好方法吗?
谢谢,
斯科特
WebElement assertion = null;
List<WebElement> assignees = driver.findElements(By.xpath(".//*[@id='assignee']/li"));
int count = assignees.size();
String[] names = {"Mick", "Keith"};
for (int i = 1; i < count; i++)
{
assertion = driver.findElement(By.xpath(".//*[@id='assignee']/li["+i+"]"));
assertTrue(assertion.getText().contains(names[i-1]));
如果names
代表的是完整的字符串,就直接翻转即可。确保 assertion
中的文本(可能应该命名为 assignee 而不是 assertion)包含在 collection:
中
assertTrue(Arrays.asList(names).contains(assertion.getText());
让我知道这是否行不通,因为 name
实际上是断言中文本的子集,我会调整答案。
如果它们不完全匹配(你已经指出它们不匹配),你可以在 c# 中使用 linq 来匹配它。由于您使用的是 java ,因此您可以使用额外的循环。在我不知道的 java 中可能有更有效的方法来执行此操作。
String assigneeText = assertion.getText();
boolean nameFound = false;
for(String name: names)
{
nameFound = assigneeText.contains(name);
if(nameFound)
{
break;
}
}
assertTrue(nameFound, "None of the expected names were found in the following assignee text: " + assigneeText);
我有一个字符串需要与网站上的名称进行比较。所以我做的第一件事就是获取行数(因为有些数组中的人数多于或少于 2 人),然后将该大小放入一个 int 中。 String[]
names come from names that selenium is supposed find when it goes to the website to execute this statement assertTrue(assertion.getText().contains(names[i-1]));
问题是:如果名字不对按照它们在它打破的数组中出现的顺序出现。换句话说,如果 Mick Jagger 在 li[1]
中,而 Keith Richards 在 li[2]
中,则一切都按预期进行。但是如果 Keith Richards 出现在 li[1]
中,它就会崩溃。此外,我应该使用 assertTrue 命令来执行此操作。我尝试过排序,将网络上的内容推送到新的 ArrayList 中,但我不断收到错误。有人知道确保顺序不重要并且仍然使用 assertTrue 命令的好方法吗?
谢谢, 斯科特
WebElement assertion = null;
List<WebElement> assignees = driver.findElements(By.xpath(".//*[@id='assignee']/li"));
int count = assignees.size();
String[] names = {"Mick", "Keith"};
for (int i = 1; i < count; i++)
{
assertion = driver.findElement(By.xpath(".//*[@id='assignee']/li["+i+"]"));
assertTrue(assertion.getText().contains(names[i-1]));
如果names
代表的是完整的字符串,就直接翻转即可。确保 assertion
中的文本(可能应该命名为 assignee 而不是 assertion)包含在 collection:
assertTrue(Arrays.asList(names).contains(assertion.getText());
让我知道这是否行不通,因为 name
实际上是断言中文本的子集,我会调整答案。
如果它们不完全匹配(你已经指出它们不匹配),你可以在 c# 中使用 linq 来匹配它。由于您使用的是 java ,因此您可以使用额外的循环。在我不知道的 java 中可能有更有效的方法来执行此操作。
String assigneeText = assertion.getText();
boolean nameFound = false;
for(String name: names)
{
nameFound = assigneeText.contains(name);
if(nameFound)
{
break;
}
}
assertTrue(nameFound, "None of the expected names were found in the following assignee text: " + assigneeText);