appium foundation Automation summary

--- --- restore content begins

Have been learning two weeks appium automation, the foundation often used to write Come on! Facilitate future easy viewing.

appium solve the Chinese can not enter and start the app automatically clear the data problem. In the pre-code inside with these three can be resolved.

desired_caps = {

    "unicodeKeyboard": "True", # use unicode input method 
"resetKeyboard": "True", # reset input to the initial state
  
"noReset": "True", # troubleshoot startup app automatically clear the data problem, add the phrase let the app startup does not clear the original data
}

Install the apk to your phone


driver.install_app ( "D: \\ download \\ 38193ab216f29fdc9243bd8c39b65f95.apk") # followed apk path

Remove App 
driver.remove_app ( 'com.ibox.calculators') # fill parameters package name
Analyzing app is installed on the phone, to fill the package name parameter 
result = driver.is_app_installed ( 'com.ibox.calculators')

Gets the current page of the document structure, used for assertion 
print (driver.page_source)

asserts the existence of elements of the page
if 'more' in driver.page_source: 
Print ( 'success, there is')
the else:
Print ( 'error, does not exist')

implicitly wait
driver.implicitly_wait (30)


get the package name and appActivity

1, first open the package name to obtain App
2, adb shell
3, dumpsys Activity | grep mFocusedActivity to get the package name and
appActivity

get the screen size
size = driver.get_window_size()


Screenshot positioning method:


Subsequent supplementary code
# TODO achieve appium1.9 version automatically log in the United States visiting the function 
from appium Import webdriver
 Import Time 


