How to validate web page link is broken or not?

Hi,

I was able to get all the link URL in a web page. I want to know,

  1. how to get the name of the link
  2. how to validate link URL is broken(404) or accessible(200)

Glad if anyone help me on this.

Thushar

Q2 first. Did you try…

As for Q1, when you say “name” what do you mean? <a href=...> elements don’t usually have a name attribute.

You can retrieve the value of any attribute though.

hello,

with native selenium, check this page
https://www.guru99.com/find-broken-links-selenium-webdriver.html

Thanks Thomas for your reply.

Q2: I am getting a stepfailedException.

I have set up the test as below,
image

For Q1:
I can get the url of those links, but how can I get the link names?
image

image

Unfortunately for you, the developers of your page have not used recognized standards to construct links. Links are HTML <a> elements that look something like this:

<a href="https://...">some text</a>

The href attribute informs the browser where to link to.

Your page uses this non-standard approach:

<a href="#">

This means when you try to use verifyLinksAccessible, WebDriver can’t verify the links since, in effect, they don’t exist.

Back to Q1: I still don’t know for sure what you mean by “how can I get the link name?”

Assuming you mean the text within the <a>...</a> element, try to use WebUI.getText(). But again, the developers may be doing something odd with the <span> which I can’t see from your screenshot.

i may misunderstood a bit the topic.
for me, to verify if a link is accesible means it is clickable …
to verify if the link is broken or not (returns 200 or > 400 response code) you can simply make a get request, provided you have the url, using the Webservice API.
not sure if it helps …

Sure. But that’s not the same as verifying the link works <- the thing a user would do. You’re verifying an endpoint is responding.

No doubt, there is JavaScript behind that <a> tag, but since it’s Chrome it’s considered “secret” :roll_eyes:

agreed. this is why we may have to detail a bit on the ‘accesibility’ from the OP point of view.
for me it can also mean that ‘it is readable by screen readers and can be accessed by using keyboard only’

(yeah, for a while i had to do that kind of accesibility testing … and it is a pain in the ass, cannot be automated)

so, sometime, splitting the problem in smaller pieces may help

imho, there is no point to actualy check how a click on the ‘rendered link’ is behaving, if the target url is not valid, unless is subject for negative testing.

1 Like

Thanks bionel for your reply. Could you please tell me more about Webservice API, where I can find them? I tried to find groovy method/programs that can be used to get response code, but still no luck.

I am using following code in UFT. Can I use it in Katalon with groovy?

Set objWinHTTP = CreateObject(“WinHttp.WinHttpRequest.5.1”)
objWinHTTP.Open “GET”, sURL, False
objWinHTTP.SetRequestHeader “User-Agent”, “Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)”
objWinHTTP.Send
iReturnVals = objWinHTTP.Status

@thushar.ameen see the docs, you have already provided objects and keywords for such requests:

https://docs.katalon.com/katalon-studio/docs/ws-send-request.html#returns

will be a bit tricky, since mostly your project is started with the web template, but you can use also the webservice objects into it

see also:

After successfully login, when I am trying to get working urls using code:
@When(“Collect all the links in the web page based on ‘a’ tag”)
def collect_links() {

	List<String> allURLs = WebUI.getAllLinksOnCurrentPage(true, [])
	println ('Total links on page: ' +allURLs.size())
	
			
	for(String aURL : allURLs) {
		println aURL;
		WebUI.verifyLinksAccessible(allURLs)
	   }

Getting below errors:
Login Sucessfully true
2020-03-24 06:05:18.547 DEBUG c.k.k.c.c.keyword.CucumberReporter - ✓ I am at home page
2020-03-24 06:05:18.550 DEBUG c.k.k.c.c.keyword.CucumberReporter - STEP Collect all the links in the web page based on ‘a’ tag
Total links on page: 39
https://connect.facebook.net/signals/config/1630419187211975?v=2.9.15&r=stable
2020-03-24 06:05:21.920 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to send request (Root cause: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.kms.katalon.core.webservice.common.RestfulClient.response(RestfulClient.java:207)
at com.kms.katalon.core.webservice.common.RestfulClient.sendRequest(RestfulClient.java:140)
at com.kms.katalon.core.webservice.common.RestfulClient.send(RestfulClient.java:75)
at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:42)
at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)
at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)
at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:38)
at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:33)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.builtin.VerifyLinksAccessibleKeyword$_verifyLinksAccessible_closure1.doCall(VerifyLinksAccessibleKeyword.groovy:42)
at com.kms.katalon.core.webui.keyword.builtin.VerifyLinksAccessibleKeyword$_verifyLinksAccessible_closure1.call(VerifyLinksAccessibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.VerifyLinksAccessibleKeyword.verifyLinksAccessible(VerifyLinksAccessibleKeyword.groovy:37)
at com.kms.katalon.core.webui.keyword.builtin.VerifyLinksAccessibleKeyword.execute(VerifyLinksAccessibleKeyword.groovy:32)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyLinksAccessible(WebUiBuiltInKeywords.groovy:3723)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyLinksAccessible$6.call(Unknown Source)
at progression.StudentPortalLinkVerification.collect_links(StudentPortalLinkVerification.groovy:31)
at cucumber.runtime.Utils$1.call(Utils.java:26)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:20)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:48)
at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
at cucumber.runner.TestStep.executeStep(TestStep.java:55)
at cucumber.runner.TestStep.run(TestStep.java:42)
at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)
at cucumber.runner.TestCase.run(TestCase.java:47)
at cucumber.runner.Runner.runPickle(Runner.java:44)
at cucumber.runtime.Runtime.runFeature(Runtime.java:120)
at cucumber.runtime.Runtime.run(Runtime.java:106)
at cucumber.api.cli.Main.run(Main.java:35)
at cucumber.api.cli.Main$run.call(Unknown Source)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$_runFeatureFile_closure1.doCall(CucumberBuiltinKeywords.groovy:76)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$_runFeatureFile_closure1.doCall(CucumberBuiltinKeywords.groovy)
at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)
at com.kms.katalon.core.keyword.internal.KeywordMain$runKeyword.call(Unknown Source)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:46)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile$0.callStatic(Unknown Source)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:169)
at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile.call(Unknown Source)
at WSVerification1584990278906.run(WSVerification1584990278906:2)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:146)
at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:140)
at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:123)
at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:115)
at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:103)
at com.kms.katalon.core.main.TestCaseMain.runFeatureFile(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain$runFeatureFile$0.call(Unknown Source)
at TempTempCase1584990274355.run(TempTempCase1584990274355.groovy:23)
Caused by: java.security.cert.CertificateException: No X509TrustManager implementation available
… 61 more
)

And html is also standard:

hi,
you are missing ssl certification
SSLHandshakeException: java.security.cert.CertificateException
your urls are using ssl certification
ouh and here you have typo
for(String aURL : allURLs) {
println aURL;
WebUI.verifyLinksAccessible(allURLs)
}
should be
WebUI.verifyLinksAccessible(aURL)