自定義的遠端關鍵字庫能使用哪些資料類型來和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<String, String> testMap(String key, String val) throws Exception {
System.out.println("key=" + key + ";val=" + val);
Map<String, String> map = new HashMap<String, String>();
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<String, PlayResource> testMap(String key, String val)
throws Exception {
System.out.println("key=" + key + ";val=" + val);
Map<String, PlayResource> map = new HashMap<String, PlayResource>();
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