RobotFramework-自定义远程java关键字库能否返回Map类型

自定义的远程关键字库能使用哪些数据类型来和robotframework交互?先引用官网上的一段话
4.2.3   Supported argument and return value types

Because the XML-RPC protocol does not support all possible object types, the values transferred between the Remote library and remote servers must be converted to compatible types. This applies to the keyword arguments the Remote library passes to remote servers and to the return values servers give back to the Remote library.

Both the Remote library and the Python remote server handle Python values according to the following rules. Other remote servers should behave similarly.

Strings, numbers and Boolean values are passed without modifications.
Python None is converted to an empty string.
All lists, tuples, and other iterable objects (except strings and dictionaries) are passed as lists so that their contents are converted recursively.
Dictionaries and other mappings are passed as dicts so that their keys are converted to strings and values converted to supported types recursively.
Strings containing bytes in the ASCII range that cannot be represented in XML (e.g. the null byte) are sent as Binary objects that internally use XML-RPC base64 data type. Received Binary objects are automatically converted to byte strings.
Other types are converted to strings.
Note

Prior to Robot Framework 2.8.3, only lists, tuples, and dictionaries were handled according to the above rules. General iterables and mappings were not supported.

Binary support is new in Robot Framework 2.8.4.

概括起来java能传递的是: 数字类型 boolean String List Array Map。

当java关键字返回的是Map时,该如何书写?

1.远程库方法举例:注意返回类型是Map

package keywords.leading.testcases;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

@RobotKeywords
public class FjTest {

    @RobotKeyword("Example:\n" + "|test Map | key | value | \n")
    @ArgumentNames({ "key", "val" })
    public Map testMap(String key, String val) throws Exception {
        System.out.println("key=" + key + ";val=" + val);
        Map map = new HashMap();
        map.put("邓肯微笑遭驱逐!NBA史上10大怪异技犯吹罚",
                “http://baidu.com.cn");
        map.put(key, val);
        return map;
    }
}

2.robot framework用例书写举例:

1.rf若要实现对Map的遍历取值等操作,还需要引入一个内建库Collections;它提供了字典(Dictionary)操作相关的关键字,python中的字典数据结构对应于java的Map

2.使用${}变量接受Map;使用Collections库的方法操作Map


*** Settings ***
Library           Remote    http://127.0.0.1:8270/    WITH NAME    MyRemoteLibrary
Library           Collections

*** Test Cases ***
ReturnMap
    ${dct}    Test Map    hello    world
    @{keys}=    Get Dictionary Keys    ${dct}
    : FOR    ${key}    IN    @{keys}
    \    log    key=${key}
    \    ${v}=    Get From Dictionary    ${dct}    ${key}
    \    log    value=${v}


3.输出结果:

Starting test: Fjtest.RemoteLib.ReturnMap
20160120 17:09:04.534 :  INFO : key=hello;val=world
20160120 17:09:04.534 :  INFO : ${dct} = {'hello': 'world', u'\u9093\u80af\u5fae\u7b11\u906d\u9a71\u9010\uff01NBA\u53f2\u4e0a10\u5927\u602a\u5f02\u6280\u72af\u5439\u7f5a': 'http://baid.com...
20160120 17:09:04.536 :  INFO : @{keys} = [ hello | 邓肯微笑遭驱逐!NBA史上10大怪异技犯吹罚 ]
20160120 17:09:04.538 :  INFO : key=hello
20160120 17:09:04.539 :  INFO : ${v} = world
20160120 17:09:04.540 :  INFO : value=world
20160120 17:09:04.541 :  INFO : key=邓肯微笑遭驱逐!NBA史上10大怪异技犯吹罚
20160120 17:09:04.542 :  INFO : ${v} = http://baidu.com.cn
20160120 17:09:04.543 :  INFO : value=http://baidu.com.cn

Ending test:   Fjtest.RemoteLib.ReturnMap


===================

若关键字返回的Map的值是个对象怎么办?

例如下面例子:

@RobotKeyword("Example:\n" + "|test Map | 123 | 456 | \n")
	@ArgumentNames({ "key", "val" })
	public Map testMap(String key, String val)
			throws Exception {
		System.out.println("key=" + key + ";val=" + val);
		Map map = new HashMap();
		PlayResource res = new PlayResource(
				"电视剧",
				"排行",
				"赵又廷,姚晨,凤小岳,李晨,唐嫣,冯瓅,李光洁,王庆祥,吴军,王德顺 da lian huan!!!~",
				"http://baidu.com"
				"1234", null, false);

		map.put("p1", res);
		return map;
	}

class PlayResource {
	private String channelName;// 所属频道名称
	private String navName;// 所属标签名称
	private String name;// 资源名称
	private String playGetUrl;// 获取播放资源url
	private String pid;
	private String vid;
	private boolean ok;// 是否能播

	public PlayResource() {
		super();
		// TODO Auto-generated constructor stub
	}

	public PlayResource(String channelName, String navName, String name,
			String playGetUrl, String pid, String vid, boolean ok) {
		super();
		this.channelName = channelName;
		this.navName = navName;
		this.name = name;
		this.playGetUrl = playGetUrl;
		this.pid = pid;
		this.vid = vid;
		this.ok = ok;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getVid() {
		return vid;
	}

	public void setVid(String vid) {
		this.vid = vid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isOk() {
		return ok;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}

	public String getChannelName() {
		return channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public String getNavName() {
		return navName;
	}

	public void setNavName(String navName) {
		this.navName = navName;
	}

	public String getPlayGetUrl() {
		return playGetUrl;
	}

	public void setPlayGetUrl(String playGetUrl) {
		this.playGetUrl = playGetUrl;
	}

	@Override
	public String toString() {
		return "PlayResource [channelName=" + channelName + ", navName="
				+ navName + ", name=" + name + ", playGetUrl=" + playGetUrl
				+ ", pid=" + pid + ", vid=" + vid + ", ok=" + ok + "]";
	}
}


robot接收类型除了以上列出的类型外,其他均以java对象toString方法输出字符串形式!

输出如下:

Starting test: Fjtest.RemoteLib.ReturnMap
20160120 19:26:57.324 :  INFO : ${dct} = {'p1': u'PlayResource [channelName=\u7535\u89c6\u5267, navName=\u6392\u884c, name=\u8d75\u53c8\u5ef7,\u59da\u6668,\u51e4\u5c0f\u5cb3,\u674e\u6668,\u5510\u5ae3,\u51af\u74c5,\u674e\u5149\u6d01,\u738b\...
20160120 19:26:57.325 :  INFO : @{keys} = [ p1 ]
20160120 19:26:57.326 :  INFO : key=p1
20160120 19:26:57.329 :  INFO : ${v} = PlayResource [channelName=电视剧, navName=排行, name=赵又廷,姚晨,凤小岳,李晨,唐嫣,冯瓅,李光洁,王庆祥,吴军,王德顺 da lian huan!!!~, playGetUrl=http://baidu...
20160120 19:26:57.335 :  INFO : value=PlayResource [channelName=电视剧, navName=排行, name=赵又廷,姚晨,凤小岳,李晨,唐嫣,冯瓅,李光洁,王庆祥,吴军,王德顺 da lian huan!!!~, playGetUrl=http://baidu.com, pid=1234, vid=null, ok=false]
Ending test:   Fjtest.RemoteLib.ReturnMap


你可能感兴趣的:(自动化)