getstyle(back-color) returns differ values across browsers

Class library usage, coding and language questions.
pleugene
Posts: 3
Joined: Thu May 07, 2015 7:46 pm

getstyle(back-color) returns differ values across browsers

Post by pleugene » Tue Jun 30, 2015 10:56 pm

Hello

I'm trying to get background color of some TdTag to verify against our database.

Code: Select all

tdColor.GetStyle("background-color").ToString()
The above code returns me correct hex value for the color in IE while it returns me RGB values of rgb(255, 255, 255) in chrome and firefox.

However, when I track the td item in developer's tool with chrome/firefox, the style in html is showing up in hex value which is what i need.

Code: Select all

style=background-color:#6495ED;
I wonder if there's any reason why it returns me rgb value for chrome/firefox. So I don't need to convert them manually according to browsers after getting the value.


Thanks,
Eugene

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: getstyle(back-color) returns differ values across browsers

Post by odklizec » Wed Jul 01, 2015 6:12 am

Hi,

I think this is caused by how the individual browsers interpret color values (no matter how is the color value written in css/html). While IE returns the same value as saved in css, Firefox apparently converts the color values to rgb. So what you need to do is to check the format of returned color and convert it to expected one.

PS: you can use something like this to convert obtained RGB value to HEX:
public void GetBGColor(Ranorex.Adapter tdTagToInspect)
		{
			TdTag tdTagElmt = tdTagToInspect.Element;
			var bgColor = tdTagElmt.GetStyle("background-color");
			Regex regExp = new Regex(@"\((\d{1,3}), (\d{1,3}), (\d{1,3})\)");
			MatchCollection matchCol = regExp.Matches(bgColor);
			if (matchCol.Count >=1)
			{
				Match[] rgbArray = new Match[matchCol.Count];
				matchCol.CopyTo(rgbArray, 0);
				int rCol = int.Parse(rgbArray[0].Groups[1].Value);
				int gCol = int.Parse(rgbArray[0].Groups[2].Value);
				int bCol = int.Parse(rgbArray[0].Groups[3].Value);
				string hexColor = "#"+HexFromRGB(rCol, gCol, bCol);
			}
		}

		private string HexFromRGB(int r, int g, int b)
		{
			return ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", r, g, b)).Name.Remove(0,2);
		}
Simply add the GetBGColor method as UserCode action and assign expected TDTag from repository as parameter. hexColor should then return color in hex format. You may just need to add some code to check if bgColor is in RGB or HEX format (or simply use the above method only with FF or Chrome).
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration