Best Python code snippet using robotframework-appiumlibrary_python
AssertElement.py
Source:AssertElement.py
...14 AssertElement class contains methods to verify web elements in the application15 '''16 def __init__(self, browser):17 self._browser = browser18 def element_should_contain_text(self, locator, expected):19 """Verifies element identified by `locator` contains text `expected`.20 It matches substring on the text of the element21 Key attributes for arbitrary elements are `id` and `name`22 """23 self.actual = self._get_text(locator)24 if not expected in self.actual:25 message = "Element '%s' should have contained text '%s' but "\26 "its text was '%s'." % (locator, expected, self.actual)27 log.mjLog.LogReporter("AssertElement","error",message)28 raise AssertionError(message)29 log.mjLog.LogReporter("AssertElement","debug","Element '%s' contains text '%s'" %(locator, expected))30 31 def element_should_not_contain_text(self, locator, expected):32 """Verifies element identified by `locator` does not contains text `expected`.33 It matches substring on the text of the element34 """35 self.actual = self._get_text(locator)36 if expected in self.actual:37 message = "Element '%s' should not have contained text '%s' but "\38 "it does." % (locator, expected)39 log.mjLog.LogReporter("AssertElement","error",message)40 raise AssertionError(message)41 log.mjLog.LogReporter("AssertElement","debug","Element '%s' contains text '%s'" %(locator, expected))42 def values_should_be_equal(self, actualValue, expectedValue):43 """Verifies that actualValue is equal to expectedValue44 """45 if actualValue != expectedValue:46 raise AssertionError("Actual value and Expected Values are not equal")47 log.mjLog.LogReporter("AssertElement","debug"," Actual value is equal to Expected Value") 48 def frame_should_contain_text(self, locator, text):49 """Verifies frame identified by `locator` contains `text`.50 """51 if not self._frame_contains(locator, text):52 raise AssertionError("Frame should have contained text '%s' "53 "but did not" % text)54 log.mjLog.LogReporter("AssertElement","debug","Frame '%s' contains text '%s'" %(locator, text))55 def page_should_contain_text(self, text):56 """Verifies that current page contains `text`.57 """58 if not self._page_contains(text):59 raise AssertionError("Page should have contained text '%s' "60 "but did not" % text)61 log.mjLog.LogReporter("AssertElement","debug","Page contains text '%s'" %( text))62 def page_should_contain_element(self, locator):63 """Verifies element identified by `locator` is found on the current page.64 """65 if not self._page_should_contain_element(locator):66 message = "Page should have contained %s but did not"\67 % (locator)68 raise AssertionError(message)69 log.mjLog.LogReporter("AssertElement","debug","Current page contains element %s" % (locator))70 def page_should_not_contain_text(self, text):71 """Verifies the current page does not contain `text`.72 """73 if self._page_contains(text):74 raise AssertionError("Page should have not contained text '%s' "75 "but it did " % text)76 log.mjLog.LogReporter("AssertElement","debug","Page does not contains text '%s'" %( text))77 def page_should_not_contain_element(self, locator):78 """Verifies element identified by `locator` is not found on the current page.79 """80 if self._page_should_contain_element(locator):81 message = "Page should have not contained %s but did not"\82 % (locator)83 raise AssertionError(message)84 log.mjLog.LogReporter("AssertElement","debug","Current page does not contains %s" % (locator))85 86 def element_should_be_disabled(self, locator):87 """Verifies that element identified with `locator` is disabled.88 """89 if self._is_enabled(locator):90 raise AssertionError("Element '%s' is enabled." % (locator))91 log.mjLog.LogReporter("AssertElement","debug","Element is disabled as expected - %s" % (locator))92 def element_should_be_disabled_1(self, locator):93 """Verifies that element identified with `locator` is disabled by searching disabled in the class attribute of the element.94 """95 if not self._is_disabled(locator):96 raise AssertionError("Element '%s' is enabled." % (locator))97 log.mjLog.LogReporter("AssertElement","debug","Element is disabled as expected - %s" % (locator))98 def element_should_be_enabled(self, locator):99 """Verifies that element identified with `locator` is enabled.100 """101 if not self._is_enabled(locator):102 raise AssertionError("Element '%s' is disabled." % (locator))103 log.mjLog.LogReporter("AssertElement","debug","Element is enabled as expected - %s" % (locator))104 def element_should_be_displayed(self, locator):105 """Verifies that the element identified by `locator` is displayed.106 Herein, displayed means that the element is logically visible, not optically107 visible in the current browser viewport. For example, an element that carries108 display:none is not logically visible, so using this keyword on that element109 would fail.110 """111 self.visible = self._is_visible(locator)112 if not self.visible:113 if not message:114 message = "The element '%s' should be visible, but it "\115 "is not." % locator116 raise AssertionError(message)117 log.mjLog.LogReporter("AssertElement","debug","Element is displayed as expected - %s" % (locator))118 def element_should_not_be_displayed(self, locator):119 """Verifies that the element identified by `locator` is NOT displayed.120 This is the opposite of `element_should_be_displayed`.121 """122 self.visible = self._browser.elements_finder(locator)123 if len(self.visible)>0:124 message = "The element '%s' should not be visible, "\125 "but it is." % locator126 raise AssertionError(message)127 log.mjLog.LogReporter("AssertElement","debug","Element is not displayed as expected - %s" % (locator))128 def element_should_be_selected(self, locator):129 """Verifies element identified by `locator` is selected/checked.130 """131 self.element = self._element_finder(locator)132 if not self.element.is_selected():133 raise AssertionError("Element '%s' should have been selected "134 "but was not" % locator)135 log.mjLog.LogReporter("AssertElement","debug","Element is selected as expected - %s" % (locator))136 def element_should_not_be_selected(self, locator):137 """Verifies element identified by `locator` is not selected/checked.138 """139 140 self.element = self._element_finder(locator)141 if self.element.is_selected():142 raise AssertionError("Element '%s' should not have been selected"143 % locator)144 log.mjLog.LogReporter("AssertElement","debug","Element is not selected as expected - %s" % (locator))145 def element_text_should_be_exact(self, locator, expected):146 """Verifies element identified by `locator` exactly contains text `expected`.147 In contrast to `element_should_contain_text`, this keyword does not try148 a substring match but an exact match on the element identified by `locator`.149 """150 log.mjLog.LogReporter("AssertElement","debug","Element should contains exact text: %s => %s" % (locator,expected))151 self.element = self._element_finder(locator)152 actual = self.element.text153 if expected != actual:154 message = "The text of element '%s' should have been '%s' but "\155 "in fact it was '%s'." % (locator, expected, actual)156 raise AssertionError(message)157 log.mjLog.LogReporter("AssertElement","debug","Element contains exact text: %s => %s" % (locator,actual))158 def current_frame_contains_text(self, text):159 """Verifies that current frame contains `text`.160 """161 if not self._is_text_present(text):162 log.mjLog.LogReporter("AssertElement","error","Frame should have contained text '%s' "163 "but did not" % text)164 raise AssertionError("Frame should have contained text '%s' "165 "but did not" % text)166 log.mjLog.LogReporter("AssertElement","debug","Current Frame contains text '%s'." % text)167 168 def current_frame_should_not_contain_text(self, text):169 """Verifies that current frame contains `text`.170 """171 if self._is_text_present(text):172 log.mjLog.LogReporter("AssertElement","error","Frame should not have contained text '%s' "173 "but did contain" % text)174 raise AssertionError("Frame should not have contained text '%s' "175 "but it did" % text)176 log.mjLog.LogReporter("AssertElement","debug","Current Frame does not contains text '%s'." % text)177 def verify_element_color(self, locator, expectedColor):178 """179 Verifies element color180 """181 pass182 183 def verify_text_in_dropdown(self, textList, valueToVerify):184 ''' Verifies the value identified by valueToVerify is present in list identified by textList185 186 '''187 try:188 count = 0189 for text in textList:190 if text == valueToVerify:191 count = count +1192 log.mjLog.LogReporter("AssertElement", "info", "verify_text_in_dropdown"193 " - "+valueToVerify+" is present in list")194 break195 if count == 0:196 log.mjLog.LogReporter("AssertElement", "error", "%s is not present in list" % valueToVerify)197 198 except:199 raise AssertionError("Error in verify_text_in_dropdown "+str(sys.exc_info()))200 201 def values_should_not_be_equal(self, actualValue, expectedValue):202 """Verifies that actualValue is not equal to expectedValue203 """204 if actualValue == expectedValue:205 raise AssertionError("Actual value and Expected Values are equal")206 log.mjLog.LogReporter("AssertElement","debug"," Actual value is not equal to Expected Value")207 #private methods208 def _element_finder(self, locator):209 '''210 _element_finder() - Method to invoke element_finder from browser class211 '''212 return self._browser.element_finder(locator)213 214 def _get_browser_driver(self):215 return self._browser.get_current_browser()216 217 def _frame_contains(self, locator, text):218 self._driver = self._get_browser_driver()219 element = self._element_finder(locator)220 221 self._driver.switch_to_frame(element)222 found = self._is_text_present(text)223 self._driver.switch_to_default_content()224 return found225 def _get_text(self, locator):226 element = self._element_finder(locator)227 if element is not None:228 return element.text229 return None230 def _is_text_present(self, text):231 locator = "//*[contains(., '%s')]" % (text);232 return self._is_element_contains(locator)233 def _is_element_present(self, locator):234 return self._element_finder(locator)235 236 def _is_element_contains(self, locator):237 self._driver = self._browser.get_current_browser()238 return self._driver.find_elements_by_xpath(locator) 239 def _page_contains(self, text):240 self._driver = self._get_browser_driver()241 self._driver.switch_to_default_content()242 if self._is_text_present(text):243 return True244 subframes = self._element_finder("xpath=//frame|//iframe")245 if subframes:246 for frame in subframes:247 self._driver.switch_to_frame(frame)248 found_text = self._is_text_present(text)249 self._driver.switch_to_default_content()250 if found_text:251 return True252 return False253 def _page_should_contain_element(self, locator):254 return self._is_element_present(locator)255 256 def _get_value(self, locator, tag=None):257 element = self._element_finder(locator, True, False, tag=tag)258 return element.get_attribute('value') if element is not None else None259 def _is_enabled(self, locator):260 element = self._element_finder(locator)261 if not element.is_enabled():262 return False263 read_only = element.get_attribute('readonly')264 if read_only == 'readonly' or read_only == 'true':265 return False266 return True267 def _is_disabled(self, locator):268 element = self._element_finder(locator)269 if "disabled" in element.get_attribute('class').lower():270 return True271 return False272 def _is_visible(self, locator):273 element = self._element_finder(locator)274 if element is not None:275 return element.is_displayed()276 return None277if __name__ == "__main__":278 params = {"name" : "Vinay"}279 myBrowser = Browser(params)280 myBrowser.go_to("http://google.com")281 Webaction = WebElementAction(myBrowser)282 Webaction.input_text("SearchButton","Vinay")283 Webaction.press_key("SearchButton","ENTER")284 AssertMethods = AssertElement(myBrowser)285 AssertMethods.page_should_contain_text("Indian film")286 print (1)287 AssertMethods.element_should_contain_text("SearchTools", "Search tools")288 print (2)289 AssertMethods.page_should_contain_element("SearchIcon")290 print (3)291 AssertMethods.page_should_not_contain_text("ShoreTel")292 print (4)293 AssertMethods.element_should_be_enabled("SearchTools")294 print (5)295 #AssertMethods.element_should_be_disabled("SearchIcon")296 AssertMethods.element_should_be_displayed("SearchIcon")297 #AssertMethods.page_should_not_contain_element("Mapper")298 print ("6 sec")299 AssertMethods.element_text_should_be_exact("SearchTools", "Search tools")300 time.sleep(3)301 myBrowser.quit()
test_element.py
Source:test_element.py
...204 el.text_should_be_disabled = MagicMock()205 el.text_should_be_disabled.return_value = True206 result = el.text_should_be_disabled(device=MagicMock(), className=MagicMock())207 assert result == True208def test_element_should_contain_text():209 el = ExpectedConditions()210 el.element_should_contain_text = MagicMock()211 el.element_should_contain_text.return_value = True212 result = el.element_should_contain_text(device=MagicMock(), className=MagicMock(), text=MagicMock())213 assert result == True214def test_element_should_not_contain_text():215 el = ExpectedConditions()216 el.element_should_not_contain_text = MagicMock()217 el.element_should_not_contain_text.return_value = True218 result = el.element_should_not_contain_text(device=MagicMock(), className=MagicMock(), text=MagicMock())219 assert result == True220def test_check_element_visible():221 el = ExpectedConditions()222 el.check_element_visible = MagicMock()223 el.check_element_visible.return_value = True224 result = el.check_element_visible(device=MagicMock(), className=MagicMock(), text=MagicMock())225 assert result == True226def test_check_element_non_visible():...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!