Skip to content

Commit 57be117

Browse files
authored
Merge pull request #1969 from b-vamsipunnam/add-get-css-property-value-keyword
Add "Get CSS Property Value" keyword to retrieve computed CSS values
2 parents 2141caf + 8290239 commit 57be117

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

atest/acceptance/keywords/elements.robot

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,37 @@ Cover Element can cover just one element
218218

219219
Cover Elements should throw exception when locator is invalid
220220
Run Keyword And Expect Error No element with locator '//img?@src="inexistent"?' found.
221-
... Cover Element //img[@src="inexistent"]
221+
... Cover Element //img[@src="inexistent"]
222+
223+
Get CSS Property Value Returns Correct Values For Common Properties
224+
[Setup] Go To Page "cssproperties.html"
225+
${display}= Get CSS Property Value id:styled-div display
226+
Should Be Equal ${display} block
227+
${font_size}= Get CSS Property Value id:styled-div font-size
228+
Should Be Equal ${font_size} 16px
229+
${margin_top}= Get CSS Property Value id:styled-div margin-top
230+
Should Be Equal ${margin_top} 10px
231+
${text_align}= Get CSS Property Value id:styled-div text-align
232+
Should Be Equal ${text_align} center
233+
234+
Get CSS Property Value With Missing Element
235+
[Setup] Go To Page "cssproperties.html"
236+
Run Keyword And Expect Error
237+
... Element with locator 'id:non-existent' not found.
238+
... Get CSS Property Value id:non-existent color
239+
240+
Get CSS Property Value Returns Background Color
241+
[Setup] Go To Page "cssproperties.html"
242+
${color}= Get CSS Property Value id:styled-div background-color
243+
Should Match Regexp ${color} ^rgba?\(.+\)$
244+
245+
Get CSS Property Value Using WebElement
246+
[Setup] Go To Page "cssproperties.html"
247+
${element}= Get WebElement id:styled-div
248+
${display}= Get CSS Property Value ${element} display
249+
Should Be Equal ${display} block
250+
251+
Get CSS Property Value With Non Existing Property
252+
[Setup] Go To Page "cssproperties.html"
253+
${value}= Get CSS Property Value id:styled-div non-existent-property
254+
Should Be Empty ${value}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CSS Properties Test Page</title>
5+
<style>
6+
#styled-div {
7+
display: block;
8+
font-size: 16px;
9+
background-color: rgb(255, 0, 0);
10+
margin-top: 10px;
11+
text-align: center;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
17+
<div id="styled-div">
18+
Demo Element
19+
</div>
20+
21+
</body>
22+
</html>

src/SeleniumLibrary/keywords/element.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,3 +1276,21 @@ def _convert_special_keys(self, keys):
12761276

12771277
def _selenium_keys_has_attr(self, key):
12781278
return hasattr(Keys, key)
1279+
1280+
@keyword("Get CSS Property Value")
1281+
def get_css_property_value(
1282+
self, locator: Locator, css_property: str
1283+
) -> str:
1284+
"""Returns the computed value of ``css_property`` from the element ``locator``.
1285+
1286+
See the `Locating elements` section for details about the locator syntax.
1287+
1288+
The value returned is the browser-computed CSS value of the property.
1289+
For example, colors are often returned in ``rgba(...)`` format and sizes
1290+
are typically returned in pixels.
1291+
1292+
Example:
1293+
| ${color}= | `Get CSS Property Value` | css:button.submit | background-color |
1294+
| ${size}= | `Get CSS Property Value` | id:username | font-size |
1295+
"""
1296+
return self.find_element(locator).value_of_css_property(css_property)

utest/test/api/test_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setUpClass(cls):
2222
def test_no_libraries(self):
2323
for item in [None, "None", ""]:
2424
sl = SeleniumLibrary(plugins=item)
25-
self.assertEqual(len(sl.get_keyword_names()), 182)
25+
self.assertEqual(len(sl.get_keyword_names()), 183)
2626

2727
def test_parse_library(self):
2828
plugin = "path.to.MyLibrary"

0 commit comments

Comments
 (0)