Four examples of automated testing models and their advantages and disadvantages

1. Linear 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 to log in to 126 mailbox, element clear(), send_keys(), click() operations
When positioning, I found that some elements could not be positioned. 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 in to 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 "Record log: URL input is correct"

# driver.switch_to_frame('x-URS-iframe') #Jump to iframe
    driver.switch_to.frame('x-URS-iframe') #Same as the above statement, jump to the iframe frame
    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 'NetEase Mailbox' in driver.title
    except AssertionError:
        print 'Email login failed'
    else:
        print 'Email login successful'
    
finally:
    #Operation: receive letters, write letters and other operations, I won’t write examples for now
    driver.quit()
    
print 'Test ends'

Second, modular driver test

1. Concept:

The repeated operations are successfully combined into separate modules, and are called when this module operation is needed during the execution of the use case.

Operation + (repeat operation, data) mixed together.

2. Advantages:

By eliminating duplication to the greatest extent, development efficiency is improved and the maintainability of test cases is improved.

3. Disadvantages:

Although the modularization steps are the same, the test data is different. For example, for repeated login modules, if the login users are different, the login script still needs to be written repeatedly.

4.Examples

Public module: Modular encapsulation of login and exit

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-27
@author:Jennifer
Project: Modularly drive test instances, placing duplicate login scripts in separate scripts for other use cases to call
'''
import time
classLogin():
    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'Exit').click()
        driver.quit()

Writing use case: The following code uses various positioning methods, which is worth learning. I will summarize this part again later.

Directly call the module’s login and exit methods.

# coding=utf-8
'''
Created on 2016-7-27
@author:Jennifer
Project:Send email
'''
from selenium import webdriver
import time

from test_5_2_public import Login #Because the public module file is named test_5_2_public
driver=webdriver.Firefox()
driver.implicitly_wait(30)
driver.get(r'http://www.126.com/') #Add r to the string to prevent escaping.
time.sleep(3)
driver.switch_to.frame('x-URS-iframe')
#Call login module
Login().user_login(driver)
time.sleep(10)
#send email
#Click outbox
#_mail_component_61_61 is a dynamic id, so it cannot be used for positioning
#driver.find_element_by_css_selector('#_mail_component_61_61>span.oz0').click()
#Cannot add u"//span[contains(text(),u'write')]", otherwise it cannot be located.
#The following positioning is to find the element whose span tag has text (contains)'write letter'. This positioning method is important.
driver.find_element_by_xpath("//span[contains(text(),'Write a letter')]").click()
#Fill in the recipient
driver.find_element_by_class_name('nui-editableAddr-ipt').send_keys(r'[email protected]')
#Fill in the topic
#Connect more attributes through and to uniquely mark an element
driver.find_element_by_xpath("//input[@class='nui-ipt-input' and @maxlength='256']").send_keys(u'Automated Test')
#Fill in the text
#Switch the current positioning to the embedded page of the frame/iframe form through switch_to_frame()
driver.switch_to_frame(driver.find_element_by_class_name('APP-editor-iframe'))
#Locate the email content in the embedded page
emailcontext=driver.find_element_by_class_name('nui-scroll')
#Fill in the email content
emailcontext.send_keys(u'This is the first automated test email')
#Jump back to the outermost page through switch_to().default_content()
#Note: Do not write switch_to().default_content(), otherwise AttributeError will be reported: SwitchTo instance has no __call__ method
driver.switch_to.default_content()
#driver.switch_to.parent_frame()
#Click to send
time.sleep(3)
#There may be elements that are invisible (the element is gray when viewed), and an ElementNotVisibleException error will be reported.
#There are many elements containing the word "send", so other restrictions must be added.
#sendemails=driver.find_element_by_xpath("//span[contains(text(),'Send')]")
sendemails=driver.find_element_by_xpath("//span[contains(text(),'send') and @class='nui-btn-text']")
time.sleep(3)

#Verify whether the email is sent successfully
try:
    assert 'Sent successfully' in driver.page_source
except AssertionError:
    print 'Email sent successfully'
else:
    print 'Failed to send email'

#Call exit module
Login().user_logout(driver)

Receipt example:

Directly call the module’s login and exit methods.

# coding=utf-8
'''
Created on 2016-7-27
@author:Jennifer
Project:Receive emails
'''
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/') #Add r to the string to prevent escaping.
time.sleep(3)
driver.switch_to.frame('x-URS-iframe')
#Call login module
Login().user_login(driver)
time.sleep(10)
#incoming mail
#Click to receive
#The following positioning is to find the element whose span tag has text (contains)'receiver'. This positioning method is important.
driver.find_element_by_xpath("//span[contains(text(),'receive')]").click()

#Verify whether you have entered the inbox, enter if no error is reported
try:
    #Click on one of the emails
    driver.find_element_by_xpath("//div[@sign='letter']").click()
except Exception as e:
    print e
else:
    print 'Received successfully'

#Call exit module
Login().user_logout(driver)

Three, data-driven testing

1. Concept:

It separates the test data and operations in the test, and the data is stored in another file and maintained separately.

Changes in data drive the execution of automated tests, ultimately causing changes in test results.

Operations + repeated operations + data separation.

2. Advantages:

In this way, data and repeated operations are separated, similar tests can be quickly added, and tests under different data situations can be completed.

3. Disadvantages

None yet

4.Examples

Read the username and password from the excel table and log in to the email address.

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-28
@author:Jennifer
Project: data-driven testing, the data is saved in excel and needs to be imported into the xlrd module
'''
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] #Get sheet0 page by default
    table = excelfile.sheet_by_index(by_index)#Get sheet0 page by default
    nrows=table.nrows #Get the number of rows on sheet 0 of excel
    colnames=table.row_values(colnameindex) #Get the list data of row 0 by default: two values name and password
    list =[] #Create an empty list to store the user password dictionary
    for rownum in range(1,nrows): #Initial row 0, starting from row 1
        row = table.row_values(rownum) #Get the list data of a certain row
        if row:
            app = {} #Create an empty dictionary to store a certain set of user password data
            for i in range(len(colnames)): #Currently it is 2
                app[colnames[i]] = row[i] #New data to the dictionary: loop twice, add two pairs of key-value to the dictionary
            list.append(app) #Add the new dictionary data to the list data
    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/') #Add r to the string to prevent escaping.
        time.sleep(3)
    
        driver.switch_to.frame('x-URS-iframe') #Same as the above statement, jump to the iframe frame
        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 email login failed'%(userlist[i]['name'])
        else:
            print 'User %s email login successful'%(userlist[i]['name'])
        
        finally:
            driver.quit()


if __name__=='__main__':
    Login()

4. 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 everyone is familiar with, so as to ultimately achieve the effect of an entire test 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 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.

Thank you to everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it directly if you can use it:

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!Yes Friends who need it can click on the small card below to get it