Automated test case design examples

Before writing use cases, the author once again emphasizes several principles for writing automated test cases:

1. A script is a complete scene, from the user's login operation to the user's exit from the system and closing the browser.
2. A script only verifies one function point. Do not try to verify all functions after the user logs into the system and then exit the system. 3. Try to only verify the
forward logic of the function. Do not consider too much reverse logic verification and reverse logic. There are many situations (for example, there are many situations where hand numbers are entered incorrectly). On the one hand, the verification is more complicated and requires writing a large number of scripts. On the other hand, the automated scripts themselves are relatively fragile, and the verification ability of many abnormal logics is not strong. (We try to write scripts in accordance with the principles of normal user use)
4. There should be no correlation between scripts, that is to say, each script written is independent and cannot depend on or affect other scripts.
5. If the data is modified, the data needs to be restored.
6. Only verify the verification points in the entire script. Do not verify every step of the entire script.

1. Login use case example:

The author recommends writing automated test cases through excel  tables.
Use case 001:

Code example:

Note: Determine whether the user has logged in successfully by matching the user's nickname after logging in.
Use case script (login.py):

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
 
driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")
driver.maximize_window() #浏览器最大化
 
#登陆快播私有云
driver.find_element_by_id("user_name").send_keys("testing360")
driver.find_element_by_id("user_pwd").send_keys("198876")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
 
#获取用户名
now_user=driver.find_element_by_xpath("//div[@id='Nav']/ul/li[4]/a[1]/span").text
 
#用户名是否等于虫师,不等于将抛出异常
if now_user==u'虫师':
       print '登陆成功'
else:
       raise NameError('user name error!')
 
#退出
driver.find_element_by_class_name("Usertool").click()
time.sleep(2)
driver.find_element_by_link_text("退出").click()
time.sleep(2)
driver.close()

2. Add file use case instance

Use case 002:

 Note: Determine whether the file is added successfully by counting the number of files in the user list.

Example script (collect.py):

Note: Use case login and exit refer to use case 001. This use case only focuses on the logic code shared by the collection user. #Judge the current number of files

inputs=driver.find_elements_by_tag_name('input')
n=0
for i in inputs:
    if i.get_attribute('type')=="checkbox":
    n=n+1
print u"当前列表文件为%d" %n
 
 
 
#收藏用户分享文件
driver.find_element_by_class_name("collect").click()
time.sleep(3)
 
#再次获取当前文件的个数
inputs=driver.find_elements_by_tag_name('input')
ns=0
for ii in inputs:
     if ii.get_attribute('type')=="checkbox":
         ns=ns+1
print u"当前列表文件为%d" %ns
 
 
 
#判断执行收藏文件之后比收藏之间文件加1 ,否则抛异常
if  ns==n+1:
     print "ok!"
else:
     raise NameError('添加文件失败!!')

3. Delete file instance:

Use case 003:

Note: Because deleting a file changes the data of the file, if the script is executed multiple times, an exception will be thrown when all the files in the list are deleted. Therefore, after deleting a file, you need to add another file, but the adding file operation No verification is done.
Use case script (del_one_file.py): #Judge the current number of files

inputs=driver.find_elements_by_tag_name('input')
n=0
for i in inputs:
      if i.get_attribute('type')=="checkbox":
            n=n+1
print u"当前列表文件为%d" %n
 
 
 
#删除操作
driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[4]/table/tbody/tr/td/input").click()
driver.find_element_by_class_name("dele").click()
driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div").click()
time.sleep(4)
 
#再次获取当前文件的个数
inputs=driver.find_elements_by_tag_name('input')
ns=0
for ii in inputs:
         if ii.get_attribute('type')=="checkbox":
               ns=ns+1
print u"当前列表文件为%d" %ns
 
 
 
#判断执行删除单个文件之后比删除之后文件减1 ,否则抛异常
if  ns==n-1:
    print "ok!"
else:
     raise NameError('删除文件失败!!')
#收藏用户分享单个文件
 
 
driver.find_element_by_class_name("collect").click()
time.sleep(3)

4. Rename file use case example

Use case 004:

Note: It is actually difficult for us to find evidence (verification point) to prove that the renaming is successful when renaming files. Then the entire script runs without an error, and we can also vaguely judge that the functional test is OK.

Example script (renaming.py)

#Check the renamed file
driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[4]/table/tbody[5]/tr/td/input") .click()
time.sleep(3)

#Move the mouse to the "More" button to pop up the drop-down box

element=driver.find_element_by_class_name("more-fe") #Locate the "more" button
ActionChains(driver).move_to_element(element).perform() #Move the mouse to the "more" button to pop up the drop-down box
time.sleep(2 )   

#Select the data-action==rename (rename) option in the li tag (more drop-down box) and click

lis=driver.find_elements_by_tag_name('li')
for li in lis:
      if li.get_attribute('data-action') == 'rename':
            li.click()
time.sleep(2)

Filter the renamed input box of type==text in the input tag

inputs=driver.find_elements_by_tag_name('input')
for input in inputs:
       if input.get_attribute('type') == 'text':
                input.send_keys(u"new file name") #Perform duplicate name operation
                input.send_keys( Keys.ENTER) #Press Enter to confirm renaming
                time.sleep(2)

Summary:
In this chapter, we briefly compare the differences between manual test users and automated test cases, the principles of writing automated test cases, how to catch and throw exceptions through python, the get_screenshot_as_file() function provided by webdriver, and how to write automated test cases. with scripts etc.


However, the author should not rush to implement automated testing yet, although we can write a single test case and determine whether the test case runs successfully through exception capture. But only through integration with the testing framework can we truly use automated testing technology effectively and feasiblely.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/132759445