python + appium app instance of the use of automated methods

# -*- coding: utf-8 -*-
import os
import sys
import time
import unittest

from appium import webdriver
# from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
from appium.webdriver.common.touch_action import TouchAction


global driver


class MyTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 初始化测试平台
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = '78d358ad'
desired_caps['appPackage'] = 'com.wochacha'
desired_caps['appActivity'] = 'com.wochacha.StartupActivity'
# desired_caps['appWaitActivity'] = ''
desired_caps["unicodeKeyboard"] = "True" # 输入中文
desired_caps["resetKeyboard"] = "True" # 输入中文

cls.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {WebView.setWebContentsDebuggingEnabled(true);}
# cls.driver.implicitly_wait(8)
time.sleep(8)

test_home_page DEF (Self):
"" "Native Home page and page switching H5" ""
the time.sleep (2)
Print (self.driver.contexts)
contexts = self.driver.contexts
self.driver.switch_to.context (contexts [ 1]) # H5 to switch to page
the time.sleep (2)
Print (self.driver.current_context)
self.driver.switch_to.context ( "NATIVE_APP") to switch to the native page #

DEF test_native_h5 (Self):
"" "Home native page and H5 page switching" ""
context at # Note to switch webview page H5 time, first print to see if there H5 page
# because some H5 page does not open debug mode, then, even with h5 page can not be switched
# solution, students need to develop to play a test package, plus some debugging code
Print (self.driver.context)
Print (self.driver.contexts) # print all contexts
print (self.driver .current_context)
Print (self.driver.current_activity) # print the current Activity
self.driver.switch_to.context ( "NATIVE_APP") # switch to original page
self.driver.switch_to.context ( "WEBVIEW_com.android.browser") # switch to the H5 page

test_find_element DEF (Self):
"" "The method of locating elements" ""
self.driver.find_element_by_id ( "ID") ID # Location
self.driver.find_element_by_name ( "name") # name positioning
self.driver.find_element_by_link_text ( "text" ) # link name positioning
self.driver.find_element_by_partial_link_text ( "text") # visible link text positioning by elemental part
self.driver.find_element_by_tag_name ( "name") # name positioned elements by searching for the tags of html
self.driver.find_element_by_xpath ( "xpath ") # path to locate
self.driver.find_element_by_class_name (" android.widget.LinearLayout ") # class name positioning
self.driver.find_element_by_css_selector (" css ") # css selector positioned

test_find_elements_list DEF (Self):
"" "The method of the positioning elements of a complex set of" ""
self.driver.find_elements_by_id ( "ID") ID # collection of elements
self.driver.find_elements_by_name ( "name") # name a collection of elements
self.driver.find_elements_by_link_text ( "text") # link elements of the collection name
self.driver.find_elements_by_partial_link_text ( "text") # some elements visible collection of links
self.driver.find_elements_by_tag_name ( "name") # html tag name collection
self.driver.find_elements_by_xpath ( "xpath" ) # path to locate the set
self.driver.find_elements_by_class_name ( "android.widget.LinearLayout") # class name set positioning
elements = self.driver.find_elements_by_css_selector ( "css") # css selector set positioning
elements [0] .click () selecting a plurality of set # to locate inside the first few

test_element_action DEF (Self):
"" "element method of operation" ""
Element = self.driver.find_element_by_id ( "the above mentioned id")
element.click () # Click
element.clear () # remove an element content
element.text () # returns text content elements
element.submit (self) # submit the form
element.is_enabled () # elements is available
element.is_slected () # elements are optional
element.is_displayed () # element is visible
element.send_keys ( "+ selenium in English ") # input method

test_swipe_all DEF (Self):
"" "Summary sliding method" ""
self.driver.swipe (500, 1700, 500, 100, 500) slides up # (slide may not be very stable coordinate)

el1 = self.driver.find_element_by_name ( "black list of goods")
EL2 = self.driver.find_element_by_name ( "Exposure bar")
self.driver.scroll (EL1, EL2) # slid from one element to another element of stability, this recommendation
TouchAction (self.driver) .press (el1) .move_to (el2) .release (). perform () # slid from one element to another element

# Continuous sliding can be used as a sliding screen unlocking
TouchAction (). Press (el1) .move_to (el2) .move_to (el3) .move_to (el4) .release (). Perform () # coordinate continuous slide
TouchAction (self.driver ) .press (x, y) .move_to (x, y) .move_to (x, y) .move_to (x, y) .release (). perform () # continuous sliding element

# Get the element size (height and width), according to the screen resolution slidably
X = self.driver.get_window_size () [ 'width']
Y = self.driver.get_window_size () [ 'height']
self.driver.swipe (x * 0.5, y * 0.75 , x * 0.5, y * 0.25, 500) sliding on a #
self.driver.swipe (0.5 X *, Y * 0.25, 0.5 X *, Y * 0.75, 500) # falling
self. driver.swipe (x * 0.75, y * 0.5, x * 0.25, y * 0.5, 500) # left slider
self.driver.swipe (x * 0.25, y * 0.5, x * 0.75, y * 0.5, 500) # right slide

