Reputation: 1
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
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();
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
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