Selenium - Java - 如何 assert/verify 页面上的所有链接都有效,获取标题并根据预期标题进行验证
Selenium - Java - how to assert/verify all links on a page are working, get title and verify against expected titles
我是测试自动化的新手,目前正在从事个人项目
我有这种方法,它可以找到页面上某个部分的所有 link,单击每个 link,通过每个选项卡进行刺激,然后获取每个页面的标题
但是,我想要一种方法来根据预期标题列表验证这些 link 的标题
为了做到这一点,修改它的最佳方法是什么?将每个标题分别存储在数组中然后 assert/verify 会更好吗?
我尝试了几种方法来通过更改 return 类型来断言 String 和 List 但没有成功
public void linksAreWorkingDashBoardLeftPanal() throws Exception {
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
Thread.sleep(5000);
}
//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
while(it.hasNext())
{
driver.switchTo().window(it.next());
System.out.println(driver.getTitle());
}
修改while循环如下;
List<String> expectedTitleList = new ArrayList<>();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
// Create copies of each list;
Set<String> expectedSet = new HashSet<>(expectedTitleList);
Set<String> actualSet = new HashSet<>(actualTitleList);
Set<String> expectedOnlySet = new HashSet<>(expectedSet);
Set<String> actualOnlySet = new HashSet<>(actualSet);
expectedOnlySet.removeAll(actualSet);
actualOnlySet.removeAll(expectedSet);
// Check if expectedOnlySet and actualOnlySet are empty
// If both of them are empty then all titles are received as expected.
boolean isEmpty = (expectedOnlySet.size() == 0 && actualOnlySet.size() == 0);
if (expectedOnlySet.size() > 0 || actualOnlySet.size() > 0) {
// If expectedOnlySet contain any title; it means those titles are not opened in the browser.
// If actualOnlySet contain any title; it means those titles are not expected titles.
StringBuilder message = new StringBuilder();
for (String x: expectedOnlySet) {
message.append("Did not receive: ");
message.append(x).append(", ");
}
for (String y: actualOnlySet) {
message.append("Not expected but received: ");
message.append(y).append(", ");
}
Assert.assertTrue(false, message,toString());
}else {
Assert.assertTrue(true);
}
这里可以使用Collections中的removeAll()方法。检查 documentation
我们必须检查这两种情况;
- 是否收到所有预期标题
- 所有收到的标题都是预期的
谢谢!我从你的代码中获取了我需要的东西,它运行得很好。
这项工作,我添加了自己的断言
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
}//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> expectedTitleList = new ArrayList<>();
expectedTitleList.add("Page 1 title");
expectedTitleList.add("Page 2 title");
expectedTitleList.add("Page 3 title");
expectedTitleList.add("Page 4 title");
expectedTitleList.add("Page 5 title");
expectedTitleList.add("Page 6 title");
expectedTitleList.add("Page 7 title");
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
//System.out.println(expectedTitleList);
// System.out.println(actualTitleList);
if(actualTitleList.equals(expectedTitleList)).....
我是测试自动化的新手,目前正在从事个人项目
我有这种方法,它可以找到页面上某个部分的所有 link,单击每个 link,通过每个选项卡进行刺激,然后获取每个页面的标题
但是,我想要一种方法来根据预期标题列表验证这些 link 的标题
为了做到这一点,修改它的最佳方法是什么?将每个标题分别存储在数组中然后 assert/verify 会更好吗?
我尝试了几种方法来通过更改 return 类型来断言 String 和 List 但没有成功
public void linksAreWorkingDashBoardLeftPanal() throws Exception {
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
Thread.sleep(5000);
}
//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
while(it.hasNext())
{
driver.switchTo().window(it.next());
System.out.println(driver.getTitle());
}
修改while循环如下;
List<String> expectedTitleList = new ArrayList<>();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
// Create copies of each list;
Set<String> expectedSet = new HashSet<>(expectedTitleList);
Set<String> actualSet = new HashSet<>(actualTitleList);
Set<String> expectedOnlySet = new HashSet<>(expectedSet);
Set<String> actualOnlySet = new HashSet<>(actualSet);
expectedOnlySet.removeAll(actualSet);
actualOnlySet.removeAll(expectedSet);
// Check if expectedOnlySet and actualOnlySet are empty
// If both of them are empty then all titles are received as expected.
boolean isEmpty = (expectedOnlySet.size() == 0 && actualOnlySet.size() == 0);
if (expectedOnlySet.size() > 0 || actualOnlySet.size() > 0) {
// If expectedOnlySet contain any title; it means those titles are not opened in the browser.
// If actualOnlySet contain any title; it means those titles are not expected titles.
StringBuilder message = new StringBuilder();
for (String x: expectedOnlySet) {
message.append("Did not receive: ");
message.append(x).append(", ");
}
for (String y: actualOnlySet) {
message.append("Not expected but received: ");
message.append(y).append(", ");
}
Assert.assertTrue(false, message,toString());
}else {
Assert.assertTrue(true);
}
这里可以使用Collections中的removeAll()方法。检查 documentation
我们必须检查这两种情况;
- 是否收到所有预期标题
- 所有收到的标题都是预期的
谢谢!我从你的代码中获取了我需要的东西,它运行得很好。
这项工作,我添加了自己的断言
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
}//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> expectedTitleList = new ArrayList<>();
expectedTitleList.add("Page 1 title");
expectedTitleList.add("Page 2 title");
expectedTitleList.add("Page 3 title");
expectedTitleList.add("Page 4 title");
expectedTitleList.add("Page 5 title");
expectedTitleList.add("Page 6 title");
expectedTitleList.add("Page 7 title");
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
//System.out.println(expectedTitleList);
// System.out.println(actualTitleList);
if(actualTitleList.equals(expectedTitleList)).....