I trying to use python3.7 + appium + unittest to build a simple automation test framework. And I got a issue that my test case script always fail at asserTrue() after encapsulation whole assert function into another python script. This assert is for check current screen have the text which user required. My test case script like:
......
class Test1(unittest.TestCase):
@classmethod
def setUpClass(self):
print("execute setUpClass")
@classmethod
def tearDownClass(self):
print("execute tearDownClass")
def test_demo(self):
time.sleep(1)
try:
assert_text = driver.find_element_by_android_uiautomator('new UiSelector().text("WLAN")')
except Exception as e:
assert_text = False
self.assertTrue(assert_text,'Fail to find text "WLAN"')
Text "WLAN" is 100% on current screen as an element. Above code could run successful with failure. But after I put following part into another script as a function, it will always got
AssertionError: False is not true : Fail to find text "WLAN"
try:
assert_text = driver.find_element_by_android_uiautomator('new UiSelector().text("WLAN")')
except Exception as e:
assert_text = False
self.assertTrue(assert_text,'Fail to find text "WLAN"')
encapsulation script assert_zsq.py is like:
def Verify_Text(self,req_text):
try:
assert_text = driver.find_element_by_android_uiautomator('new UiSelector().text("{0}")'.format(req_text))
except Exception as e:
assert_text = False
self.assertTrue(assert_text,'Fail to find text "{0}"'.format(req_text))
Also change test case script main action to:
def test_demo(self):
time.sleep(1)
Verify_Text(self,'WLAN')
Any idea about why asserTrue() alwasys fail after encapsulation even required text is on screen? Thanks in advance!
assertTrue()
method expects True or False, but find_element_by_android_uiautomator
is not returning True or False but MobileWebElement
.
You can try with:
assert_text = driver.find_element_by_android_uiautomator('new UiSelector().text("WLAN")').is_displayed()
is_displayed()
returns True or False, depending if it was able to locate element or not.
Now assert_text
should work as expected.