Get nth element text and Get element text shall return a String instead of Page Object

bug-report

#1

Reporting against Webtestit version 0.24.0

Get nth element text and Get element’s text should return text of the element instead of the page object.

Following are the sample code generated by Webtestit

For Get element’s text

public CheckoutPo performNewAction6() {
String firstNameFieldText = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.firstNameField)).getText();

return this;
}

String shall be returned instead of PageObject

  1. For Get nth element text

public CheckoutPo performNewAction6(Integer index) {
this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.firstNameField));
List firstNameFieldAllElements = driver.findElements(this.firstNameField);

WebElement firstNameFieldElementByIdx = firstNameFieldAllElements.get(index);
String firstNameFieldText = firstNameFieldElementByIdx.getText();

return this;
}

For Get nth element
image
Sample code generated by Webtestit

public ItemsOverviewPo performNewAction(Integer index) {
this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.item1AddToCartButton));
List item1AddToCartButtonAllElements = driver.findElements(this.item1AddToCartButton);

WebElement item1AddToCartButtonElementByIdx = item1AddToCartButtonAllElements.get(index);

return this;
}

This should return WebElement instead of PageObject


#2

Hello @janesh
For the first two requests ( Get element’s text and Get nth element text ), we will consider your suggestion of returning the actual String, and I will let you know about the outcome.
The Get nth element method’s return type will not be changed, hence returning a Web Element is not the very best practise as far as the Page Object pattern goes. So the PageObject remains in the return statement, allowing the user to modify the method by his needs.
And of course, many thanks for your feedback!


#3

To clarify the nth element scenario. A pageobject should not return anything that is related to Webdriver/Selenium or the likes to the Testmethod as that means that the clear separation of FrameworkCode and TestCode is violated.

The idea with the get nth element is that you start with a stub in your PO and than continue what you’d like to do and return an appropriate value. E.g one example could be to get the nth element and return it’s class by using

public String performNewAction(Integer index) {
  this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.item1AddToCartButton));
  List item1AddToCartButtonAllElements = driver.findElements(this.item1AddToCartButton);

  WebElement item1AddToCartButtonElementByIdx = item1AddToCartButtonAllElements.get(index);
  
  // continue with your code here
  String classes = item1AddToCartButtonElementByIdx.getAttribute("class");
  return classes;
}

#4

Hi @smatijas
Do we have any update on this?


#5

Hello again @janesh!
We have discussed this feature and have it in our backlog. As this feature may affect some other parts of the app, like changing the default templates, and this requires some more work to be done until the feature is ready. I will make sure to inform you about the progress.