Looking for duplicate links in a page

Good morning,

I’m looking for a way to check and see if duplicate links come up when a page is loaded with given drop-down values.

Example: I want to see all the stories that a given writer posted in January. So I select the drop down menu with the writer’s name, the start date of 1/1, and the end date of 1/31. How do I make sure that none of the links I get are duplicates? I tried using “verify text present” but that only shows me if the text is there, not how many times it appeared.

maybe a while loop?

I could try to figure that out if needed, I was hoping there was a way to do it in manual mode since I’m pretty new…

I’m not sure of a way already built into Katalon…but I would recommend playing with script view you can do so much with it.

However if you are ok with it here is the code to copy and paste. This would work better as a keyword but here is the raw code:

import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver

// get the text of the body element
WebDriver driver = DriverFactory.getWebDriver();
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();

// count occurrences of the string
int count = 0;

// search for the String within the text
while (bodyText.contains("Writer's Name Here")){

    // when match is found, increment the count
    count++;

    // continue searching from where you left off
    bodyText = bodyText.substring(bodyText.indexOf("Writer's Name Here") + "Writer's Name Here".length());
}

countIs = count.toString()
WebUI.comment(countIs)

I found and modified this from https://stackoverflow.com/questions/18442895/how-many-times-a-text-appears-in-webpage-selenium-webdriver/18449828

1 Like

You should be careful here, as two links that share the same text are not necessarily duplicates of each other (they may have other attributes that make them unique, most importantly the href attribute).

That being said, if you’re sure that a link’s text guarantees its uniqueness on your page, then the goal becomes “count the number of occurrences of the link with text ‘some text’, and assert that that number is either 0 or 1”.

There’s no built-in keyword that you will be able to call from the manual view to accomplish this. You will need to do some scripting, and it likely won’t be the last time you will need to do something like this, so why not start learning it now? Luckily, this one won’t be too bad. I’ll show you a brute-force way to do it, then a better way that I would do it if I were in your shoes:

Brute-force approach:

1.) Create a Test Object named “links” that grabs every link on the page:

2.) Go to the script view for your Test Case and enter the following code in the appropriate place (wherever you want to check for duplicates):

import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.common.WebUiCommonHelper

List<WebElement> links = WebUiCommonHelper.findWebElements(findTestObject("links"), 30);
int linkCount = 0;
for(WebElement link : links) {
	if(link.getText().equals("some text")) {
		linkCount++;
	}
}
assert linkCount <= 1;

A better way:

1.) Create a Test Object named “links” that grabs all links that have a given text:

2.) Go to the script view for your Test Case and enter the following code in the appropriate place (wherever you want to check for duplicates):

import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.common.WebUiCommonHelper

List<WebElement> links = WebUiCommonHelper.findWebElements(findTestObject("links", [("text"): "some text"]), 30);
assert links.size() <= 1;

In both of these cases, you would replace “some text” with the link text that you are trying to validate.

1 Like