From edf50adba9f147ad369918b53e7a5e01c885249d Mon Sep 17 00:00:00 2001 From: zephraph Date: Mon, 27 Jul 2015 23:14:53 -0500 Subject: [PATCH 1/4] Added keyword 'Get WebElement' --- src/Selenium2Library/keywords/_element.py | 11 +++++++++-- test/acceptance/keywords/elements.robot | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Selenium2Library/keywords/_element.py b/src/Selenium2Library/keywords/_element.py index 5a2cf7470..95159309b 100644 --- a/src/Selenium2Library/keywords/_element.py +++ b/src/Selenium2Library/keywords/_element.py @@ -21,6 +21,13 @@ def __init__(self): # Public, get element(s) + def get_webelement(self, locator): + """Returns the first WebElement matching the given locator. + + See `introduction` for details about locating elements. + """ + return self.get_webelements(locator)[0] + def get_webelements(self, locator): """Returns list of WebElement objects matching locator. @@ -599,7 +606,7 @@ def get_matching_xpath_count(self, xpath): """Returns number of elements matching `xpath` One should not use the xpath= prefix for 'xpath'. XPath is assumed. - + Correct: | count = | Get Matching Xpath Count | //div[@id='sales-pop'] Incorrect: @@ -615,7 +622,7 @@ def xpath_should_match_x_times(self, xpath, expected_xpath_count, message='', lo """Verifies that the page contains the given number of elements located by the given `xpath`. One should not use the xpath= prefix for 'xpath'. XPath is assumed. - + Correct: | Xpath Should Match X Times | //div[@id='sales-pop'] | 1 Incorrect: diff --git a/test/acceptance/keywords/elements.robot b/test/acceptance/keywords/elements.robot index 4e7c731f1..bb4adfcef 100644 --- a/test/acceptance/keywords/elements.robot +++ b/test/acceptance/keywords/elements.robot @@ -7,6 +7,13 @@ Get Elements @{links}= Get WebElements //div[@id="div_id"]/a Length Should Be ${links} 11 +Get First Matching Element + @{links}= Get WebElements //div[@id="div_id"]/a + ${link}= Get WebElement //div[@id="div_id"]/a + LOG @{links}[0] + LOG ${link} + Should Be Equal @{links}[0] ${link} + More Get Elements [Setup] Go To Page "forms/prefilled_email_form.html" @{checkboxes}= Get WebElements //input[@type="checkbox"] @@ -51,4 +58,3 @@ Get Vertical Position ${pos}= Get Vertical Position link=Link Should Be True ${pos} > ${0} Run Keyword And Expect Error Could not determine position for 'non-existent' Get Horizontal Position non-existent - From c4e8fe63ee398dc4d44ba6b484c9faadb9f481b4 Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Sun, 2 Aug 2015 09:59:29 -0400 Subject: [PATCH 2/4] Refactor method for getting first web element. Added extra check for non-existing web element. --- src/Selenium2Library/keywords/_element.py | 2 +- test/acceptance/keywords/elements.robot | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Selenium2Library/keywords/_element.py b/src/Selenium2Library/keywords/_element.py index 95159309b..655782a55 100644 --- a/src/Selenium2Library/keywords/_element.py +++ b/src/Selenium2Library/keywords/_element.py @@ -26,7 +26,7 @@ def get_webelement(self, locator): See `introduction` for details about locating elements. """ - return self.get_webelements(locator)[0] + return self._element_find(locator, True, True) def get_webelements(self, locator): """Returns list of WebElement objects matching locator. diff --git a/test/acceptance/keywords/elements.robot b/test/acceptance/keywords/elements.robot index bb4adfcef..bcbdc7482 100644 --- a/test/acceptance/keywords/elements.robot +++ b/test/acceptance/keywords/elements.robot @@ -7,12 +7,14 @@ Get Elements @{links}= Get WebElements //div[@id="div_id"]/a Length Should Be ${links} 11 -Get First Matching Element +Get Web Element @{links}= Get WebElements //div[@id="div_id"]/a ${link}= Get WebElement //div[@id="div_id"]/a LOG @{links}[0] LOG ${link} Should Be Equal @{links}[0] ${link} + Run Keyword and Expect Error ValueError: Element locator 'id=non_existing_elem' did not match any elements. + ... Get WebElement id=non_existing_elem More Get Elements [Setup] Go To Page "forms/prefilled_email_form.html" From 860b6e603fc314a290a644f2e3552c546cce8a71 Mon Sep 17 00:00:00 2001 From: Ed Manlove Date: Mon, 3 Aug 2015 19:37:32 -0400 Subject: [PATCH 3/4] Removed logging. --- test/acceptance/keywords/elements.robot | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/acceptance/keywords/elements.robot b/test/acceptance/keywords/elements.robot index bcbdc7482..21706ce0a 100644 --- a/test/acceptance/keywords/elements.robot +++ b/test/acceptance/keywords/elements.robot @@ -10,8 +10,6 @@ Get Elements Get Web Element @{links}= Get WebElements //div[@id="div_id"]/a ${link}= Get WebElement //div[@id="div_id"]/a - LOG @{links}[0] - LOG ${link} Should Be Equal @{links}[0] ${link} Run Keyword and Expect Error ValueError: Element locator 'id=non_existing_elem' did not match any elements. ... Get WebElement id=non_existing_elem From cde2ca843ec0906d74db85f4f03b8b96e31fcd01 Mon Sep 17 00:00:00 2001 From: zephraph Date: Mon, 3 Aug 2015 18:41:51 -0500 Subject: [PATCH 4/4] Update changelog --- CHANGES.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index e5fa6931d..3df9ee3dc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,10 @@ Release Notes ============= +1.7.3 (unreleased) +------------------- +- Added 'Get WebElement' [zephraph][emanlove] + 1.7.2 ---------------- - Added an argument called screenshot_root_directory that can be passed into S2L's