简体   繁体   中英

I'm trying to validate all links on a webpage are working. How do I ignore a logout link

The following method clicks on all links of our application home page and validates they are working. The problem is the logout link on the page, as you can imagine once I click it they test fails. Is there a way I can ignore or remove the check of the logout link? FYI the logout is the first element in the array. Any help is appreciated

public void checkAllLinks() {

    driver.switchTo().defaultContent().switchTo().frame("mainFrame");
    List<WebElement> linkElements = driver.findElements(By.tagName("a"));

    String[] linkTexts = new String[linkElements.size()];

    int i = 0;

    // extract the link texts of each link element

    for (WebElement e : linkElements) {

        logger.info(linkTexts[i] = e.getText());

        i++;

    }
    for (String l : linkTexts) {

        driver.findElement(By.linkText(l)).click();

        if (driver.getTitle().equals(title)) {

            System.out.println("\"" + l + "\""

            + " is not Working.");

        } else {

            System.out.println("\"" + l + "\""

            + " is working.");

        }

        driver.navigate().back();

    }
}

Remove the logout link from the list before clicking all the links

  List<WebElement> linkElements = driver.findElements(By.tagName("a"));

  int logoutlinkIndex;

  for (WebElement linkElement : linkElements) {
           if (linkElement.getText().equals("Log out link text")) {
                       logoutlinkIndex = linkElements.indexOf(linkElement);
                       break;
            }

  }

 linkElements.remove(logoutlinkIndex);

If you know it's the first, then add a count and skip the first:

i = 0;
for (String l : linkTexts) {
    if (i > 0) {
        // ...
    }
    i++;
}

You should have a function that goes to the starting page (and logs in, if necessary. (The best option would be to use Page Objects, but that requires more setup on your project)

Rather than using driver.navigate().back() , use that function.

(If you can't figure out a way to test if you are on the logon page, find an element on the page, and catch NoSuchElementException )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM