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;

@Listeners({SoftAsserts.class})
public class LandingPageTest {

    @Test
    public void allLocatorsOnPageShouldWork() {

        //arrange
        Configuration.assertionMode = SOFT;

        //act
        new LandingPage().goTo();

        //assert
        TestUtilities.checkLocatorsForPage(LandingPage.class);
    }
}
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() {
Selenide.open("http://newtours.demoaut.com/");
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 org.testng.Assert.fail;

public class TestUtilities {

static final Logger log = LogManager.getLogger();

public static void checkLocatorsForPage(Class<?> classToInspect) {
try {
Object pageInstance = classToInspect.newInstance();

log.info("Checking locators for " + classToInspect.getSimpleName());

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

SelenideElement element = (SelenideElement) field.get(pageInstance);

log.info(String.format("%s.%-40s", classToInspect.getSimpleName(), field.getName()));

element.shouldBe(Condition.visible);
}

}
} catch (InstantiationException e) {
fail(e.getMessage());
} catch (IllegalAccessException e) {
fail(e.getMessage());
}
}
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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