简体   繁体   中英

Call a function from another class in Python unittest

I am performing mobile app automation in Appium using Python. Currently I have 2 classes and in one of my 2nd class I need to check if it returns to a certain screen. I have already validate that screen in my 1st class. I am hoping to call that function here, without repeating all the work.

I am getting HTML reporting as well. But I am unable to get the results what I am in need of.

I need to get an error if the function of UI validation fails, when called from the 2nd class.

# 1st class and it's function I need to call 

class Q_suite1_01(unittest.TestCase):

def signin_ui_validate(self):

    quallogi_logo = self.driver.find_element_by_class_name('android.widget.ImageView').is_displayed()
    sign_in = self.driver.find_element_by_xpath('//*[contains(@text,"Sign In") and contains(@class, "android.widget.TextView")]').is_displayed()
    phone_num = self.driver.find_element_by_xpath('//*[contains(@text,"Email or Phone") and contains(@class, "android.widget.TextView")]').is_displayed()
    phone_num_txtBox = self.driver.find_element_by_class_name('android.widget.EditText').is_displayed()
    pw = self.driver.find_element_by_xpath('//*[contains(@text,"Password") and contains(@class, "android.widget.TextView")]').is_displayed()
    pw_txtBox = self.driver.find_elements_by_class_name('android.widget.EditText')[1].is_displayed()
    login_bttn = self.driver.find_element_by_xpath('//*[contains(@text,"Login") and contains(@class, "android.widget.TextView")]').is_displayed()
    forget_pw = self.driver.find_element_by_xpath('//*[contains(@text,"Forgot password?") and contains(@class, "android.widget.TextView")]').is_displayed()
    sign_up = self.driver.find_element_by_xpath('//*[contains(@text,"Sign up") and contains(@class, "android.widget.TextView")]').is_displayed()

    if quallogi_logo is True:
        if sign_in is True:
            if phone_num is True:
                if phone_num_txtBox is True:
                    if pw is True:
                        if pw_txtBox is True:
                            if login_bttn is True:
                                if forget_pw is True:
                                    if sign_up is True:
                                        print 'Sign In UI is successfully validated'
    else:
        return



# function in 2nd class

def forgotPW_rq_cancel(self):
    cancel_bttn = self.driver.find_element_by_xpath('//*[contains(@text,"Cancel") and contains(@class, "android.widget.TextView")]')
    cancel_bttn.click()


        Q_suite1_01.signin_ui_validate

You can add True or False as return statement in the signin_ui_validate(self) function.

Example:

def signin_ui_validate(self):
    ...
    if quallogi_logo is True:
      if sign_in is True:
        if phone_num is True:
            if phone_num_txtBox is True:
                if pw is True:
                    if pw_txtBox is True:
                        if login_bttn is True:
                            if forget_pw is True:
                                if sign_up is True:
                                    print 'Sign In UI is successfully validated'
                                    return True
    else:
       return False

then in 2nd class you do what you want based on what signin_ui_validate() returned, True or False:

def forgotPW_rq_cancel(self):
    cancel_bttn = self.driver.find_element_by_xpath('//*[contains(@text,"Cancel") and contains(@class, "android.widget.TextView")]')
cancel_bttn.click()

    # It will enter if statement if signin_ui_validate() returns True
    if Q_suite1_01.signin_ui_validate():
       print('Do something here because True is returned')
    else:
       print('Do something else here because False is returned')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM