4.使用基于框架的架构
The framework provides an entirely different approach, although it is often used in conjunction with one or more data-driven testing strategies. Tom Arnold (one of the LAWST participants) discusses this approach in his book [8] and courses.
虽然框架通常与一个或一个以上的数据驱动测试策略一同使用,但它本身却提供了一种完全不同的方法。Tom Arnold(LAWST成员之一)在它的书和课程中论述了这种方法。
The framework isolates the application under test from the test scripts by providing a set of functions in a shared function library. The test script writers treat these functions as if they were basic commands of the test tool’s programming language. They can thus program the scripts independently of the user interface of the software.
框架通过提供共享函数库中的一套函数,把应用程序从测试脚本中分离。这样,测试脚本编写者在处理这里函数时,就可以把它们视为测试工具的编程语言的基本命令。因此,他们能单独编写软件的用户界面脚本。
For example, a framework writer might create the function, openfile(p). This function opens file p. It might operate by pulling down the file menu, selecting the Open command, copying the file name to the file name field, and selecting the OK button to close the dialog and do the operation. Or the function might be richer than this, adding extensive error handling. The function might check whether file p was actually opened or it might log the attempt to open the file, and log the result. The function might pull up the File Open dialog by using a command shortcut instead of navigating through the menu. If the program that you’re testing comes with an application programmer interface (API) or a macro language, perhaps the function can call a single command and send it the file name and path as parameters. The function’s definition might change from week to week. The scriptwriter doesn’t care, as long as openfile(x) opens file x.
例如,一个框架编写者创建了函数openfile(p)。该函数用来打开文件p。它可以通过下拉文件菜单、选择打开命令、复制文件名到文件名域、点击OK按钮关闭对话框来完成操作。此外,这个函数也可以变得更复杂一些,比如,添加可扩展的错误处理功能。它能检查文件p是否已经被打开,或者记录打开文件的尝试以及对结果做日志。该函数还可以用一个命令快捷键,而不是菜单导航来弹出文件打开对话框。如果你的测试程序涉及到一个应用编程接口(API)或宏语言,那么该函数或许能调用一个单独的命令,并把文件名和路径作为参数传递给它。函数的定义经常在变化。但只要openfile(x)能打开文件x,脚本编写者就不必在意。
Many functions in your library will be useful in several applications (or they will be if you design them to be portable). Don’t expect 100% portability. For example, one version of openfile() might work for every application that uses the standard File Open dialog but you may need additional versions for programs that customize the dialog.
很多函数库中的函数在一些应用程序中依然有用(或者当它们被设计成可移植的时,也会很有用)。不要指望100%的可移植性。例如,openfile()的某个版本可以在每个应用程序中使用标准文件打开对话框,但你可能需要为那些有定制对话框的程序设计额外的版本。
Frameworks include several types of functions, from very simple wrappers around simple application or tool functions to very complex scripts that handle an integrated task. Here are some of the basic types:
框架包括若干类函数,从简单应用程序或工具功能的打包到处理完整任务的复杂脚本。以下是一些基本的类型:
a. Define every feature of the application.
a. 定义应用程序的每种特性
You can write functions to select a menu choice, pull up a dialog, set a value for a variable, or issue a command. If the UI changes how one of these works, you change how the function works. Any script that was written using this function changes automatically when you recompile or relink.
你可以通过编写函数来选择菜单选项,弹出对话框,设定变量值或发布命令。如果用户界面改变了其中的一种工作方式,那么你也就改变了函数的工作方式。而当你重新编译或重新链接时,任何一个用该函数编写的脚本都能自动修改。
Frameworks are essential when dealing with custom controls, such as owner-draw controls. An owner-draw control uses programmer-supplied graphics commands to draw a dialog. The test-automation tool will know that there is a window here, but it won’t know what’s inside. How do you use the tool to press a button in a dialog when it doesn’t know that the button is there? How do you use the tool to select an item from a listbox, when it doesn’t know the listbox is there? Maybe you can use some trick to select the third item in a list, but how do you select an item that might appear in any position in a variable-length list? Next problem: how do you deal consistently with these invisible buttons and listboxes and other UI elements when you change video resolution?
在处理定制控件时,框架是最重要的,比如自绘制控件。一个自绘制控件利用程序员提供的图表命令来绘制一个对话框。自动化测试工具只能辨认窗口的存在,却不能辨认里面的东西。因此,当工具并不知道按钮是否在对话框中时,如何让它点击这个按钮呢?而当工具并不知道列表框是否在那里时,又如何让它选择其中的表项呢?或许,你可以用某种小技巧来选择列表中的第三项吧,但是你如何选择一个出现在可变长度的列表中任何位置的表项呢?还有一个问题:当改变视频协议后,你如何像往常那样处理这些不可见的按钮和列表框,以及其他的用户界面元素呢?
At the LAWST meeting, we talked of kludges upon kludges to deal with issues like these. Some participants estimated that they spent half of their automation development time working around the problems created by custom controls.
在LAWST会议上,我们谈到用基于组装件的组装件来处理这类问题。有一些与会者估计,他们把自动化开发的一半时间都用来处理由定制控件所带来的问题了。
These kludges are a complex, high-maintenance, aggravating set of distractions for the script writer. I call them distractions because they are problems with the tool, not with the underlying program that you are testing. They focus the tester on the weaknesses of the tool, rather than on finding and reporting the weaknesses of the underlying program.
这些组装件对于脚本编写者来说,是一系列复杂的、难以维护且越来越严重的干扰。之所以称它们是干扰,是因为它们不是由正在测试的程序本身引起的,而是由工具引起的。因此,它们使测试人员把精力放在工具的缺点上,而不是放在找出并报告程序本身的缺点上。
If you must contend with owner-draw controls, encapsulating every feature of the application is probably your most urgent large task in building a framework. This hides each kludge inside a function. To use a feature, the programmer calls the feature, without thinking about the kludge. If the UI changes, the kludge can be redone without affecting a single script.
如果你必须使用自绘制控件,那么封装应用程序的每种特性很可能是你构建框架的最紧迫且繁重的任务。这可以把每个组装件隐藏到一个函数中。如果要使用一个特性,程序员就调用该特性,而不必考虑组装件的存在。而如果用户界面发生改变,则可以在不影响单个脚本的情况下重建组装件。
文章来源于领测软件测试网 https://www.ltesting.net/