How to assert links count on web page through selenium webdriver

  • I have written below code in a separate class to count links on a particular webpage, How to assert this in a test case class:

    public static void navigateToLinkInHomePage() throws Exception {

            List<WebElement> linksize = Driver.getDriver().findElements(By.tagName("a"));
            int linksCount = linksize.size();
            System.out.println("Total number of links on home page" + linksCount);
            String[] links = new String[linksCount];
            System.out.println("Number of links available");
            for(int i=0;i<linksCount;i++) {

    above code is written is CommonAssertion class, How can we Assert this with different test class

  • Change return type to int. And return size of list.

    Did you have to use static method?

    For more specific answer, If you want to get the number of links,

    // this method returns List of link url.
        public List nameOfLinks(){
    List aTagList = Driver.getDriver().findElements(By.tagName("a"));
        List names = new List();
    for(WebElement elem : aTagList){
    return names;
    public boolean comapreLists(List list1, List<

    If you want to assert it using testNG

    ``` /** int number : your expected link value */ @Test public void homeLinkNumberTest(int number, List expectedNames){ Asserts.assertTrue(nameOfLinks().containsAll(expectedNames); } ```

    This might be works.

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2