method to gather all links. But the issue is this method gathers unwanted links as well which when clicked leads to script page of .js .
What i found is that the links which are visible on front end and i am concerned with have ‘a’ tags with href . If i locate the links with ‘a’ tag i would not be dealing with unwanted links like earlier. But i dont know how to gather the ‘a’ tag hrefs.
Please help!!!
You are interested in only <a href="..."> tags and not in other tags like <link>, <script>, <img>. Then you should not bother yourself with WebUI.getAllLinksOnCurrentPage. You can find all <a> elements by WebUI.findWebElements.
You disclosed the URL of your target web app : https://malasuat.express.shoptimize.in/
So I could make a sample code. Make a Test Case script, copy&paste the following code and run it.
import org.openqa.selenium.WebElement
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
String pageUrl = "https://malasuat.express.shoptimize.in/"
WebUI.openBrowser("")
WebUI.navigateToUrl(pageUrl)
TestObject tObjAnchors =
new TestObject("all anchors").addProperty("xpath", ConditionType.EQUALS,
"//a")
// find all <a> elements
List<WebElement> anchors = WebUI.findWebElements(tObjAnchors, 10)
boolean hasBrokenLinks = false
StringBuilder sb = new StringBuilder()
for (aTag in anchors) {
String url = aTag.getAttribute("href")
String statusCode = getResponseStatus(url)
sb.append("${statusCode} ${url}${System.lineSeparator}")
// check if this link is broken
if (statusCode != '200' && statusCode != '302') {
hasBrokenLinks = true
}
}
println("*** All <a> elements in ${pageUrl} ***")
print(sb.toString())
println("size of anchors: ${anchors.size()}")
WebUI.closeBrowser()
if (hasBrokenLinks) {
KeywordUtil.markFailed("${pageUrl} has one or more broken links")
}
/**
* try to make HTTP GET for this url and return the HTTP Status Code of the response
* @param url
* @return
*/
String getResponseStatus(String url) {
String status = '???'
if (url != null && url.startsWith('http')) {
RequestObject req = new RequestObject('check if this url is reachable')
req.setServiceType('REST')
req.setRestUrl(url)
req.setRestRequestMethod('GET')
// Send the request and get the response
ResponseObject res = WS.sendRequest(req)
status = res.getStatusCode() // '200' OK, '302' redirected, '403' etc
}
return status
}
This script emitted the following messages to the console.
And finally this script failed because it found some links in doubt:
2020-11-21 11:26:54.805 ERROR com.kms.katalon.core.util.KeywordUtil - ❌ https://malasuat.express.shoptimize.in/ has one or more broken links
2020-11-21 11:26:54.811 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/TC10 FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: https://malasuat.express.shoptimize.in/ has one or more broken links
at com.kms.katalon.core.util.KeywordUtil.markFailed(KeywordUtil.java:19)
at TC10.run(TC10:40)
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.TestCaseExecutor.runScript(TestCaseExecutor.java:339)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1605925398853.run(TempTestCase1605925398853.groovy:25)
2020-11-21 11:26:54.820 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC10
It runs too slowly. It runs slowly because Katalon Studio (free version) logs every single steps in Test Cases, and it emits very verbose output to the LogViewer.
The code is not modularised, not reusable at all. It can not process multiple web pages.
So have worked out to refactor the sample to solve these problems. I have made another post:
You can manipulate the logging level through the logging.something file in your project, even in free version.
Default for testcase execution is Debug, set it at Info level and see if it helps.
The logger you have to set it is shown into the console log
But I think it is not enough in some cases. Imagine that Mr.X learned the above sample code and created his own “Finding broken links” project where he imitated my code. Let me suppose Mr.X is NOT aware of “log.properties”. Then he will find his project runs slowly.
I thought it would be better to refactor the code. I moved the code fragments, which are repeatedly executed, out of Test Cases script into Keywords class. Katalon Studio does not emit any execution logs for Keywords, so the Keyword codes will be executed hundreds of times without emitting verbose logs regardless how log.properties file is configured.