Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Robotframework does interface test how to use to json keyword

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use the to json keyword for robotframework interface testing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In the Python language, the conversion between json data and dict dictionaries and objects is essential. First of all, do the concept introduction, because you know less about coding before testing, so if you want to learn systematically, you still have to learn some basic theories.

The JSON syntax is a subset of the JavaScript object representation syntax.

The data is kye:value in the name / value pair (in double quotes), followed by a colon, followed by the value: "firstName": "John"

Data is separated by commas

Curly braces save object {}

Square brackets save array []

An object can contain multiple name / value pairs:

{"firstName": "John", "lastName": "Doe"}

An array can contain multiple objects:

{

"employees": [

{"firstName": "John", "lastName": "Doe"}

{"firstName": "Anna", "lastName": "Smith"}

{"firstName": "Peter", "lastName": "Jones"}

]

}

Most interface tests deal with data in json format.

Example:

{

"id": "1496654252"

"code": 0

"msg": "ok"

"data": {

"tag_list": [

{

Game_id: "28"

"game_name": "Arena of Valor"

}

{

Game_id: "29"

"game_name": "Zhenmaoqu"

}

]

}

}

Some of the class libraries that interface testing depends on:

Json parsing involves the following libraries:

1. RequestsLibrary, installation command: pip install requests

2. HttpLibrary, installation command: pip install robotframework-httplibrary

3. HttpLibrary.HTTP, this library is in HttpLibrary, and HTTP is a class of HttpLibrary

4. Json, installation command: pip install jsonpatch

5. Collections, which is included with python and is directly introduced into the

There are two cases where the json value is taken:

1. Take json value: use the get json Value keyword

{

"id": "1496654252"

"code": 0

"msg": "ok"

}

Of course, objects in the array can also be fetched through separate / xx/xx/xx parsing layer by layer.

But the result is a string, with quotation marks.

If you want to remove the double quotation marks, you need to convert the json string into a dictionary object.

two。 Take the parenthesized part of json []

Need to convert the object format of json to Dictionary of python

Several methods:

(1) to json keyword in RequestsLibrary

(2) parse json keyword in HttpLibrary.HTTP

(3) loads keyword in json

In addition, the cost of Wuxi passenger flow http://www.xasgfk120.com/

Loads (): convert json data to dict data

Dumps (): convert dict data to json data

Result: dictionary format

Note that the result is returned:

Pretty_print=false defaults to false, which converts Content from Json to dictionary type, and if defined as true, converts Content from dictionary type to Json

'The game_id' becomes uplifted because a type conversion occurs and the str is converted to unicode

Json.loads () is used to convert data of type str to dict.

Please refer to: import json

Name_emb = {'axiafiuzhuo 1111' menagerie '2222' 'cantileverine' 3333''dawning' 1444'}

JsDumps = json.dumps (name_emb)

JsLoads = json.loads (jsDumps)

Print (name_emb)

Print (jsDumps)

Print (jsLoads)

Print (type (name_emb))

Print (type (jsDumps))

Print (type (jsLoads))

{'asides:' 1111, 'cations:' 3333, 'baked:' 2222, 'dudes:' 4444'}

{"a": "1111", "c": "3333", "b": "2222", "d": "4444"}

{ubiquitous: upright 1111, upright 3333, upright 2222, upright 4444'}

Take a value

Use the Get From Dictionary keyword, which is derived from the Collections library; you can see that Get From Dictionary is followed by two parameters, which are the Dictionary converted from json. Take the value in the first object, starting with the 0 subscript.

The first parameter, ${var1todict [0]}, refers to the dictionary converted by json in the previous step. Note that there should be a subscript, starting with 0: [0]; the second parameter is the key we want to take.

Thank you for reading! On "robotframework interface test to json keyword how to use" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report