1. Problems with the kit
all of the test cases are added and executed once, for use case names without canonical test start
def suite():
suite = unittest.TestSuite
suite.addTest(LoginTestCase("test_log_in"))
suite.addTest(LoginTestCase("test_log_out"))
return suite
if __name__ == ‘__main__‘:
unittest.main(defaultTest = ‘suite‘)
2. Python exception
try:
self.driver.find_element_by_id(‘tv_subtopiccomments_content‘)
result = 0
except Exception as e:
result = 1
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
excel = xlrd.open_workbook(‘excel文件位置‘)
sheet = excel.sheets()[0] # 读取第一个sheet数据
a = []
for i in range(0, 10):
a.append(sheet.row_values(i)) # 读取保存在list里
print(a[i])
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 filesTo 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 filesThe 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 fileF.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