Reputation: 45
I want to show in Extent Report the name of the web element ("USER_LOGIN" in my example); how do I get it?
I tried to use the getText()
but it replies with a blank as result.
For example this is site code:
<input class="form_control w100p ffg big" type="text"
name="USER_LOGIN" value="[email protected]" maxlength="255" placeholder="E-mail">
and this is what I'm trying to do:
public void verifyElementExist(WebElement weElem) throws ParserConfigurationException, SAXException, IOException
{
try
{
weElem.isDisplayed();
extnTest.log(LogStatus.INFO, "Element exist - " + weElem.getText());
}
catch (Exception e)
{
...
}
}
Upvotes: 1
Views: 1393
Reputation: 111
Your code must return blank!
I believe you are confusing with name as an attribute and name as the Element's name. In your case, input box has a name as an attribute which you can use to find element or developers can use that to parse data once user hits on submit button.
Your code (getText()) would have returned UserName if the HTML code was something like this
**UserName**
<input class="form_control w100p ffg big" type="text" name="USER_LOGIN" value="[email protected]" maxlength="255" placeholder="E-mail">
You certainly would have got UserName in your log.
Most of the element on a professional project should have a name to it, but there is no obligation to have it. So promising the element name on a framework level is not justified. You might need to do some filtering while logging if at all you want to log their names.
Upvotes: 0
Reputation: 12255
You can get details about difference between getText
and getAttribute
here.
Code below will get name or id:
String nameOrId = weElem.getAttribute("name") == null ? weElem.getAttribute("name") : weElem.getAttribute("id");
if (name == null)
name = "uknown";
Not all elements will have id or name, some elements you'll find using xpath or css selectors and code above will not do job for you. Easy way is to modify you method like below:
public void verifyElementExist(String name, WebElement weElem) throws ParserConfigurationException, SAXException, IOException
{
try
{
weElem.isDisplayed();
extnTest.log(LogStatus.INFO, "Element exist - " + name);
}
catch (Exception e)
{
...
}
}
Upvotes: 1