Skip to content
Advertisement

How to check if all the elements present on the page

I am using page object model using page factory selenium using Java

I have page class for example:

`public class Login extends BasePage {

@Findby(css ="Abc")
WebElement ele1;

@Findby(css ="xyz")
WebElement ele2;

@Findby(css ="xyz")
WebElement ele3;

public void verifyVisibilityOfAllElements {
List<Webelements> elementList = new ArrayList<Webelements>;
elementList.add(ele1);
elementList.add(ele2);
elementList.add(ele3);
for(Webelements ele:elementList){
if(ele.isDisplayed){
system.out.println("element is displayed" +ele);
} else {
system.out.println("element is not displayed" +ele);
}
}
}
}`

I want to check if all the elements defined in the page class are displayed on the webpage. I want to perform this on in all my page classes. Looking for reusable generic method which can be written in BasePage and can be reuse in all the other pages.

Advertisement

Answer

You can so like this below using Java Reflection Create a Abstract Base Class package com.af.steps;

import io.github.bonigarcia.wdm.WebDriverManager;
import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

public abstract class BasePage<T> {

   static WebDriver driver;

   static {

      //just for testing purpose , driver instance should be handled in separate class

      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
   }


   public void base(Class<?> pageClass) throws InstantiationException, IllegalAccessException {

      T page = null;

      page = (T) PageFactory.initElements(driver, pageClass);
      Field[] fields = page.getClass().getDeclaredFields();
      for (Field field : fields) {
         field.setAccessible(true);
         if (field.getType().equals(WebElement.class)) {
            WebElement value = (WebElement) field.get(page);
            if (value != null) {
               System.out.println(value.isDisplayed());
            }
         }
      }
   }


   protected abstract void checkDisplayed() throws InstantiationException, IllegalAccessException;
}

Your Page Class should call it like this , I have Tested this On google and works fine Implement the checkDisplayed and class the base method from Abstract class and pass the Class to it, it will initialize the page suing page factory and check if all webelements are displayed or not

import org.apache.poi.ss.formula.functions.T;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class PageClass extends BasePage<T> {

   @FindBy(xpath = "//input[@class="gLFyf gsfi"]")
   WebElement ele1;

   @FindBy(xpath = "//input[@class="gNO89b"]")
   WebElement ele2;

   @FindBy(xpath = "//input[@class="RNmpXc"]")
   WebElement ele3;

   void loadUrl() {
      driver.get("https://www.google.com/");
   }

   public static void main(String[] a) throws IllegalAccessException, InstantiationException {

      PageClass pageClass = new PageClass();
      pageClass.loadUrl();
      pageClass.checkDisplayed();
   }


   @Override
   protected void checkDisplayed()
       throws InstantiationException, IllegalAccessException {
      base(this.getClass());
   }

}

you will an output like below for above example

true
false
false
Advertisement