class OpenMG ():
     '' ' auto-login visiting the US App ' '' 

    DEF  __init__ (Self):
         '' ' to open the APP and initialization ' '' 
        desired_caps = {
             " PlatformName " : " the Android " ,
             " platformVersion " : " 5.1 " ,
             " deviceName ": "127.0.0.1:62001 " ,
             " appPackage " : " com.xk.span.zutuan " ,
             " appActivity " : " .module.main.ui.activity.MainActivity " ,
             # " appWaitActivity ":" .module.main.ui .activity.MainActivity ", 
            " unicodeKeyboard " : " True " ,   # TODO screen keyboard, to solve the problem appium not enter the Chinese 
            " resetKeyboard " : " True " ,
             " Moresheth ": " True "      # TODO start Do not clear app in the original data when the app 
        } 
        self.driver = webdriver.Remote ( ' http://127.0.0.1:4723/wd/hub ' , desired_caps) 
        self.driver.implicitly_wait ( 30 ) 

    DEF slide_pafe (Self):
         '' ' interface to achieve the function of the slide ' '' 

        # TODO swipe parameters, (X-axis start position, y-start, x-axis ends, the end of the y-axis, the last parameter is sliding speed, do not fill) 
        '' ' 
        # TODO slide up from the bottom, the end of the initial coordinate y1 is greater than y2, the greater the gap, the greater the sliding distance, 
        # slides from the TODO, smaller than a starting end coordinates y1 y2 , the greater the gap, the greater the distance of the sliding 
        # todo sliding from left to right, starting end coordinates x1 is greater than x2, the greater the gap, the greater the distance of the sliding 
        # TODO slide from right to left, the end of the initial coordinate x1 x2 smaller than , the greater the gap, the greater the sliding distance 
        '' '
        #Home Home picture to the slide 
        for I in Range (. 4 ): 
            self.driver.swipe ( 972, 386, 108, 386, 500 ) 
        self.driver.implicitly_wait ( 30 ) 
        self.driver.find_element_by_class_name ( ' android.widget.ImageView ' ) .click () 
        self.driver.implicitly_wait ( 30 ) 
        self.driver.find_element_by_id ( ' com.xk.span.zutuan: ID / image_canclePlay ' ) .click () 
        self.driver.implicitly_wait ( 30 ) 

    DEF register_page (Self) :
         '' ' is switched from the account register to the input interface ' ''
        self.driver.find_element_by_id('com.xk.span.zutuan:id/tv_tab_txt4').click()
        self.driver.find_element_by_id('com.xk.span.zutuan:id/stv_user_un_login').click()
        self.driver.find_element_by_id('com.xk.span.zutuan:id/stv_login_mobile').click()

    def input_account(self, phone, password):
        '''输入账号密码到首页界面'''
        self.phone = phone
        self.driver.find_element_by_id('com.xk.span.zutuan:id/et_mobile').send_keys(phone)
        self.driver.find_element_by_id ( ' com.xk.span.zutuan: ID / password ' ) .click () 
        self.driver.find_element_by_id ( ' com.xk.span.zutuan: ID / et_password ' ) .send_keys (password) 
        Self .driver.find_element_by_xpath ( ' // * [@ text = "Login"] ' ) .click () 
        the time.sleep ( 0.3 ) 

    DEF switch (Self):
         '' ' Home to switch to individual centers ' '' 
        self.driver. find_element_by_xpath ( ' // * [@ text = "personal center"] ' ) .click () 
        the time.sleep ( 0.3 ) 

    DEF finally_result (Self):
         ' ''The results assertion is correct, whether the account in the current element interface '' '
        IF self.phone in self.driver.page_source:
             Print ( ' account is correct, the login is successful ' )
         the else :
             Print ( ' account error, login failed ' ) 

    DEF click_exit (Self):
         '' ' Click to exit, clearing the login information ' ' ' 
        self.driver.implicitly_wait ( 30 ) 
        self.driver.find_element_by_id ( ' com.xk.span.zutuan: ID / iv_user_logout ' ) .click () ) .click () 
        self.driver.implicitly_wait(30 ) 
        self.driver.find_element_by_xpath ( ' // * [@ text = "OK"] ' 
        the time.sleep ( . 1 ) 

    DEF Closed (Self):
         '' ' Close driven ' '' 
        the time.sleep ( . 1 ) 
        Self. driver.quit () 


DEF runl ():
     '' ' according to the operation sequence of the calling method performed ' '' 
    mg = OpenMG () 
    mg.slide_pafe () 
    mg.register_page () 
    mg.input_account ( ' 1582 ' , ' mg123456 ' ) 
    mg.switch()
    mg.finally_result()
    mg.click_exit () 
    mg.closed () 


class SearchGoods (OpenMG):
     Search Goods ' '' DEF input_goods (Self, Content):
         '' ' operation of the search box and enter the product search ' '' 
        self.content = Content 
        self.driver .find_elements_by_class_name ( ' android.widget.TextView ' ) [0] .click () 
        self.driver.implicitly_wait ( 30 ) 
        self.driver.find_element_by_xpath ( ' // * [@ text = "keyword search or paste Taobao title"] ' ) .send_keys (self.content) 
        the time.sleep ( 0.5 ) 
        self.driver.find_element_by_id('''

     ' com.xk.span.zutuan: ID / tv_search_submit ' ).
        click()
        # self.driver.swipe (108, 1800, 108, 200 is, 500) 
        # self.driver.swipe (108, 200 is, 108, 800, 500) 
        the time.sleep (1 ) 

    DEF finally_result (Self):
         '' ' assertion results are correct, whether the account in the current element interface ' '' 
        iF self.content in self.driver.page_source:
             Print ( ' find the product correctly, test success ' )
         the else :
             Print ( ' find the product incorrectly, the test failed ' ) 
        self.driver.implicitly_wait(30)

        # . Super () Closed () 

    DEF home_pege (Self):
         '' ' search interface to return to the home page ' '' 
        self.driver.find_element_by_id ( ' com.xk.span.zutuan: the above mentioned id / iv_back ' ) .click () 
        Self .driver.find_element_by_id ( ' com.xk.span.zutuan: ID / iv_back ' ) .click () 


DEF RUN2 ():
     '' ' invoke method of performing an operation sequence ' '' 
    SG = SearchGoods () 
    sg.slide_pafe () 
    sg.register_page () 
    sg.input_account ( ' 158 ' , ' mg123456 ')
    sg.input_goods ( ' short sleeves ' ) .send_keys (price1) ) 
    SG.finally_result()
    sg.home_pege () 
    sg.switch () 
    sg.click_exit () 
    sg.closed () 


class Filter_Goods (SearchGoods):
     '' ' screening goods '' ' 

    DEF filter (Self, price1, price2):
         ' '' of the filter box operation '' ' 
        self.driver.implicitly_wait ( 30 ) 
        self.driver.find_element_by_xpath ( ' // * [the contains (@Text, "filter")] ' ) .click () 
        self.driver.find_element_by_xpath ( ' // * [contains (@text, "lowest")] ' 
        self.driver.find_element_by_xpath ( ' // * [contains (@text, "Highest price ")] ' ) .send_keys (price2)
        self.driver.implicitly_wait(30)
        self.driver.find_element_by_id('com.xk.span.zutuan:id/ok').click()


def run3():
    fg = Filter_Goods()
    fg.slide_pafe()
    fg.register_page()
    fg.input_account('1582', 'mg123456')
    fg.input_goods('书包')
    fg.filter(30,100)
    fg.finally_result()
    fg.home_pege()
    fg.switch()
    fg.click_exit()
    fg.closed()


def main():
    run1()
    run2()
    run3()


if __name__ == '__main__':
    main()

 

Guess you like

Origin www.cnblogs.com/xiamaojjie/p/11442336.html