cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to get Hex/RGB value of a color preview swatch, please help!

Regular Contributor

Trying to get Hex/RGB value of a color preview swatch, please help!

Hi everyone

 

In configuration page some colors are customizable. 

  • When clicked on any of the input fields, a color chooser pops up.
  • Selecting a color populates the field with the hex value.
  • The preview swatch updates to the selected color.

I am trying to access the hex value of the preview color. After some reading, this is what I came up with:

(I have noticed that swatch value shows up as Hex in Chrome but as RGB in IE!)

 

  var colorPrimary = page.QuerySelector(".evo-colorind");
  var style = page.contentDocument.defaultView.getComputedStyle(colorPrimary, "");
  Log.Message(style.background-color);

 

error 0x80020101.png

 

 

I am getting JavaScript runtime error 0x80020101 at 'style' var. Any help deeply appreciated.

14 REPLIES 14
Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

I am surprised that even the Smartbear support team is surprisingly quiet! Does this mean there is no way TestComplete can do this!!?

Community Hero

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

Actually, I think the difference is beyond even just whether or not it shows as RGB versus Hex...  I have a checkpoint set up that does something similar.... but the caveat that I have is that it will work with IE only.  I think the contentDocument property/object or some of the methods and properties of it are only available in IE.  That would be where I'd start, to put a breakpoint on that line and do some investigation as to why JavaScript is burping there... 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

Thanks Robert!

 

You are always there when I need help!  I almost gave up hope. I will give it a try tomorrow and let you know.

 

Dave

 

 

Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

Hi Robert,

 

As you suggested, I am using IE 11. Now I am getting runtime error:

 

            JavaScript runtime error.
      TypeError: Cannot read property 'color' of null
 
If you take a look at the above screenshot, the style (color) in named 'backgroud-color'. I am using the following to Log the color:
var style = page.contentDocument.defaultView.getComputedStyle(colorPrimary, "");
Log.Message(style.background-color);
I am not quite sure if I should be using 
Log.Message(style.background-color);
or
Log.Message(style.color);
 
Either way, I am getting the same error. 
 
Thank you.
Community Hero

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

In the code that I'm using, I get the element from the page in IE... then I get the "currentStyle.backgroundColor" property.  I think that's better than the getComputedStyle method.  See if that works better for you.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

Thank you. I will give it a try.

Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

I used:

var style = page.contentDocument.defaultView.currentStyle.backgroundColor(colorPrimary, "")

as you suggested.

 

I am getting:

               JavaScript runtime error.
              TypeError: Cannot read property 'backgroundColor' of undefined
Community Hero

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

backgroundColor is not a method, it's the actual property.   So, just compare that property to the desired value.

 

 


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Regular Contributor

Re: Trying to get Hex/RGB value of a color preview swatch, please help!

Hi Robert

 

Thank you for the clarification. Smiley Happy

Ask a SmartBear
Top Kudoed Authors