Skip to content

Commit 4a8d2b8

Browse files
committed
Add 'Get CSS Property Value' keyword with tests
1 parent c5c21ea commit 4a8d2b8

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

atest/acceptance/keywords/elements.robot

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,32 @@ 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
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
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
@@ -1275,3 +1275,21 @@ def _convert_special_keys(self, keys):
12751275

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