After much thought eventually decided to post my question at the risk to get banned for a stupid question: D Therefore apologize in advance
So I write automation test using java with selenium. Purpose - to compare comments value on different pages on the website. But when I need to get this value through find.Element / s there is much duplicate code I made a separate method but for the homepage there is exception - I have to search through certain webElement / s (in this case - variable article) unlike other pages where I can just use driver.find ...
So I wanna know what the most efficient way to use my commentsAvailabilityAndValue(); method to find this value. There is a possibility for example somehow change whats parameters method takes but only in case when we call it for homepage? Or make "reference/inheritance" to this method but instead driver use article? I hope you understood.
Here is snippets:
@Test
public void delfiTest() {
WebDriver driver = new ChromeDriver();
//Here would be my finals By.xpath driver settings etc. but it's not so important so let's skip it.Add only one example
private final By COMMENT_COUNT_HOME_PAGE = By.xpath(".//a[@class = 'comment-count text-red-ribbon']");
//Getting needed article from the list.
List<WebElement> articles = driver.findElements(ARTICLES_HOME_PAGE);
articles.removeIf(arr -> arr.getText().isEmpty());
WebElement article = articles.get(2);
//Getting comment count on homepage. We cannot use my methods because we need search through variable article insted of driver
int commentsValueHP = 0;
//Checking if comment count even exist.
boolean exists = article.findElements(COMMENT_COUNT_HOME_PAGE).size() != 0;
if (exists) {
WebElement commentCount = article.findElement(COMMENT_COUNT_HOME_PAGE);
String commentCountText = commentCount.getText();
String replacedText = commentCountText.replaceAll("\\D+", "");
commentsValueHP = Integer.parseInt(replacedText);
}
//Proceeding to the article page.
titleHP.click();
//Getting commentsValue on article page. Here everything ok
int commentsValueTitleAP = commentsAvailabilityAndValue(COMMENT_COUNT_TITLE_ARTICLE_PAGE);
Assertions.assertEquals(commentsValueHP, commentsValueTitleAP, "Comments value on homepage and article page(in title) doesn't match.");
//Here would be same steps but with other pages
//METHODS:
private int commentsAvailabilityAndValue(By selector) {
int commentsValue = 0;
boolean exists = driver.findElements(selector).size() != 0;
if (exists)
{
commentsValue = getCommentsValue(selector);
}
return commentsValue;
}
private int getCommentsValue(By selector) {
WebElement commentCount = driver.findElement(selector);
String commentCountText = commentCount.getText();
String replacedText = commentCountText.replaceAll("\\D+", "");
int commentsValue = Integer.parseInt(replacedText);
return commentsValue;
}
Aucun commentaire:
Enregistrer un commentaire