Appium Automated Testing (5)-Some Pyhon operations

Source: Internet
Author: User

1. Problems with the kit
all of the test cases are added and executed once, for use case names without canonical test start
   
  1. def suite():
  2. suite = unittest.TestSuite
  3. suite.addTest(LoginTestCase("test_log_in"))
  4. suite.addTest(LoginTestCase("test_log_out"))
  5. return suite
  6. if __name__ == ‘__main__‘:
  7. unittest.main(defaultTest = ‘suite‘)
2. Python exception
   
  1. try:
  2. self.driver.find_element_by_id(‘tv_subtopiccomments_content‘)
  3. result = 0
  4. except Exception as e:
  5. result = 1
  6. self.assertEqual(result,1,‘delete topic sub comment failed‘)
3. Use the ADB command to open an exact page by ID

Remark: Import OS
4, Package (!!) Init file required for each package)
from  util  import  add  
Reference: File name. class name (). Method name (parameter):Add.add (). FindByID (selfdriver)
Function

4, image comparison using Pil:python Imaging Library, is already the Python platform in fact the image processing standard library. The PIL feature is very powerful, but the API is very easy to use. 
http://effbot.org/imagingbook/
5. Python reads Excel data
   
  1. excel = xlrd.open_workbook(‘excel文件位置‘)
  2. sheet = excel.sheets()[0] # 读取第一个sheet数据
  3. a = []
  4. for i in range(0, 10):
  5. a.append(sheet.row_values(i)) # 读取保存在list里
  6. print(a[i])
  7. print(sheet.row_values(1)) # 打印第i行 print(sheet.col_values(0)) # 打印第j列
6, install XLRD, XLWT plug-in, read Excel file
Download: HTTPS://PYPI.PYTHON.ORG/PYPI/XLWT
Unzip, enter the directory from the command window
Run python setup.py install
Import it on the Pycharm interface.


Python file read/write 1, reading files

To read the TXT file, to define an identifier, to call the close function after opening the file

f = File (name[, mode[, Buffering])
Entry parameters: Name File name
Mode string
Buffering whether buffered (0= not buffered, 1 = buffered, int number of >1 = buffer size)

Mode option
R: Read-open, read-only file, exception occurs if file does not exist
W : Open in writing, can only write files, if the file does not exist, create the file, if the file already exists, first empty, then open the file
RB: Open in binary read mode, only read files, if the file does not exist, an exception will occur
WB: Opened in binary write, can only write files, if the file does not exist, create the file. If the file already exists, empty it before opening the file
RT: Open as text read, only read files, if the file does not exist, an exception will occur
WT: Open as text, write only the file, if the file does not exist, create the file. If the file already exists, empty it before you open it
rb+: Open in binary read, can read, write the file, if the file does not exist, an exception will occur
wb+: Open in binary write, can read, write the file, if the file does not exist, create the file if the file already exists, first empty, then open the file

report =Open(Path,' R ')# Open readable file as read filePrint("before changed:"+ report.Read() +' \ n ')#read一次读取所有Report.Close() Report =Open(Path,' WB ')# Open writable fileReport.Write("yes! It ' s alien! ") Report.Close() Report =Open(Path,' R ')Print(' after changed: '+ report.Read() +' \ n ') Report.Close()

Introduce with, automatically call the close function, prevent intermediate error, the system open too many files

with open(‘/path/to/file‘, ‘r‘) as f: print f.read()

If the file size cannot be determined, repeated calls to read (size) are insured; if it is a configuration file, calling ReadLines () is the most convenient:

for line in f.readlines():      print(line.strip()) # 把末尾的‘\n‘删掉
2. Read binary files

The default is to read the text file, and it is an ASCII-encoded text file. To read a binary file, compare slices, videos, and so on, open the file in ' RB ' mode:

f = open ('/users/michael/test.jpg ', ' RB ') F.read () '\xff\xd8\xff\xe1\x00\x18Exif\x00\x00 ... '#Hexadecimal representation of bytes
3. Clear the contents of the file

F.truncate ()

4. Read a line of TXT

# with open (path, ' R ') as FD:
# for line in FD:
# Lnum + = 1
# if (lnum = = 11):
# token = line[15:-1]
# Print (token)
# Fd.close ()

5. Read JSON data, key value
defGetValue(testfile,Key):
Res=[]
s="'
withOpen(testfile, ' R ') asF:
forLineinchF:
Line=line.decode (' GBK ')
if(Line.find (Key) <>-1):
S=line.split (Key) [1].strip (). Split ("')[0].strip ()
returnS

Print(GetValue (path, ' X-access-token: '))



From for notes (Wiz)

Appium Automated Testing (5)-Some Pyhon operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.