Skip to content

Commit

Permalink
Merge pull request #402 from dduong42/master
Browse files Browse the repository at this point in the history
Add finding by text.
  • Loading branch information
andrewsmedina committed May 17, 2015
2 parents e114d7b + c64e2df commit 908cd31
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/finding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Finding elements
++++++++++++++++

Splinter provides 6 methods for finding elements in the page, one for each
selector type: ``css``, ``xpath``, ``tag``, ``name``, ``id``, ``value``.
selector type: ``css``, ``xpath``, ``tag``, ``name``, ``id``, ``value``,
``text``.
Examples:

.. highlight:: python
Expand All @@ -22,6 +23,7 @@ Examples:
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_text('Hello World!')
browser.find_by_id('firstheader')
browser.find_by_value('query')

Expand Down Expand Up @@ -82,7 +84,7 @@ If you need to find the links in a page, you can use the methods

As the other ``find_*`` methods, these returns a list of all found elements.

You also can search for links using other selector types with the methods
You also can search for links using other selector types with the methods
``find_by_css``, ``find_by_xpath``, ``find_by_tag``, ``find_by_name``,
``find_by_value`` and ``find_by_id``.

Expand Down
4 changes: 3 additions & 1 deletion docs/matchers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Checking the presence of elements

Splinter provides 6 methods to check the presence of elements in the page, one
for each selector type: ``css``, ``xpath``, ``tag``, ``name``, ``id``,
``value``. Examples:
``value``, ``text``. Examples:

.. highlight:: python

Expand All @@ -68,6 +68,7 @@ for each selector type: ``css``, ``xpath``, ``tag``, ``name``, ``id``,
browser.is_element_present_by_xpath('//h1')
browser.is_element_present_by_tag('h1')
browser.is_element_present_by_name('name')
browser.is_element_present_by_text('Hello World!')
browser.is_element_present_by_id('firstheader')
browser.is_element_present_by_value('query')
browser.is_element_present_by_value('query', wait_time=10) # using wait_time
Expand All @@ -85,5 +86,6 @@ There's also the negative forms of these methods, as in ``is_text_present``:
browser.is_element_not_present_by_xpath('//h6')
browser.is_element_not_present_by_tag('h6')
browser.is_element_not_present_by_name('unexisting-name')
browser.is_element_not_present_by_text('Not here :(')
browser.is_element_not_present_by_id('unexisting-header')
browser.is_element_not_present_by_id('unexisting-header', wait_time=10) # using wait_time
28 changes: 28 additions & 0 deletions splinter/driver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def find_by_value(self, value):
"""
raise NotImplementedError("%s doesn't support finding elements by value." % self.driver_name)

def find_by_text(self, text):
"""
Finds elements in current page by their text.
Returns an instance of :class:`ElementList <splinter.element_list.ElementList>`
"""
raise NotImplementedError("%s doesn't support finding elements by text." % self.driver_name)

def find_by_tag(self, tag):
"""
Find all elements of a given tag in current page.
Expand Down Expand Up @@ -431,6 +439,26 @@ def is_element_not_present_by_value(self, value, wait_time=None):
raise NotImplementedError(
"%s doesn't support verifying if element is not present by value" % self.driver_name)

def is_element_present_by_text(self, text, wait_time=None):
"""
Verify if the element is present in the current page by text,
and wait the specified time in ``wait_time``.
Returns True if the element is present and False if is not present.
"""
raise NotImplementedError(
"%s doesn't support verifying if element is present by text" % self.driver_name)

def is_element_not_present_by_text(self, text, wait_time=None):
"""
Verify if the element is not present in the current page by text,
and wait the specified time in ``wait_time``.
Returns True if the element is not present and False if is present.
"""
raise NotImplementedError(
"%s doesn't support verifying if element is not present by text" % self.driver_name)

