├── books-patch.json ├── books-post.json ├── books-put.json ├── README.md ├── books.csv └── jmeter-rest.jmx /books-patch.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "${title}" 3 | } -------------------------------------------------------------------------------- /books-post.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "${title}", 3 | "author": "${author}" 4 | } -------------------------------------------------------------------------------- /books-put.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "${title}", 3 | "author": "${author}", 4 | "price": "$10.00" 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jmeter-rest-data-drivern 2 | 3 | # Goal: 4 | 5 | Our aim here is to come up with a framework to test REST API with different HTTP Methods. This test will be driven through a spreadsheet and the spreadsheet will contain all the input parameters, API URL, HTTP Method, request JSON, text response should be used for assertion etc. 6 | 7 | By using one single HTTP Sampler, we would like to send different types of request GET / POST / PUT / PATCH / DELETE. Even the HTTP Request body data will be added dynamically at run time. 8 | 9 | 10 | This test was created using [JSON-Server](https://github.com/typicode/json-server). 11 | 12 | 13 | # Test: 14 | 15 | ![](http://i0.wp.com/www.testautomationguru.com/wp-content/uploads/2017/03/dd-rest011.png?resize=767%2C423) 16 | -------------------------------------------------------------------------------- /books.csv: -------------------------------------------------------------------------------- 1 | test.id,test.description,title,author,http.path,http.method,input.json,assertion 2 | 1,Get all the books,,,/books/,GET,,[] 3 | 2,Add a new book - id=1,In Search of Lost Time,Marcel Proust,/books/,POST,books-post.json,Marcel Proust 4 | 3,Add a new book - id=2,Don Quixote,Miguel de Cervantes,/books/,POST,books-post.json,Miguel de Cervantes 5 | 4,Add a new book - id=3,Ulysses,James Joyce,/books/,POST,books-post.json,James Joyce 6 | 5,Add a new book - id=4,The Odyssey,Homer,/books/,POST,books-post.json,Homer 7 | 6,Add a new book - id=5,War and Peace,Leo Tolstoy,/books/,POST,books-post.json,Leo Tolstoy 8 | 7,Get the 4th Book Details,,,/books/4,GET,books-post.json,"""id"": 4" 9 | 8,Replace 4th Book details + Add price,The Odyssey,Test Automation Guru,/books/4,PUT,books-put.json,Test Automation Guru 10 | 9,Get the 4th Book Details,,,/books/4,GET,,"""$10.00""" 11 | 10,Update the title of the 3rd book,Jmeter is awesome,,/books/3,PATCH,books-patch.json,Jmeter is awesome 12 | 11,Delete 1st book,,,/books/1,DELETE,, 13 | 12,Delete 2nd book,,,/books/2,DELETE,, 14 | 13,Delete 3rd book,,,/books/3,DELETE,, 15 | 14,Delete 4th book,,,/books/4,DELETE,, 16 | 15,Delete 5th book,,,/books/5,DELETE,, 17 | -------------------------------------------------------------------------------- /jmeter-rest.jmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | false 7 | false 8 | 9 | 10 | 11 | jmeter.test.home 12 | ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)} 13 | = 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | continue 22 | 23 | false 24 | -1 25 | 26 | 1 27 | 1 28 | 1489675226000 29 | 1489675226000 30 | false 31 | 32 | 33 | 34 | 35 | 36 | , 37 | 38 | books.csv 39 | true 40 | false 41 | shareMode.all 42 | true 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | localhost 51 | 3000 52 | 53 | 54 | 55 | 56 | ${http.path} 57 | POST 58 | true 59 | false 60 | true 61 | false 62 | false 63 | 64 | 65 | 66 | 67 | 68 | 69 | Content-Type 70 | application/json 71 | 72 | 73 | 74 | 75 | 76 | d7fc748e-22b1-43b5-9f67-56334eaace08 77 | 78 | 79 | import org.apache.jmeter.engine.util.CompoundVariable; 80 | import org.apache.jmeter.protocol.http.util.HTTPArgument; 81 | 82 | //tets input parameters 83 | def httpMethod = vars.get("http.method"); 84 | 85 | //change the http method as per the data sheet 86 | sampler.setMethod(httpMethod); 87 | 88 | //set the HTTP request body 89 | if(!vars.get("input.json").equals("")){ 90 | def dataToBePosted = new CompoundVariable(new File(vars.get("jmeter.test.home") + vars.get("input.json")).text).execute(); 91 | def arg= new HTTPArgument("", dataToBePosted, null, true); 92 | arg.setAlwaysEncoded(false); 93 | sampler.getArguments().addArgument(arg); 94 | 95 | //HTTP Body is set to 96 | log.info(dataToBePosted); 97 | } 98 | 99 | // If you need to modify anything else, check HTTPSamplerBase API 100 | // https://jmeter.apache.org/api/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.html 101 | 102 | groovy 103 | 104 | 105 | 106 | 107 | ${assertion} 108 | 109 | Assertion.response_data 110 | false 111 | 16 112 | 113 | 114 | 115 | 300 116 | 117 | 118 | 119 | 120 | 121 | false 122 | 123 | saveConfig 124 | 125 | 126 | true 127 | true 128 | true 129 | 130 | true 131 | true 132 | true 133 | true 134 | false 135 | true 136 | true 137 | false 138 | false 139 | false 140 | true 141 | false 142 | false 143 | false 144 | true 145 | 0 146 | true 147 | true 148 | true 149 | true 150 | true 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | --------------------------------------------------------------------------------