Hey all, Can someone help out in integrating the w...
# help
j
Hey all, Can someone help out in integrating the wiremock with appium? If someone could share a git repository, it would be of great help! 🙂
l
Hi, I am not sure there are many of us who have used appium to any great extent. There are a few people in the community who have integrated WireMock with it so it might be worth searching slack and reaching out to them to see if they can help
ž
Do you have any specific question?
b
I’ve done it with Selenium: https://github.com/basdijkstra/parabank-wiremock Don’t think it’ll be much different with Appium, it’s just another testing library and built on top of Selenium, too. This example uses JSON mapping files because it’s a SOAP service I’m mocking, but the mocks could just as easily be defined in code. As long as you know what calls to mock you should be good. If not, we’ll need a more specific question.
❤️ 1
👍 1
j
com.github.tomakehurst.wiremock.common.JsonException: { "errors" : [ { "code" : 10, "source" : { }, "title" : "Error parsing JSON", "detail" : "Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (String)\"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n<title>Error 403 No valid crumb was included in the request</title>\n</head>\n<body><h2>HTTP ERROR 403 No valid crumb was included in the request</h2>\n<table>\n<tr><th>URI&lt;/th&gt;&lt;td&gt;/ admin/mappings&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;th&gt;STATUS</th><td>403</td></tr>\n<tr><th>MESSAGE:</th><td>No valid crumb was included in the request</td></tr>\n<tr><th>SERVLET:</th><td>Stapler</td></tr>\n</table>\n<hr/><a href=\"https://eclipse.org/jetty\"[truncated 56 chars]; line: 1, column: 2]" } ]
WireMockServer wireMockServer = new WireMockServer( _options_() .usingFilesUnderDirectory("/iQ-automation/src/test/java/Wiremock") .notifier(new ConsoleNotifier(true))); wireMockServer.start(); _stubFor_(_post_(_urlMatching_("/auth/api/v1/Authentication/login")) .willReturn(_aResponse_() .withBodyFile("auth_api_v1_authentication_login.json.json") .withStatus(400)));
Can someone help out with the above error? I have added this piece of code down and it throws the error? @Žiga sternad @Lee Turner
b
Are you trying to let the mock return HTML? Are you accidentally confusing mapping files with response body files? The error looks to me like you're trying to parse an HTML file as a mapping file, which it isn't.
usingFilesUnderDirectory
allows you to use a non-default root folder (i.e., something other than
src/test/resources
) for your mapping files and response body files. In other words, it'll look for
auth_api_v1_authentication_login.json.json
(is that filename correct, with the double extension?) under
/iQ-automation/src/test/java/Wiremock/__files
. Is it there?
j
{
"id" : "1850cfe7-8e96-4c24-877f-40de1e5d6c32", "name" : "auth_api_v1_authentication_login", "request" : { "url" : "/auth/api/v1/Authentication/login", "method" : "POST", "bodyPatterns" : [ { "equalToJson" : "{ \"email\": \"emily_foulkes+d3@hotmail.com\", \"password\": \"Password1\"}", "ignoreArrayOrder" :
*true*,
"ignoreExtraElements" :
true
} ]
},
"response" : {
"status" : 400,
"body" : "Incorrect username or password.",
"headers" : {
"Server" : "Kestrel",
"api-supported-versions" : "1",
"Date" : "Mon, 18 Mar 2024 14:36:35 GMT",
"Content-Type" : "text/plain; charset=utf-8"
}
},
"uuid" : "1850cfe7-8e96-4c24-877f-40de1e5d6c32",
"persistent" : *true*,
"insertionIndex" : 6 }
I had manually captured this stub by giving a wrong password in the login. What I expect here is when I give the right login credentials, it should give me 400 status with the above body.
Thats right, I had removed json from the file name and right now my mock file is in mappings and not in __files. Should I be keeping it in __files?
This is how I placed it now.
b
No, that goes in
mappings
.
👍 1
j
So its already in mappings folder now
b
The JSON file is syntactically correct, too, I can see, my WireMock instance reads it just fine.
j
Ohh, okayy
{ "errors" : [ { "code" : 10, "source" : { }, "title" : "Error parsing JSON", "detail" : "Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (String)\"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n<title>Error 403 No valid crumb was included in the request</title>\n</head>\n<body><h2>HTTP ERROR 403 No valid crumb was included in the request</h2>\n<table>\n<tr><th>URI&lt;/th&gt;&lt;td&gt;/ admin/mappings&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;th&gt;STATUS</th><td>403</td></tr>\n<tr><th>MESSAGE:</th><td>No valid crumb was included in the request</td></tr>\n<tr><th>SERVLET:</th><td>Stapler</td></tr>\n</table>\n<hr/><a href=\"https://eclipse.org/jetty\"[truncated 56 chars]; line: 1, column: 2]" } ] }. I ran it again I see this error now 😞
b
You don't need this part, though:
Copy code
stubFor(post(urlMatching("/auth/api/v1/Authentication/login"))
.willReturn(aResponse()
.withBodyFile("auth_api_v1_authentication_login.json.json")
.withStatus(400)));
That's another stub definition, pointing to your stub definition file. What happens if you remove that from the code?
One moment, I'll run a test against that mapping file
j
Sure 🙂
_stubFor_(_post_(_urlMatching_("/auth/api/v1/Authentication/login")) .willReturn(_aResponse_() .withBodyFile("auth_api_v1_authentication_login.json") .withStatus(400)));
I had removed the json from it as well
b
Works like a charm it seems:
❤️ 1
Have you tried removing this entire code snippet?
Copy code
stubFor(post(urlMatching("/auth/api/v1/Authentication/login"))
.willReturn(aResponse()
.withBodyFile("auth_api_v1_authentication_login.json.json")
.withStatus(400)));
You don't need it. Everything is in the JSON mapping file.
j
Yes Bas. I removed the entire snippet but it still gets through the App login though
That is my code now
b
Could you try placing the WireMock folder (
__files
and
mappings
) under
src/test/resources
(the default location) and try again? I don't think it's a good idea to put them in
src/test/java
. It's not source code.
j
Sure
I have changed it to resources and have added the mock json there, but still it gets through.
I tried doing it from postman and it worked though.
Should I be specifiying the port number 2424 when I start the wiremock server?
b
What have you configured as the authentication endpoint to hit in your app?
j
So basically it should be a https call and should be running in port 2424.
b
So, you have set your auth endpoint in your app to be
<https://something.xyz:2424/auth/api/v1/Authentication/login>
? If that's the case then yes, your WireMock server should be running on port 2424, of course. And you should enable HTTPS: https://wiremock.org/docs/https/
j
I was just running the wiremock from terminal and I have commented the code to start from the code. I have also added httpsport(2424) in the test code as well
I shall confirm with the dev whether they have set the endpoint in 2424
1
The https port(2424) addition in the code looks correct right?
b
I've never used WireMock over HTTPS tbh
👍 1
But yeah, that should do the trick according to the docs
❤️ 1
I'm off for today, happy to see what else I can do tomorrow
j
Thank you so much for your time Bas.
I will work for a progress in this as well :-
🙂