Selenium IDE 進階部分-Rollup策略

    資源推薦:

    視頻資源: 軟件測試相關係列視頻

     社區資源: 自動化測試交流羣


Selenium IDE Rollup策略

爲什麼需要使用Rollup策略呢?

使用Rollup策略可以使你的測試腳本看起來更加簡潔,測試命令集合重用性更強,使用Rollup可以讓你的腳本看起來像下面代碼一樣:

blob.png

其中,rollup命令中loggincommands是一個selenese指令集合,包含了登錄部分腳本指令,通過這種方式,可以將任何在多腳本中重複的部分提取出來,以供更多的測試腳本重複使用,這樣做有三大優勢:

§  腳本簡潔,通俗易懂。

§  公共部分selenese命令集合得到了最大限度的重用性。

§  提供了單點訪問入口,一處修改,處處使用的效果。

下面我們看一下如何來定義一組rollup策略,定義rollup需要新建一個.js爲結尾的javascript文件。

var manager = new RollupManager();

manager.addRollupRule({ ... });

manager.addRollupRule({ ... });

...

RollupManager作爲rollup策略實現的管理對象,我們可以向rollup manager中添加多組rollupRule,只需要簡單的使用RollupManager#addRollupRule(Object)方法就可以完成rollup策略的添加。

    添加完rollupRule之後,我們再來看一下如何定義rollup對象:

manager.addRollupRule({
	name: 'logincommands',
	description: 'register & login into newtours.com',
	args: [
		{
			name: 'email',
			description: 'register one user account, used it just for the identifier.',
			exampleValues: ['aa', 'bb', 'cc']
		}
		,{
			name: 'passwd',
			description: 'register one user account, used it just for the identifier.',
			exampleValues: ['122', '333', '222']
		}	
	], 
	commandMatchers: [
		{
			command: 'type'
			, target: 'id=email'
			, updateArgs: function(command, args) {
				args.email = command.value;
				return args;
			}
		}
		, {
			command: 'type'
			, target: 'name=password'
			, updateArgs: function(command, args) {
				args.passwd = command.value;
				return args;
			}
		}
		, {
			command: 'type'
			, target: 'name=confirmPassword'
			, updateArgs: function(command, args) {
				args.passwd = command.value;
				return args;
			}
		}
		, {
			command: 'clickAndWait'
			, target: 'name=register'
		}
	],
	getExpandedCommands: function(args) {
		var commands = [];
			
		commands.push({
			command: 'type'
			, target: 'id=email'
			, value: args.email
		});
		commands.push({
			command: 'type',
			target: 'name=password',
			value: args.passwd
		});

		commands.push({
			command: 'clickAndWait',
			target: 'name=register'
		});
		return commands;
	}
});

       
以上代碼片段是一個完整的
Rollup策略定義,在rollup對象中包含多個屬性:name, description, args, commandMatchers, getExpandedCommands,下面對這幾個屬性一一介紹:

    Namerollup策略的名字,需保證在rollup manager中唯一。

    Description: 對該rollup策略的描述,該描述作爲幫助文檔顯示給使用者。

     Args: array類型,可以有多組argsargs用於調用者傳入rollup策略的參數,比如:blob.png,這裏調用logincommand時傳入了兩個參數,也就對應了args數組中存在兩個args定義,在args定義中包含三個參數:name(參數名), description(參數描述), exampleValues(沒有實際意義,爲用戶展示參數值類型),Args中的定義的變量應用於兩個位置,commandMatchers中的updateArgs方法,以及getExpandedCommands方法。

CommandMatchers,該參數用於使用Selenium IDE中應用組策略功能,也就是Selenium IDEblob.png,只有當定義了commandMatchers時,這裏纔可用。

  commandMatchers用於匹配腳本中符合commandMachers定義的規則的selenese命令,並一鍵生成rollup命令。

                  {
                          command: 'type'
                          , target: 'id=email'
                          , updateArgs: function(command, args) {
                                   args.email = command.value;
                                   return args;
                          }
              }

    commandMatchers中可以定義多組command,每個command中可以包含command, target, updateArgs若干參數。        

        Command selenese命令,也就是該command matcher所要匹配的命令。       

       Target: 即元素定位器,這裏可以使用正則方式匹配多種target,如果使用正則也就是該command matcher可以匹配多個selenese命令,當然前提是同一個類型的command

       UpdateArgsFunction, 函數的實現用於動態接收rollup調用者穿來的參數及參數值,並動態的根據自定義規則更新args中定義的變量,注意,這裏只是根據調用者傳過來的參數值動態修改args變量值,最後將args對象作爲方法返回值return.

其中當定義rollupargs的值爲空時,如,args:[],此時updateArgs定義無效。

       GetExpandedCommands

       getExpandedCommands參數用於定義該rollup命令所包含的Selenese命令集合,也就是在運行時真正需要執行的命令

       該參數定義爲一個帶有args參數的函數,且該函數返回值爲數組,數組包含所有需要執行的命令集合。

               commands.push({
                          command: 'type',
                          target: 'name=password',
                          value: args.passwd
              });


       數組元素中包含三部分,即selenese指令的三部分,分別爲command, target, value, 其中value可以通過用戶調用rollup命令時傳入的參數進行動態更新。

       總結,rollup策略是一個selenium ide提供的一個非常強大的功能,使用rollup策略有三大好處,掌握rollup策略只需要腳本開發人員掌握javascript語言,就能輕鬆靈活運用。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章