Four examples of automated testing models and their advantages and disadvantages

[Software Testing Interview Crash Course] How to force yourself to finish the software testing eight-part essay tutorial in one week. After finishing the interview, you will be stable. You can also be a high-paying software testing engineer (automated testing)

1. Linearity test
1. Concept:

A linear script generated by recording or writing the steps corresponding to the application. Simply to simulate the user's complete operation scenario.

(operations, repeated operations, data) all mixed together.

2. Advantages:

Each script is relatively independent and does not generate other dependencies or calls.

3. Disadvantages:

The development cost is high and there are duplicate operations between use cases. Such as repeated user login and logout.

The maintenance cost is high. Due to repeated operations, when repeated operations change, the scripts need to be modified one by one.

4. Linear test example

User login

You can apply for the following username and password by yourself when the time comes, so I won’t post the author’s username and password.

# coding=utf-8
'''
Created on 2016-7-20
@author: Jennifer
Project: Simple element operation login 126 mailbox, the clear(), send_keys(), click() operation of the element
found some elements during positioning It couldn't be located, and finally I found that there was an iframe, and another page was actually embedded in the frame.
If the iframe has a name or id, use switch_to_frame("name value") or switch_to_frame("id value") directly.
This is the most ideal method and the simplest and easiest to use method.
'''
from selenium import webdriver
import time

driver=webdriver.Firefox()
driver.get(r'http://www.126.com/') #Add r to the string to prevent escaping.
time.sleep(3)

print 'Start logging into email'

try:
    assert '126' in driver.title #title is a variable and cannot be title()
except AssertionError:
    print "error: The URL input is incorrect"
else:
    print "Log: The URL input is correct"

#    driver.switch_to_frame('x-URS-iframe')  #跳转到iframe框架
    driver.switch_to.frame('x-URS-iframe')   #同上面语句一样,跳转到iframe框架
    username=driver.find_element_by_name('email')
    username.clear()
    username.send_keys('Jennifer···')
    time.sleep(0.1)
    
    userpasswd=driver.find_element_by_name('password')
    userpasswd.clear()
    userpasswd.send_keys('·····')
    time.sleep(0.1)
    
    loginbt=driver.find_element_by_id('dologin')
    loginbt.click()
    time.sleep(3)
    
    try:
        assert '网易邮箱' in driver.title
    except AssertionError:
        print '邮箱登录失败'
    else:
        print '邮箱登录成功'
    
finally:
    #操作:收信,写信等操作,暂不写例子了
    driver.quit()
    
print '测试结束'
二,模块化驱动测试
1.概念:

将重复的操作独立成功共模块,当用例执行过程中需要用到这一模块操作时则被调用。

操作+(重复操作,数据)混合在一起。

2.优点:

由于最大限度消除了重复,从而提高了开发效率和提高测试用例的可维护性。

3.缺点:

虽然模块化的步骤相同,但是测试数据不同。比如说重复的登录模块,如果登录用户不同,依旧要重复编写登录脚本。

4.实例

公共模块:对登陆和退出进行模块化封装

以下的用户名密码到时候自己去申请,就不将笔者的用户密码贴出来了。

# coding=utf-8
'''
Created on 2016-7-27
@author: Jennifer
Project: Modular driver test instance, place repeated login scripts in separate scripts for other use cases to call
'''
import time
class Login ():
    def user_login(self,driver):
        username=driver.find_element_by_name('email')
        username.clear()
        username.send_keys('username')
        time.sleep(0.1)
        
        userpasswd=driver.find_element_by_name('password')
        userpasswd.clear()
        userpasswd.send_keys('password')
        time.sleep(0.1)
        
        loginbt=driver.find_element_by_id('dologin')
        loginbt.click()
        time.sleep(3)
        
    def user_logout(self,driver):
        driver.find_element_by_link_text(u'退出').click()
        driver.quit()
        

写信用例:以下代码用了各种定位方法,值得学习,后续再重新对这部分进行总结

直接调用模块的登录和退出方法。

# coding=utf-8
'''
Created on 2016-7-27
@author: Jennifer
Project:发送邮件
'''
from selenium import webdriver
import time