test_pinch_zoom DEF (Self):
"" "kneaded screen and enlarge the screen" ""
E1 = self.driver.find_element_by_id (ID)
# kneader (Pinch) kneaded screen (two fingers to move to narrow the screen)
self.driver.pinch ( E1 = Element)
# amplification (zoom) to enlarge the screen (two fingers move outwardly to enlarge the screen)
self.driver.zoom (Element = E1)

test_screen_shot DEF (Self):
"" "screenshot method" ""
self.driver.save_screenshot ( "D: /appium/screenshot/homepage.jpg") # save only Picture
self.driver.get_screenshot_as_file ( "D: / appium / screenshot /homepage.jpg ") # full path, IOError returns False
self.driver.get_screenshot_as_base64 () # interception of the current screen images for html page embedded directly in the base64-encoded picture
self.driver.get_screenshot_as_png () # get the current binary file screenshot data string

test_get_message DEF (Self):
"" "get current information" ""
self.driver.current_url # get the current url
self.driver.page_source # get the page source
self.driver.current_package # get the current package name
self.driver.current_activity # get the current Activity
self.driver.current_context # listed in the current context

test_pull_file DEF (Self):
"" "Push device file" ""
# files out of the machine (the Pull File)
self.driver.pull_file ( 'Library / the AddressBook / AddressBook.sqlitedb')
# push into the device file
data = "Push Test"
path = "/data/local/tmp/file.txt"
self.driver.push_file (path, data.encode ( 'Base64'))
# self.driver.push_file (path, data.encode ( ' Base64 ',' errors'))
# self.driver.push_file (path, base64.b64encode (bytes ( "Push Test", 'UTF-. 8')))

test_js DEF (Self):
"" "the JS operation, performed" ""
# current window / frame (especially the Html iframe) synchronization executing javascript code
driver.execute_script ( 'document.title')
# asynchronous execution code, other code performing
driver.execute_async_script ( 'document.title')

test_system DEF (Self):
"" "Miscellaneous Function System" ""
self.driver.app_strings (Language = None, string_file = None) # string acquisition application
self.driver.background_app (5) # 5 to the current application in the background second
self.driver.hide_keyboard () # hide the keyboard
self.driver.keyevent (176) # key event (Key event) transmits to the device a key event
self.driver.toggle_location_services () # open position of the positioning device is provided Andrews
# open activity or an application, only the end of Andrews (Baidu rice and packet activity name)
self.driver.start_activity ( 'com.nuomi', 'com.baidu.bainuo.dex.InstallDexActivity')

test_lock_and_open_notifaication DEF (Self):
"" "Open the notification bar and lock screen" ""
self.driver.open_notifications () # open the notification shade
self.driver.find_element_by_id. ( "com.android.systemui: id / clear_all_button") click ( ) # click Clear notice
self.driver.lock (5) # lock screen five seconds

test_remove_install_reset DEF (Self):
"" "uninstall and install app" ""
Are self.driver.is_app_installed ( "com.wochacha") # install App
self.driver.remove_app ( "com.wochacha") # delete the application
self. driver.install_app ( "D: /wochacha.apk") # install applications
self.driver.reset () # reset the application

test_launch_close_shake DEF (Self):
"" "startup and shutdown app, shake App" ""
# These three methods are being given, although not commonly used
self.driver.launch_app () # start the application
self.driver.close_app () # close the application
self.driver.shake () # shake

@classmethod
DEF tearDownClass (CLS):
cls.driver.quit () # sentence must be added, without re-start appium will not create a new sessionid
# cls.driver.close () # close the current window


the __name__ == IF '__main__':
# unittest.main (the verbosity = 2)
date = The time.strftime ( "the Y% m% D%") # date is defined as the date, time to time 20,180,710 date
tm = time.strftime ( "% Y% m% d- % H% m% S") # 20180710-124530 date - hour, minute
# hms = time.strftime ( "% H % m% S") # 124530 minutes and seconds

# # Test set to join the test case construction
Suite = unittest.TestSuite ()
suite.addTest (MyTests ( "test_home_page")) # Home
# suite.addTest (MyTests ( "test_native_h5" )) # primary pages and switch the H5
# suite. addTest (MyTests ( "test_lock_and_open_notifaication") ) # lock screen and open the notification shade
# execute test cases TextTestRunner
unittest.TextTestRunner (). RUN (Suite) # execute test cases
. "" "
# execute test cases HtmlTestRunner
path =" ./report/ home_page / "+ date +" / "
path # determines whether a directory exists, can not exist, create
iF not os.path.exists (path):
os.makdirs (path)
the else:
Pass
# report_path path = + Time +" Report. html "# save the run results to the report, the name of a defined path and file name, run the script
report_path = path + time +" - home_page_1_report.html "# Do not add time to the test report name
report_title = u "Baidu glutinous Home home_page_1 test report."
desc = u'Appium automated testing report details: '

with open(report_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(suite)
# 关闭report,脚本结束
report.close()
"""

Guess you like

Origin www.cnblogs.com/D-zsd/p/11309218.html