From 490d7f142559b9d505edfb51793e88d1fa29385c Mon Sep 17 00:00:00 2001 From: hellmanj Date: Thu, 22 Jan 2015 13:35:01 -0500 Subject: [PATCH 1/4] allow Se2Lib actions to work by passing a WebElement --- README.md | 4 +++- robotpageobjects/base.py | 12 ++++++++++-- tests/scenarios/po/widget_template.py | 3 ++- tests/scenarios/test_find_elements_with_selector.py | 7 ++++++- tests/scenarios/test_selector_self_ref.py | 3 ++- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d116c72..a4322c0 100755 --- a/README.md +++ b/README.md @@ -407,6 +407,7 @@ This means you can pass selectors instead of locators to all Se2Lib methods that - for maintainability and readability, you should pass selectors to Se2Lib methods, not locators. - if you write your own helper methods for finding or interacting with elements allow them to be passed locators *and* selectors. +- You can also pass an instance of a selenium WebElement to Se2Lib methods instead of a selector or locator #### Looking up elements from the end of a list @@ -494,7 +495,8 @@ we don't actually need a reference to the WebElement because all page objects gi so from within your page object you can call them on `self`. If, for some reason, you need a direct reference to a WebElement you can get it by passing a locator/selector to -`find_element` or `find_element`, which is also on every page object. When at all possible, however, +`find_element` or `find_element`, which is also on every page object. You can then use this reference when +invoking Se2Lib keywords instead of a locator/selector. When at all possible, however, work at the Selenium2Library level, not at the WebElement level. For example: ... diff --git a/robotpageobjects/base.py b/robotpageobjects/base.py index 509d2d5..4ec82fa 100755 --- a/robotpageobjects/base.py +++ b/robotpageobjects/base.py @@ -9,6 +9,7 @@ from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import WebDriverException +from selenium.webdriver.remote.webelement import WebElement from Selenium2Library import Selenium2Library from Selenium2Library.keywords.keywordgroup import KeywordGroupMetaClass from . import abstractedlogger @@ -920,11 +921,18 @@ def _element_find(self, locator, *args, **kwargs): Try to use _element_find with the locator as is, then if a selector exists, try that. - :param locator: The Selenium2Library-style locator, or IFT selector. - :type locator: str + + ``locator`` can also be a WebElement if an element has been identified already + and it is desired to perform actions on that element + + :param locator: The Selenium2Library-style locator, or IFT selector + or WebElement (if the element has already been identified). + :type locator: str or WebElement :returns: WebElement or list """ + if isinstance(locator, WebElement): + return locator our_wait = self.selenium_implicit_wait if kwargs.get("wait") is None else kwargs["wait"] diff --git a/tests/scenarios/po/widget_template.py b/tests/scenarios/po/widget_template.py index 973d30a..665c30f 100755 --- a/tests/scenarios/po/widget_template.py +++ b/tests/scenarios/po/widget_template.py @@ -6,5 +6,6 @@ class WidgetItemPage(Page): name = "Widget Item Page" uri_template = "/site/category/{category}/{id}.html" selectors = {"title": "css=h1", - "hidden-element": "css=#hidden"} + "hidden-element": "css=#hidden", + "see-also": "id=see-also"} diff --git a/tests/scenarios/test_find_elements_with_selector.py b/tests/scenarios/test_find_elements_with_selector.py index f6658a0..92ec89d 100644 --- a/tests/scenarios/test_find_elements_with_selector.py +++ b/tests/scenarios/test_find_elements_with_selector.py @@ -1,4 +1,4 @@ -from po import selectors_page +from po import selectors_page, widget_template import unittest from nose.tools import raises from robotpageobjects.exceptions import SelectorError @@ -25,6 +25,11 @@ def test_find_element(self): def test_find_element_multiple(self): self.page.find_element("inputs") + def test_find_element_webelement(self): + element_found_by_selector = self.page.find_element("search-button") + self.page.click_element(element_found_by_selector) + self.page.location_should_be("/site/result.html?q=search%20term") + def tearDown(self): self.page.close() diff --git a/tests/scenarios/test_selector_self_ref.py b/tests/scenarios/test_selector_self_ref.py index f468427..8f82c87 100644 --- a/tests/scenarios/test_selector_self_ref.py +++ b/tests/scenarios/test_selector_self_ref.py @@ -10,7 +10,8 @@ def setUp(self): def test_selector_self_ref(self): print self.p.selectors['form label'] - self.p.element_should_be_visible("form label") + webel = self.p.find_element("form label") + self.p.element_should_be_visible(webel) def tearDown(self): self.p.close() From e91dd7abeab3a89bcfdf23dcc24d2924e66e7edf Mon Sep 17 00:00:00 2001 From: hellmanj Date: Thu, 22 Jan 2015 13:36:32 -0500 Subject: [PATCH 2/4] change not necessary --- tests/scenarios/test_selector_self_ref.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/scenarios/test_selector_self_ref.py b/tests/scenarios/test_selector_self_ref.py index 8f82c87..f468427 100644 --- a/tests/scenarios/test_selector_self_ref.py +++ b/tests/scenarios/test_selector_self_ref.py @@ -10,8 +10,7 @@ def setUp(self): def test_selector_self_ref(self): print self.p.selectors['form label'] - webel = self.p.find_element("form label") - self.p.element_should_be_visible(webel) + self.p.element_should_be_visible("form label") def tearDown(self): self.p.close() From 558815aa336b6b43e18e565f2c003e5e3aea54e6 Mon Sep 17 00:00:00 2001 From: hellmanj Date: Thu, 22 Jan 2015 14:42:34 -0500 Subject: [PATCH 3/4] take out unused PO --- tests/scenarios/test_find_elements_with_selector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scenarios/test_find_elements_with_selector.py b/tests/scenarios/test_find_elements_with_selector.py index 92ec89d..3ca8d0e 100644 --- a/tests/scenarios/test_find_elements_with_selector.py +++ b/tests/scenarios/test_find_elements_with_selector.py @@ -1,4 +1,4 @@ -from po import selectors_page, widget_template +from po import selectors_page import unittest from nose.tools import raises from robotpageobjects.exceptions import SelectorError From c741537ce710326baba0b724cf1cd2bcfe8dfdaa Mon Sep 17 00:00:00 2001 From: hellmanj Date: Thu, 22 Jan 2015 14:43:37 -0500 Subject: [PATCH 4/4] take out unused PO selector --- tests/scenarios/po/widget_template.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/scenarios/po/widget_template.py b/tests/scenarios/po/widget_template.py index 665c30f..973d30a 100755 --- a/tests/scenarios/po/widget_template.py +++ b/tests/scenarios/po/widget_template.py @@ -6,6 +6,5 @@ class WidgetItemPage(Page): name = "Widget Item Page" uri_template = "/site/category/{category}/{id}.html" selectors = {"title": "css=h1", - "hidden-element": "css=#hidden", - "see-also": "id=see-also"} + "hidden-element": "css=#hidden"}