harish reddy
harish reddy

Reputation: 1

Can we able to print the object along with the xpath in Page object model

I am using POM using testng+java+maven.Can we able to print By objectname in the logs/extendreport for the reporting purpose? I am using Log4j and extentreports.

Example : My commonfunction class contains all the actions perform for the testcases:

public void click(By element) {
        WebElement webElement = getElement(element);
        try {

            Log.info("Clicking on the webelement " + element);
            webElement.click();
            ExtentTestManager.getTest().info("clicking on the webelement - " + element);
        }

        catch (NoSuchElementException e) {
            Log.error(e.getMessage());
            ExtentTestManager.getTest().fail(e);
            throw new TestException(String.format("The following element is not clickable: [%s]", element));
        }
    }

and in my pageobject class im declaring xpath as:

By clearFormButton = By.xpath("//*[@id='createPopulation:j_idt198']");

and in my test step:
commonfunction.click(clearFormButton);

Actual output : Clicking on the webelement By.xpath: 
//*[@id='createPopulation:j_idt198']

Expected output : Clicking on the webelement clearFormButton - By.xpath: 
//*[@id='createPopulation:j_idt198']

Upvotes: 0

Views: 417

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

You can use Reflection mechanism to get the value of foundBy field of the RemoteWebElement class instance like:

java.lang.reflect.Field field = element.getClass().getDeclaredField("foundBy");
field.setAccessible(true);
String foundBy = field.get(element).toString();

enter image description here

Your implementation of the Page Object Model design pattern is a little bit weird, by the way, my vision is that your test should not have any Selenium internals, so instead of having commonfunction.click(clearFormButton); in the test step you should be having something like CreatePopulationPage page = myCurrentPage.clickCreatePopulationButton();

Upvotes: 1

Murthi
Murthi

Reputation: 5347

The e.extraInfo.get("*** Element info") will give you locator and it's value.

log.debug(e.extraInfo.get("*** Element info"))

public void click(By element) {
        WebElement webElement = getElement(element);
        try {

            Log.info("Clicking on the webelement " + element);
            webElement.click();
            ExtentTestManager.getTest().info("clicking on the webelement - " + element);
        }

        catch (NoSuchElementException e) {
            log.debug(e.extraInfo.get("*** Element info"))
            Log.error(e.getMessage());
            ExtentTestManager.getTest().fail(e);
            throw new TestException(String.format("The following element is not clickable: [%s]", e.extraInfo.get("*** Element info"));
        }
    }

Upvotes: 0

Related Questions