from test_5_2_public import Login  #由于公共模块文件命名为test_5_2_public
driver=webdriver.Firefox()
driver.implicitly_wait(30)
driver.get(r'http://www.126.com/')  #字符串加r,防止转义。
time.sleep(3)
driver.switch_to.frame('x-URS-iframe')
#调用登录模块
Login().user_login(driver)
time.sleep(10)
#发送邮件
#点击发件箱
#_mail_component_61_61是动态id,所以不能用于定位
#driver.find_element_by_css_selector('#_mail_component_61_61>span.oz0').click()
#不能加u"//span[contains(text(),u'写 信')]",否则定位不到。
#以下定位是查找span标签有个文本(text)包含(contains)'写 信' 的元素,该定位方法重要
driver.find_element_by_xpath("//span[contains(text(),'写 信')]").click()
#填写收件人
driver.find_element_by_class_name('nui-editableAddr-ipt').send_keys(r'[email protected]')
#填写主题
#通过and连接更多的属性来唯一地标志一个元素
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and @maxlength='256']").send_keys(u'自动化测试')
#填写正文
#通过switch_to_frame()将当前定位切换到frame/iframe表单的内嵌页面中
driver.switch_to_frame(driver.find_element_by_class_name('APP-editor-iframe'))
#在内嵌页面中定位邮件内容位置
emailcontext=driver.find_element_by_class_name('nui-scroll')
#填写邮件内容
emailcontext.send_keys(u'这是第一封自动化测试邮件')
#通过switch_to().default_content()跳回最外层的页面
#注:不要写成switch_to().default_content(),否则报AttributeError: SwitchTo instance has no __call__ method
driver.switch_to.default_content()
#driver.switch_to.parent_frame()
#点击发送
time.sleep(3)
#有可能存在元素不可见(查看元素是灰色的),会报ElementNotVisibleException错误
#包含发送二字的元素很多,所以还得再加上其他限制
#sendemails=driver.find_element_by_xpath("//span[contains(text(),'发送')]")
sendemails=driver.find_element_by_xpath("//span[contains(text(),'发送') and @class='nui-btn-text']")
time.sleep(3)

#校验邮件是否发送成功
try:
    assert '发送成功' in driver.page_source
except AssertionError:
    print '邮件发送成功'
else:
    print '邮件发送失败'

#调用退出模块    
Login().user_logout(driver)
收信用例:

直接调用模块的登录和退出方法。

# coding=utf-8
'''
Created on 2016-7-27
@author: Jennifer
Project:接收邮件
'''
from selenium import webdriver
import time

from test_5_2_public import Login
driver=webdriver.Firefox()
driver.implicitly_wait(30)
driver.get(r'http://www.126.com/')  #字符串加r,防止转义。
time.sleep(3)
driver.switch_to.frame('x-URS-iframe')
#调用登录模块
Login().user_login(driver)
time.sleep(10)
#接收邮件
#点击收信
#以下定位是查找span标签有个文本(text)包含(contains)'收 信' 的元素,该定位方法重要
driver.find_element_by_xpath("//span[contains(text(),'收 信')]").click()

#校验是否进入收件箱,没报错即进入
try:
    #点击其中一封邮件
    driver.find_element_by_xpath("//div[@sign='letter']").click()
except Exception as e:
    print e
else:
    print '成功收信'

#调用退出模块    
Login().user_logout(driver)
三,数据驱动测试
1.概念:

它将测试中的测试数据和操作分离,数据存放在另外一个文件中单独维护。

通过数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变。

操作+重复操作+数据分开。

2.优点:

通过这种方式,将数据和重复操作分开,可以快速增加相似测试,完成不同数据情况下的测试。

3.缺点

暂无

4.实例

从excel表格读取用户名密码,登录邮箱。

以下的用户名密码到时候自己去申请,就不将笔者的用户密码贴出来了。

# coding=utf-8
'''
Created on 2016-7-28
@author: Jennifer
Project:数据驱动测试,数据保存在excel中,需要导入xlrd模块
'''
from selenium import webdriver
import time
import xlrd

