sbkeenan
12 years agoFrequent Contributor
Obtaining an element's display properties in a cross-browser test
Hi
I've just started looking into creating tests for cross-browser testing one of our applications and was working on how to obtain the final display settings of a given element. Afetr a day or so of reading through the documentation and various orther web forums, I have come up with this VBScript sample that hopefully fulfills my future needs. I thought, at the very least, it might be useful for someone else, but if anyone has a better approach, I would be interested in hearing about it.
The approach I've taken is to test specifically for IE version 8 or less and if so, then use the CurrentStyle method, otherwise use the getComputedStyle method since all other browsers and IE9/10 support its use.
In taking this approach, I need to modify the property value passed to the function to remove the hyphen from it so that it works with IE8 or lower, for the other browsers, it needs no modification.
Attached are 2 screen shots showing results when run from IE 9 and IE 8. I've also tested this with Chrome and Firefox, both of which return the same results as IE9
Regards Stephen
I've just started looking into creating tests for cross-browser testing one of our applications and was working on how to obtain the final display settings of a given element. Afetr a day or so of reading through the documentation and various orther web forums, I have come up with this VBScript sample that hopefully fulfills my future needs. I thought, at the very least, it might be useful for someone else, but if anyone has a better approach, I would be interested in hearing about it.
option explicit
Sub main
dim ib
dim element
dim result
set ib = aliases.browser
log.message(ib.ProcessName & " version " & aliases.browser.FileVersionInfo)
set element = ib.Page("http://smartbear.com/").Form("aspnetForm").Panel("container").Panel("main").Panel("home").Panel("tools").Panel(0).Panel(1).Link(0)
result = getCSSproperty(element, "color")
log.message("color: " & result)
result = getCSSproperty(element, "background-Color")
log.message("backgroundColor: " & result)
result = getCSSproperty(element, "text-Align")
log.message("textAlign: " & result)
result = getCSSproperty(element, "font-Size")
log.message("fontSize: " & result)
result = getCSSproperty(element, "font-Family")
log.message("fontFamily: " & result)
end sub
function getCSSproperty(byVal element, byVal prop)
dim ib
set ib = aliases.browser
if ib.ProcessName = "iexplore" and CInt(left(ib.FileVersionInfo, (instr(ib.FileVersionInfo, ".")))) < 9 then
'Browser is Internet Explorer V8 or less, therefore use 'CurrentStyle'
prop = replace(prop, "-", "")
getCSSproperty = eval("element.currentStyle." & prop)
else 'IE9 or greater, or other browser that supports 'GetPropertyValue'
dim style
Set style = aliases.browser.Page("*").contentDocument.defaultView.getComputedStyle(element, "")
getCSSproperty = style.getPropertyValue(prop)
end if
end function
The approach I've taken is to test specifically for IE version 8 or less and if so, then use the CurrentStyle method, otherwise use the getComputedStyle method since all other browsers and IE9/10 support its use.
In taking this approach, I need to modify the property value passed to the function to remove the hyphen from it so that it works with IE8 or lower, for the other browsers, it needs no modification.
Attached are 2 screen shots showing results when run from IE 9 and IE 8. I've also tested this with Chrome and Firefox, both of which return the same results as IE9
Regards Stephen