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<String> nameOfLinks(){
List<WebElement> aTagList = Driver.getDriver().findElements(By.tagName("a"));
    List<String> names = new List();
for(WebElement elem : aTagList){
return names;

public boolean comapreLists(List<String> list1, List<

If you want to assert it using testNG

int number : your expected link value
public void homeLinkNumberTest(int number, List<String> expectedNames){

This might be works.

|improve this answer|||||
  • 1
    I guess you didn't got my question, I want use this whole method in a assertion, So How can we achieve that?, for eg, I am returning boolean value So I have added a assertion Assert.assertTrue(condition);, what should be the assert condition for this. – jay Jul 9 '19 at 11:04
  • You'd better seperate assertion code from Selenium actions. I wonder if you know about the epected number of links for the test. – junghan Jul 9 '19 at 11:15
  • can we assert name of all links using assert – jay Jul 9 '19 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.