@@ -62,6 +62,7 @@ def __add_sync_methods(self, element):
6262 lambda destination : self .__mouse_drag (element , destination )
6363 )
6464 element .mouse_move = lambda : self .__mouse_move (element )
65+ element .press_keys = lambda text : self .__press_keys (element , text )
6566 element .query_selector = (
6667 lambda selector : self .__query_selector (element , selector )
6768 )
@@ -211,7 +212,8 @@ def find_element_by_text(self, text, tag_name=None, timeout=None):
211212 element = self .__add_sync_methods (element .parent )
212213 return self .__add_sync_methods (element )
213214 elif (
214- element .parent .parent
215+ element .parent
216+ and element .parent .parent
215217 and tag_name in element .parent .parent .tag_name .lower ()
216218 and text .strip () in element .parent .parent .text
217219 ):
@@ -272,7 +274,8 @@ def find_elements_by_text(self, text, tag_name=None):
272274 if element not in updated_elements :
273275 updated_elements .append (element )
274276 elif (
275- element .parent .parent
277+ element .parent
278+ and element .parent .parent
276279 and tag_name in element .parent .parent .tag_name .lower ()
277280 and text .strip () in element .parent .parent .text
278281 ):
@@ -445,6 +448,23 @@ def __mouse_move(self, element):
445448 self .loop .run_until_complete (element .mouse_move_async ())
446449 )
447450
451+ def __press_keys (self , element , text ):
452+ element .scroll_into_view ()
453+ submit = False
454+ if text .endswith ("\n " ) or text .endswith ("\r " ):
455+ submit = True
456+ text = text [:- 1 ]
457+ for key in text :
458+ element .send_keys (key )
459+ time .sleep (0.044 )
460+ if submit :
461+ element .send_keys ("\r \n " )
462+ time .sleep (0.044 )
463+ self .__slow_mode_pause_if_set ()
464+ return (
465+ self .loop .run_until_complete (self .page .wait ())
466+ )
467+
448468 def __query_selector (self , element , selector ):
449469 selector = self .__convert_to_css_if_xpath (selector )
450470 element2 = self .loop .run_until_complete (
@@ -1681,21 +1701,79 @@ def is_element_visible(self, selector):
16811701 return True
16821702 return False
16831703
1684- def wait_for_element_visible (self , selector , timeout = None ):
1704+ def is_text_visible (self , text , selector = "body" ):
1705+ selector = self .__convert_to_css_if_xpath (selector )
1706+ text = text .strip ()
1707+ element = None
1708+ try :
1709+ element = self .find_element (selector , timeout = 0.1 )
1710+ except Exception :
1711+ return False
1712+ with suppress (Exception ):
1713+ if text in element .text_all :
1714+ return True
1715+ return False
1716+
1717+ def is_exact_text_visible (self , text , selector = "body" ):
1718+ selector = self .__convert_to_css_if_xpath (selector )
1719+ text = text .strip ()
1720+ element = None
1721+ try :
1722+ element = self .find_element (selector , timeout = 0.1 )
1723+ except Exception :
1724+ return False
1725+ with suppress (Exception ):
1726+ if text == element .text_all .strip ():
1727+ return True
1728+ return False
1729+
1730+ def wait_for_text (self , text , selector = "body" , timeout = None ):
16851731 if not timeout :
16861732 timeout = settings .SMALL_TIMEOUT
1733+ start_ms = time .time () * 1000.0
1734+ stop_ms = start_ms + (timeout * 1000.0 )
1735+ text = text .strip ()
1736+ element = None
16871737 try :
1688- self .select (selector , timeout = timeout )
1738+ element = self .find_element (selector , timeout = timeout )
16891739 except Exception :
1690- raise Exception ("Element {%s} was not found!" % selector )
1691- for i in range (30 ):
1692- if self .is_element_visible (selector ):
1693- return self .select (selector )
1740+ raise Exception ("Element {%s} not found!" % selector )
1741+ for i in range (int (timeout * 10 )):
1742+ with suppress (Exception ):
1743+ element = self .find_element (selector , timeout = 0.1 )
1744+ if text in element .text_all :
1745+ return True
1746+ now_ms = time .time () * 1000.0
1747+ if now_ms >= stop_ms :
1748+ break
16941749 time .sleep (0.1 )
1695- raise Exception ("Element {%s} was not visible!" % selector )
1750+ raise Exception (
1751+ "Text {%s} not found in {%s}! Actual text: {%s}"
1752+ % (text , selector , element .text_all )
1753+ )
16961754
1697- def assert_element (self , selector , timeout = None ):
1698- """Same as assert_element_visible()"""
1755+ def wait_for_text_not_visible (self , text , selector = "body" , timeout = None ):
1756+ if not timeout :
1757+ timeout = settings .SMALL_TIMEOUT
1758+ text = text .strip ()
1759+ start_ms = time .time () * 1000.0
1760+ stop_ms = start_ms + (timeout * 1000.0 )
1761+ for i in range (int (timeout * 10 )):
1762+ if not self .is_text_visible (text , selector ):
1763+ return True
1764+ now_ms = time .time () * 1000.0
1765+ if now_ms >= stop_ms :
1766+ break
1767+ time .sleep (0.1 )
1768+ plural = "s"
1769+ if timeout == 1 :
1770+ plural = ""
1771+ raise Exception (
1772+ "Text {%s} in {%s} was still visible after %s second%s!"
1773+ % (text , selector , timeout , plural )
1774+ )
1775+
1776+ def wait_for_element_visible (self , selector , timeout = None ):
16991777 if not timeout :
17001778 timeout = settings .SMALL_TIMEOUT
17011779 try :
@@ -1704,10 +1782,15 @@ def assert_element(self, selector, timeout=None):
17041782 raise Exception ("Element {%s} was not found!" % selector )
17051783 for i in range (30 ):
17061784 if self .is_element_visible (selector ):
1707- return True
1785+ return self . select ( selector )
17081786 time .sleep (0.1 )
17091787 raise Exception ("Element {%s} was not visible!" % selector )
17101788
1789+ def assert_element (self , selector , timeout = None ):
1790+ """Same as assert_element_visible()"""
1791+ self .assert_element_visible (selector , timeout = timeout )
1792+ return True
1793+
17111794 def assert_element_visible (self , selector , timeout = None ):
17121795 """Same as assert_element()"""
17131796 if not timeout :
@@ -1852,29 +1935,9 @@ def assert_url_contains(self, substring):
18521935 raise Exception (error % (expected , actual ))
18531936
18541937 def assert_text (self , text , selector = "body" , timeout = None ):
1855- if not timeout :
1856- timeout = settings .SMALL_TIMEOUT
1857- start_ms = time .time () * 1000.0
1858- stop_ms = start_ms + (timeout * 1000.0 )
1859- text = text .strip ()
1860- element = None
1861- try :
1862- element = self .find_element (selector , timeout = timeout )
1863- except Exception :
1864- raise Exception ("Element {%s} not found!" % selector )
1865- for i in range (int (timeout * 10 )):
1866- with suppress (Exception ):
1867- element = self .find_element (selector , timeout = 0.1 )
1868- if text in element .text_all :
1869- return True
1870- now_ms = time .time () * 1000.0
1871- if now_ms >= stop_ms :
1872- break
1873- time .sleep (0.1 )
1874- raise Exception (
1875- "Text {%s} not found in {%s}! Actual text: {%s}"
1876- % (text , selector , element .text_all )
1877- )
1938+ """Same as wait_for_text()"""
1939+ self .wait_for_text (text , selector = selector , timeout = timeout )
1940+ return True
18781941
18791942 def assert_exact_text (self , text , selector = "body" , timeout = None ):
18801943 if not timeout :
@@ -1904,6 +1967,13 @@ def assert_exact_text(self, text, selector="body", timeout=None):
19041967 % (text , element .text_all , selector )
19051968 )
19061969
1970+ def assert_text_not_visible (self , text , selector = "body" , timeout = None ):
1971+ """Raises an exception if the text is still visible after timeout."""
1972+ self .wait_for_text_not_visible (
1973+ text , selector = selector , timeout = timeout
1974+ )
1975+ return True
1976+
19071977 def assert_true (self , expression ):
19081978 if not expression :
19091979 raise AssertionError ("%s is not true" % expression )
0 commit comments