def is_element_present_by_id(self, id, wait_time=None):
"""
Verify if the element is present in the current page by id,
Expand Down
6 changes: 6 additions & 0 deletions splinter/driver/djangoclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def find_by_tag(self, tag):
def find_by_value(self, value):
return self.find_by_xpath('//*[@value="%s"]' % value, original_find="value", original_selector=value)

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text, original_find="text", original_selector=text)

def find_by_id(self, id_value):
return self.find_by_xpath(
'//*[@id="%s"][1]' % id_value, original_find="id", original_selector=id_value)
Expand Down Expand Up @@ -350,6 +353,9 @@ def find_by_value(self, value):
elements = self._element.cssselect('[value="%s"]' % value)
return ElementList([self.__class__(element, self) for element in elements])

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text)

def find_by_id(self, id):
elements = self._element.cssselect('#%s' % id)
return ElementList([self.__class__(element, self) for element in elements])
Expand Down
7 changes: 7 additions & 0 deletions splinter/driver/flaskclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def find_by_tag(self, tag):
def find_by_value(self, value):
return self.find_by_xpath('//*[@value="%s"]' % value, original_find="value", original_selector=value)

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text,
original_find="text", original_selector=text)

def find_by_id(self, id_value):
return self.find_by_xpath(
'//*[@id="%s"][1]' % id_value, original_find="id", original_selector=id_value)
Expand Down Expand Up @@ -338,6 +342,9 @@ def find_by_value(self, value):
elements = self._element.cssselect('[value="%s"]' % value)
return ElementList([self.__class__(element, self) for element in elements])

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text)

def find_by_id(self, id):
elements = self._element.cssselect('#%s' % id)
return ElementList([self.__class__(element, self) for element in elements])
Expand Down
15 changes: 15 additions & 0 deletions splinter/driver/webdriver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ def is_element_present_by_value(self, value, wait_time=None):
def is_element_not_present_by_value(self, value, wait_time=None):
return self.is_element_not_present(self.find_by_value, value, wait_time)

def is_element_present_by_text(self, text, wait_time=None):
return self.is_element_present(self.find_by_text, text, wait_time)

def is_element_not_present_by_text(self, text, wait_time=None):
return self.is_element_not_present(self.find_by_text, text, wait_time)

def is_element_present_by_id(self, id, wait_time=None):
return self.is_element_present(self.find_by_id, id, wait_time)

Expand Down Expand Up @@ -394,6 +400,10 @@ def find_by_tag(self, tag):
def find_by_value(self, value):
return self.find_by_xpath('//*[@value="%s"]' % value, original_find='value', original_query=value)

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text,
original_find='text', original_query=text)

def find_by_id(self, id):
return self.find_by(self.driver.find_element_by_id, id)

Expand Down Expand Up @@ -579,6 +589,11 @@ def find_by_value(self, value):
selector = '[value="%s"]' % value
return self.find_by_css(selector, original_find='value', original_query=value)

def find_by_text(self, text):
selector = '//*[text()="%s"]' % text
return self.find_by_xpath(selector, original_find='text',
original_query=text)

def find_by_id(self, id):
elements = ElementList(self._element.find_elements_by_id(id))
return ElementList(
Expand Down
7 changes: 7 additions & 0 deletions splinter/driver/zopetestbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ def find_by_tag(self, tag):
def find_by_value(self, value):
return self.find_by_xpath('//*[@value="%s"]' % value, original_find="value", original_selector=value)

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text,
original_find="text", original_selector=text)

def find_by_id(self, id_value):
return self.find_by_xpath(
'//*[@id="%s"][1]' % id_value, original_find="id", original_selector=id_value)
Expand Down Expand Up @@ -311,6 +315,9 @@ def find_by_value(self, value):
elements = self._element.cssselect('[value="%s"]' % value)
return ElementList([self.__class__(element, self) for element in elements])

def find_by_text(self, text):
return self.find_by_xpath('//*[text()="%s"]' % text)

def find_by_id(self, id):
elements = self._element.cssselect('#%s' % id)
return ElementList([self.__class__(element, self) for element in elements])
Expand Down
5 changes: 5 additions & 0 deletions tests/find_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def test_finding_by_value(self):
id = self.browser.find_by_id('gender-m')
self.assertEqual(id.value, value)

def test_finding_by_text(self):
"should find elements by text"
element = self.browser.find_by_text('Complex')
self.assertEqual(element.value, 'Complex')

def test_finding_by_id(self):
"should find elements by id"
value = self.browser.find_by_id("firstheader").value
Expand Down
16 changes: 16 additions & 0 deletions tests/is_element_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ def test_is_element_not_present_by_tag_returns_false_if_element_is_present(self)
self.browser.find_by_css('.add-async-element').click()
self.assertFalse(self.browser.is_element_not_present_by_tag('h4'))

def test_is_element_present_by_text(self):
"should is element present by text verify if element is present"
self.assertTrue(self.browser.is_element_present_by_text('Complex'))

def test_is_element_present_by_text_returns_false_if_element_is_not_present(self):
"should is element present by text verify if element is present"
self.assertFalse(self.browser.is_element_present_by_text('Not present'))

def test_is_element_not_present_by_text(self):
"should is element not present by text verify if element is not present"
self.assertTrue(self.browser.is_element_not_present_by_text('Not present'))

def test_is_element_not_present_by_text_returns_false_if_element_is_present(self):
"should is element not present by text returns False if element is present"
self.assertFalse(self.browser.is_element_not_present_by_text('Complex'))

def test_is_element_present_by_value(self):
"should is element present by value verify if element is present"
self.browser.find_by_css('.add-async-element').click()
Expand Down

0 comments on commit 908cd31

Please sign in to comment.