Veryfing long text of code in the browser

Hi all,

what is a good way to verify long text of code like this one in Katalon ?

<script type='text/javascript'>
var stickyadstatus = "";
function fix_stickyad() {
  document.getElementById("stickypos").style.position = "sticky";
  var elem = document.getElementById("stickyadcontainer");
  if (!elem) {return false;}
  if (document.getElementById("skyscraper")) {
    var skyWidth = Number(w3_getStyleValue(document.getElementById("skyscraper"), "width").replace("px", ""));  
    }
  else {
    var skyWidth = Number(w3_getStyleValue(document.getElementById("right"), "width").replace("px", ""));  
  }
  elem.style.width = skyWidth + "px";
  if (window.innerWidth <= 992) {
    elem.style.position = "";
    elem.style.top = stickypos + "px";
    return false;
  }
  var stickypos = document.getElementById("stickypos").offsetTop;
  var docTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  var adHeight = Number(w3_getStyleValue(elem, "height").replace("px", ""));
  if (stickyadstatus == "") {
    if ((stickypos - docTop) < 60) {
      elem.style.position = "fixed";
      elem.style.top = "60px";
      stickyadstatus = "sticky";
      document.getElementById("stickypos").style.position = "sticky";

    }
  } else {
    if ((docTop + 60) - stickypos < 0) {  
      elem.style.position = "";
      elem.style.top = stickypos + "px";
      stickyadstatus = "";
      document.getElementById("stickypos").style.position = "static";
    }
  }
  if (stickyadstatus == "sticky") {
    if ((docTop + adHeight + 60) > document.getElementById("footer").offsetTop) {
      elem.style.position = "absolute";
      elem.style.top = (document.getElementById("footer").offsetTop - adHeight) + "px";
      document.getElementById("stickypos").style.position = "static";
    } else {
        elem.style.position = "fixed";
        elem.style.top = "60px";
        stickyadstatus = "sticky";
        document.getElementById("stickypos").style.position = "sticky";
    }
  }
}
function w3_getStyleValue(elmnt,style) {
  if (window.getComputedStyle) {
    return window.getComputedStyle(elmnt,null).getPropertyValue(style);
  } else {
    return elmnt.currentStyle[style];
  }
}
</script>

I’ve managed to scan an object and in its attributes I can select to detect it by text but keep getting an error that it is unable to locate the element. In Spy Web tool it sometimes works and finds an element using text propert but no always.

There is no “good way” to verify the text of code inside a <script> element. It can be done, but I would not recommend you do so:

  1. Code is volatile. It might change, frequently, based on developer changes. It might change based on user activity within the page. Either way, you are likely to end up with a flaky test.
  2. Katalon Studio is an automation tool. You are meant to automate user actions. Users cannot see script code in a browser under normal circumstances. For that reason, there is no optimized API available to help you accomplish this task.

That said, here’s how I would do it in JavaScript:

document.querySelector("script#unique-id-of-script").innerText

You would then parcel up that JavaScript and call it with WebUI.executeJavaScript().

Okay so diffrent question, how would you assert if the given text in the article is correct, let’s say it has 100 lines of text

If you want to use WebUI code, use WebUI.getText(). If you want to use JavaScript, the same API I showed you above.

1 Like

Thanks, gonna give it a try

WebUI.getText() is definitely what I needed but another issue is what’s the best way to verify match beetwen text in the object and expected text?

I was thinking to read expected text from file, assigning it to String and then use WebUI.verifyMatch, to match text from the object but it keeps throwing an error:
Unable to verify match between actual text and expcted text are not matched
even tho the texts are exactly the same, I copied and paste from the console to editor to check .

Here is my code :

File file = new File('Drivers/test.txt')

Scanner scan = new Scanner(file)

String fileContent = ''

while (scan.hasNextLine()) {
    fileContent = fileContent.concat(scan.nextLine() + '\n')
}

//Save to new file
//FileWriter writer = new FileWriter('Drivers/newfile.txt')
//
//writer.write(fileContent)
//
//writer.close()

System.out.println(fileContent)

WebUI.openBrowser('')

WebUI.navigateToUrl('https://loremipsum.io/generator/?n=5&t=p')

WebUI.verifyMatch(WebUI.getText(findTestObject('Page_Lorem Ipsum Generator/div_Lorem ipsu')), fileContent, false)

WebUI.verifyMatch() is strict, sensitive. If there is a single whitespace character different between the two texts, it will throw an error “Unable to verify match”. The problem is, the keyword does not tell you the reason why. So you will just get at a loss what to do next.

I think that you shouldn’t use WebUI.verifyMatch() keyword. You have to invent a function to verify 2 long multi-lines texts for yourself.

No. Most probably, not the same. There must be at least one different character. You need to find it.

When I want to compare 2 text files (possibly program codes) in Katalon Studio test scripts, I often use

this helps much.

1 Like