Page 1 of 1

getstyle(back-color) returns differ values across browsers

Posted: Tue Jun 30, 2015 10:56 pm
by pleugene
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

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

Posted: Wed Jul 01, 2015 6:12 am
by odklizec
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?