Forum Discussion

jmoe's avatar
jmoe
Contributor
3 years ago
Solved

How to get the font color of a JTree item using Javascript

Hello,

I am able to get the individual items in a JTree, but only the Text method (and a few others).  It appears however, there are no properties/methods to get the font color, and there are no native methods to access either.  Do you know how I could do this?  Thx.

  • Hi jmoe.


    One option is to take a picture of the JTree Item and compare it to a static image.  I assume that the JTree Item has a picture function. If not the parent window might have the function. If that isn’t the case you could position the window in a specific location and take a picture of the screen.

     

    You could also try taking a picture and compare the pixel(s) color.  I am not sure what language you are programming in, but below is an example that will take a picture and compare the first pixel to a desired constant color. I have not tried any of this code, but it should give you some direction.  I hope this helps.

     

        function comparePixel()

        {

          // take a picture at the specified location with a width and height of 1

          var pic = jTreeItem.Picture(desiredLeftPosition, desiredTopPosition, 1, 1);

          // compare to the desired color 

          if (pic.Pixels(0,0) == clMenuText) // color values can be found here: https://support.smartbear.com/testcomplete/docs/scripting/colors.html

          {

            // whatever you need to do....

          }

        }

     

    A more detailed explanation for others...

     

        function comparePixel()

        {

          var parentControl = Aliases.Product; // the parent control could be a window if searching for a control like a textbox

          // use the correct property names for your window or control

          var propNames = new Array(propName1, propName2);

          // use the correct property values for your window or control

          var propValues = new Array(propValue1, propValue2);

          // specify a search depth.

          var depth = 100;

          // find the window/control from the parent

          var control = parentControl.FindChild(propNames, propValues, depth);

          // made up number use your distance to the desired left position

          var x = 10;

          // made up number use your distance to the desired top position

          var y = 10;

          // 1, 1 is the width and height of the image

          var pic = control.Picture(x, y, 1, 1); 

          // use this to get the constant color value if you don't know what that value is...otherwise it can be commented out...

          var color = pic.Pixels(0,0); 

          // color values can be found here: https://support.smartbear.com/testcomplete/docs/scripting/colors.html

          // if it is a custom color you can see an example function at the above link that will allow R,G,B comparison

          // specify the color you want to compare

          if (pic.Pixels(0,0) == clRed)

          {

            // whatever you need to do....

          }

        }

4 Replies

  • Hi jmoe.


    One option is to take a picture of the JTree Item and compare it to a static image.  I assume that the JTree Item has a picture function. If not the parent window might have the function. If that isn’t the case you could position the window in a specific location and take a picture of the screen.

     

    You could also try taking a picture and compare the pixel(s) color.  I am not sure what language you are programming in, but below is an example that will take a picture and compare the first pixel to a desired constant color. I have not tried any of this code, but it should give you some direction.  I hope this helps.

     

        function comparePixel()

        {

          // take a picture at the specified location with a width and height of 1

          var pic = jTreeItem.Picture(desiredLeftPosition, desiredTopPosition, 1, 1);

          // compare to the desired color 

          if (pic.Pixels(0,0) == clMenuText) // color values can be found here: https://support.smartbear.com/testcomplete/docs/scripting/colors.html

          {

            // whatever you need to do....

          }

        }

     

    A more detailed explanation for others...

     

        function comparePixel()

        {

          var parentControl = Aliases.Product; // the parent control could be a window if searching for a control like a textbox

          // use the correct property names for your window or control

          var propNames = new Array(propName1, propName2);

          // use the correct property values for your window or control

          var propValues = new Array(propValue1, propValue2);

          // specify a search depth.

          var depth = 100;

          // find the window/control from the parent

          var control = parentControl.FindChild(propNames, propValues, depth);

          // made up number use your distance to the desired left position

          var x = 10;

          // made up number use your distance to the desired top position

          var y = 10;

          // 1, 1 is the width and height of the image

          var pic = control.Picture(x, y, 1, 1); 

          // use this to get the constant color value if you don't know what that value is...otherwise it can be commented out...

          var color = pic.Pixels(0,0); 

          // color values can be found here: https://support.smartbear.com/testcomplete/docs/scripting/colors.html

          // if it is a custom color you can see an example function at the above link that will allow R,G,B comparison

          // specify the color you want to compare

          if (pic.Pixels(0,0) == clRed)

          {

            // whatever you need to do....

          }

        }

    • jmoe's avatar
      jmoe
      Contributor

      Thanks for the idea chriscc. I like that out of the box thinking!

      • chriscc's avatar
        chriscc
        Contributor

        A  couple of things to help get the correct locations for the pixel(s)...

         

        You can log the picture, so it shows in the TestComplete messages.  More of a Trial and Error approach, but you can change the rectangle values of the pic until you get the desired location.

         

        var pic = window.Picture(left, top, width, height);

         

        Log.Picture(pic "Picture of window");

         

        Another option, if you have something like MSPaint or maybe GIMP, you can get the pixels using their tools.  For example, in MSPaint paste a screenshot of the window or control and use the Selection tool to find the left, top values to your pixel…like this: