Forum Discussion

jsc's avatar
jsc
Regular Contributor
10 years ago

Chrome: window.getcomputedvalue in scriptextensions

Hey all,

 

I need to check a computed css value.

When I use the console of chrome the function is like this:

window.getcomputedvalue(document.getelementbyid('UiMask').opacity)

 

I was not able to transfer this to my scriptextensions (Javascript).

I tried different variations with various browser-elements but none of them worked.

 

Can anyone help me?

 

Best regards,

Joachim

  • Hi Joachim,

    In script extensions, you need to address web pages as Sys.Browser().Page(url). From there, you can access the document object as .contentDocument, and the window object as .contentDocument.defaultView or .contentDocument.parentWindow (in IE).

    To get computed CSS styles, we recommend using the cross-browser getStyle function from the TestComplete documentation:

    http://support.smartbear.com/viewarticle/62849/#Example


    So, your code should be something like this:

    var element = Sys.Browser().Page(...).contentDocument.getElementById("UiMask");
    var opacity = getStyle(element, "opacity");


    function getStyle(element, styleProp) { var document = element.ownerDocument; if (aqObject.IsSupported(document, "defaultView")) { // Internet Explorer 9+, Firefox, Chrome, Opera var style = document.defaultView.getComputedStyle(element, ""); return style[styleProp]; } else { // Internet Explorer 7 - 8 return element.currentStyle[styleProp]; } }
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Joachim,

    In script extensions, you need to address web pages as Sys.Browser().Page(url). From there, you can access the document object as .contentDocument, and the window object as .contentDocument.defaultView or .contentDocument.parentWindow (in IE).

    To get computed CSS styles, we recommend using the cross-browser getStyle function from the TestComplete documentation:

    http://support.smartbear.com/viewarticle/62849/#Example


    So, your code should be something like this:

    var element = Sys.Browser().Page(...).contentDocument.getElementById("UiMask");
    var opacity = getStyle(element, "opacity");


    function getStyle(element, styleProp) { var document = element.ownerDocument; if (aqObject.IsSupported(document, "defaultView")) { // Internet Explorer 9+, Firefox, Chrome, Opera var style = document.defaultView.getComputedStyle(element, ""); return style[styleProp]; } else { // Internet Explorer 7 - 8 return element.currentStyle[styleProp]; } }
    • jsc's avatar
      jsc
      Regular Contributor

      Hi Helen,

       

      thanks a lot, this solution works perfectly fine.

       

      Best regards,

      Joachim