#Convert user password table to user password list
def exceltolist(excelfile,colnameindex=0,by_index=0):
    excelfile=xlrd.open_workbook(excelfile) #Open excel table
# table = excelfile.sheets()[by_index] #Default acquisition sheet0 page
    table = excelfile.sheet_by_index(by_index) #Get sheet0 page by default
    nrows=table.nrows #Get the row number of sheet0 page of excel
    colnames=table.row_values(colnameindex) #Get the list data of row 0 by default: name and password Two values
    ​​list =[] #Create an empty list to store the user password dictionary
    for rownum in range(1,nrows): #Initial behavior is 0, starting from row 1
        row = table.row_values(rownum) #Get a certain One row of list data
        if row:
            app = {} #Create an empty dictionary to store a certain set of user password data
            for i in range(len(colnames)):    #目前是2
                app[colnames[i]] = row[i]     #字典新增数据:循环两次,字典新增两对key-value
            list.append(app)                  #将新增的字典数据,添加到列表数据中                 
    return list

def Login():
    file=r'D:\pythontest\rightpassword\userpassword.xls'
    userlist=exceltolist(file)
    for i in range(len(userlist)):
        driver=webdriver.Firefox()
        driver.get(r'http://www.126.com/')  #字符串加r,防止转义。
        time.sleep(3)
    
        driver.switch_to.frame('x-URS-iframe')   #同上面语句一样,跳转到iframe框架
        username=driver.find_element_by_name('email')
        username.clear()
        username.send_keys(userlist[i]['name'])
        time.sleep(0.1)
        
        userpasswd=driver.find_element_by_name('password')
        userpasswd.clear()
        userpasswd.send_keys(userlist[i]['password'])
        time.sleep(0.1)
        
        loginbt=driver.find_element_by_id('dologin')
        loginbt.click()
        time.sleep(3)
        try:
            assert 'NetEase Mailbox' in driver.title
        except AssertionError:
            print 'User %s mailbox login failed'%(userlist[i] ['name'])
        else:
            print 'User %s email login successful'%(userlist[i]['name'])
        
        finally:
            driver.quit()


if __name__=='__main__':
    Login()
 four, keyword-driven testing

  The origin of keyword drive is very natural. Starting from the object-oriented idea, the same business logic will naturally be written into a class or function as a keyword to be called by different test scripts. When the test framework develops to the point where all test processes can be combined by written functions and classes, it has evolved to an advanced stage of keyword-driven. At this time, the development of test cases becomes test data and keywords. combination, and simplify this combination work into a form-filling task that is familiar to everyone, thereby ultimately achieving an effect where the entire test is driven by data and keywords.

       In a keyword-driven framework, you can create keywords and associated methods and functions. Then you create a function library that contains a logic that reads keywords and then calls related actions.
    Keyword-driven automated testing (also known as table-driven test automation) is a variant of data-driven automated testing that can support tests consisting of different sequences or multiple different paths. It is an application-independent automation framework that handles automated testing while also being suitable for manual testing. The keyword-driven automated testing framework is built on data-driven means, and the table contains instructions (keywords), not just data. The tests are developed as data tables using keywords, and they are independent of the automation tool that executes the tests. Keyword-driven automated testing is an effective improvement and supplement to data-driven automated testing.

        This automated testing model mainly consists of a core data-driven engine, component functions, support libraries and application mapping tables. The automated test first starts with the initial script. This script passes the high-level test table to the high-level driver. During the process of processing these tables, the high-level driver calls the middle-level driver when it encounters the middle-level test table. The middle-level driver does the same thing when processing the middle-level table. processing. As the low-level driver processes low-level tables, it attempts to keep the application and tests in sync. When the low-level driver encounters a low-level keyword component for a certain component, it determines the type of the component and calls the corresponding component function module to process the instruction operation. All these elements rely on information in the mapping table, which is the bridge between the automated test model and the application under test. The support library mainly completes some functions such as file processing, logging, and email sending.
The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

 

文档获取方式:

这份文档,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

以上均可以分享,只需要你搜索vx公众号:程序员雨果,即可免费领取

Guess you like

Origin blog.csdn.net/2301_79535544/article/details/133210449