Check all page locators with Reflection and Soft Assertions

Selenium tests often fail not because something in the web app is actually broken, but because the layout of the page has changed. In such case Selenium cannot locate the button it tries to click even though it is technically still there. All we need to do to fix such problems is to replace the old locator with a new one. In order to find all such broken locators I wrote this simple utility.

package blog.learningtotest.examples;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.testng.SoftAsserts;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import static com.codeborne.selenide.Configuration.AssertionMode.SOFT;

public class LandingPageTest {

    public void allLocatorsOnPageShouldWork() {

        Configuration.assertionMode = SOFT;

        new LandingPage().goTo();

package blog.learningtotest.examples;

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;

import static com.codeborne.selenide.Selectors.byName;
import static com.codeborne.selenide.Selectors.byText;
import static com.codeborne.selenide.Selenide.$;

public class LandingPage {

public SelenideElement signOnButton = $(byText("SIGN-ON_broken"));
public SelenideElement registerButton = $(byText("REGISTER"));
public SelenideElement supportButton = $(byText("SUPPORT_broken"));
public SelenideElement contactButton = $(byText("CONTACT"));

public SelenideElement userNameInput = $(byName("userName_broken"));
public SelenideElement passwordInput = $(byName("password"));
public SelenideElement signInButton = $(byName("login"));

public LandingPage goTo() {"");
return this;
package blog.learningtotest.examples;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;

import static;

public class TestUtilities {

static final Logger log = LogManager.getLogger();

public static void checkLocatorsForPage(Class<?> classToInspect) {
try {
Object pageInstance = classToInspect.newInstance();"Checking locators for " + classToInspect.getSimpleName());

for (Field field : classToInspect.getDeclaredFields()) {
if (field.getType().getSimpleName().equals("SelenideElement")) {

SelenideElement element = (SelenideElement) field.get(pageInstance);"%s.%-40s", classToInspect.getSimpleName(), field.getName()));


} catch (InstantiationException e) {
} catch (IllegalAccessException e) {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s