I have some scripts in the page: like or .
What is the approach to find any content portion of such scripts? Like the content of GTM dataLayers or a presence of correct Google Analytics tracking code?
I have some scripts in the page: like or .
What is the approach to find any content portion of such scripts? Like the content of GTM dataLayers or a presence of correct Google Analytics tracking code?
Unfortunately, the actual content of a script element (certainly where type=âtext/javascriptâ) is not part of the DOM. Since every method you try in WebUI is trying to access the DOM elements constructed from the HTML in the page, it simply wonât work.
Worse, there is no solid consensus for how to achieve your aim. The closest youâll find is to re-fetch the source using an ajax call in JavaScript. Then youâll have the whole text content in a variable that you can examine and determine correctness.
There is a quite lengthy discussion here, which may help:
and here:
Is it maybe possible to act with something like VerifyTextPresent and to narrow down the selection with regex like _/<script\\b[^>]>(.?)<\/script>/i
_
I mean, sure it could be a method to run a regex through the whole page source code, butâŚit isnât a kind of nice solutionâŚ
Is it maybe possible to act with something like VerifyTextPresent
No, for the reasons I explained to you.
Challenge: prove me wrong B)
WaitâŚ
If the script element does not use src=âsome/path/to/a/file.jsâ, and you can figure out how to get the entire source for the HTML, then I can imagine there is a way to examine the resultant text to look for arbitrary strings.
Challenge #2: Figure this out and post back here to teach me something
yes, itâs not a kind of script violating same-origin policy.
Well, iâm getting already the the whole source code with:
import org.openqa.selenium.WebDriver as WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
WebUI.openBrowser(ââ)
WebUI.navigateToUrl(âhttps://www.example.comâ)
WebDriver driver = DriverFactory.getWebDriver()
attribute = driver.getPageSource()
WebUI.verifyMatch(attribute, âGTM-ABCXYZâ, false)
But verifyMatch still fails, with or without regex. And, it would be nice to narrow down the whole source code to only content of scriptâŚ
BTW: in this thread is the same problem: http://forum.katalon.com/discussion/7397/checking-page-source-for-particular-elements-presence
WebUI.verifyMatch(attribute, âGTM-ABCXYZâ, false)
But verifyMatch still fails,
WebUI.verifyMatch() is a pitfall.
Have a look at this:
I could reproduce your case.
The target HTML (with Google Tag Manager ) has such :
....
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-ABC0XYZ"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div class="container">
....
I write a mimic of your test case as follows:
import java.util.regex.Matcher
import java.util.regex.Pattern
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
Pattern pattern = Pattern.compile(/GTM-[\w]+/)
WebUI.openBrowser('')
WebUI.navigateToUrl('http://your.target.site/your_testbed.html')
WebDriver driver = DriverFactory.getWebDriver()
def pageSource = driver.getPageSource()
WebUI.comment("${pageSource}")
Matcher matcher = pattern.matcher(pageSource)
def found = matcher.find() // call find() method rather than matches() method
WebUI.verifyEqual(found, true)
def found2 = matcher.matches()
WebUI.verifyEqual(found2, false)
WebUI.closeBrowser()
When I executed the test case, I got following messages in the log:
[START] - Start action : Statement - found = matcher.find()
[END] - End action : Statement - found = matcher.find()
[START] - Start action : verifyEqual
[INFO] - Comparing actual object 'true' with expected object 'true'
[PASSED] - Actual object 'true' and expected object 'true' are equal
[END] - End action : verifyEqual
...
[START] - Start action : Statement - found2 = matcher.matches()
[END] - End action : Statement - found2 = matcher.matches()
[START] - Start action : verifyEqual
[INFO] - Comparing actual object 'false' with expected object 'false'
[PASSED] - Actual object 'false' and expected object 'false' are equal
[END] - End action : verifyEqual
----------
Please note the difference of the following 2 lines:
def found = matcher.find()
def found2 = matcher.matches()
and read the Javadoc of java.util.regex.Pattern https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html to find a description.
it would be nice to narrow down the whole source code to only content of scriptâŚ
OK, if you want narrowing down somehow, how about this?
Pattern pattern = Pattern.compile('https://www.googletagmanager.com/ns.html\\?id=GTM-[\\w]+')
For me only
def found = matcher.find()
WebUI.verifyEqual(found, true)
was working.
âŚ
I was searching for two patterns
Pattern pattern = Pattern.compile(/ABCXYZ/) // id of my container
and for non existing pattern.
âŚ
def found = matcher.find()
WebUI.verifyEqual(found, true)
was working in both test cases - test passes, if pattern exists, test fails, if pattern not exists.
âŚ
def found2 = matcher.matches()
WebUI.verifyEqual(found2, true)
fails the test, if pattern exist, and passes the test, if pattern not exist.
Behavior of
def found2 = matcher.matches()
WebUI.verifyEqual(found2, false)
was the same - test fails if pattern exists, test passed, if pattern not exist,
Iâve found a way to find the portion of the script (or any portion of the DOM) by using some of the ideas above.
Create a custom keyword with the string to compare as arg
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
public class findText {
@Keyword
def findTextFromHTMLSource(String compareString) {
String url = WebUI.getUrl()
WebDriver driver = DriverFactory.getWebDriver()
String pageSource = driver.getPageSource()
if(pageSource.contains(compareString)) {
KeywordUtil.logInfo('Search String Found')
}
else {
KeywordUtil.markFailed('Search String Missing')
}
}
}
Call the custom keyword with the desired search string.
Hope this helps.
Cheers!