I am using sessions in gatling to store values, as shown below
exec(session => {
val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
session.set("STARTED_PROCESS_ID",id)
//Store the id somewhere for processing later
session
})
.exec(
http("scenario")
.post(url)
.header("Content-Type", "application/json")
.header("id", session => session("STARTED_PROCESS_ID").as[String])
.body(StringBody(body)
.check(status.is(200))
According to the documentation, the value should be stored in session & the header "id" should be populated as expected. But when running the simuation I get the following error
java.util.NoSuchElementException: No attribute named 'STARTED_PROCESS_ID' is defined
at io.gatling.core.session.SessionAttribute.as(Session.scala:46)
at common.HttpUtil$.$anonfun$sendPostRequestForWasStartDefLoad$1(HttpUtil.scala:557)
at io.gatling.core.action.SessionHook.execute(SessionHook.scala:32)
at io.gatling.core.action.Action.$bang(Action.scala:38)
at io.gatling.core.action.Action.$bang$(Action.scala:38)
Can someone please help explain why is this happening ?
You're not using the Session API correctly. Please properly read the documentation.
Session is immutable and set returns a new instance.
exec { session =>
val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
session.set("STARTED_PROCESS_ID",id)
}
gatling sessions are immutable, so where you return session as the final line of your session function, you're actually returning the initial, unedited session.
session.set returns a new, updated session, so you can just leave that as the last line of the session function and it should work.
Related
I have been trying to check if a certain user ID exists in the current guild,
I have been looking at countless stack overflow questions and documentations hoping i could find the answer but nothing I try actually works.
i use Discord.js V13 with the latest node.js version
const server = client.guilds.fetch( message.guild.id );
if ( !server.member(args[0]) ) return commandFailed('a user with this ID does not exist');
// args[0] contains the userID
could someone please help me with this.
First: fetch returns a promise, you must handle that promise. However, you don't need to fetch the server since the server is already accessible through message.guild.
Second: Guild#member() is deprecated, try fetching the member directly and checking if a member returned.
I'll be using Async/Await for this example, make sure you're inside an Async function.
// const server = await client.guilds.fetch(message.guild.id); is not needed
const server = message.guild;
const member = await server.members.fetch(args[0]);
if (!member) return commandFailed('a user with this ID does not exist');
Take a look at this thread, which explains exactly what you want:
let guild = client.guilds.get('guild ID here'),
USER_ID = '123123123';
if (guild.member(USER_ID)) {
// there is a GuildMember with that ID
}
I have extracted token value from the login api call.but i am not able to use that variable value into next api call.My code is given below
val scn = scenario("test login")
.exec(http("login call")
.post("https://api.k6.io/v3/account/login")
.headers(headers_1)
.body(RawFileBody("data/login.json"))
.check(status.is(200))
.check(jsonPath("$.token.key").saveAs("tokenId")))
.pause(21)
.exec(http("edit profile call")
.post("https://api.k6.io/v3/users/3187878")
.headers(headers_1)
.header("Authorization", "Token ${tokenId}")
.body(RawFileBody("data/editprofile.json"))
.check(status.is(200)))
but the call get failed. I am not getting the value of that variable in the second call.
{"key":"4c713e3f5d362f0002f6eef737401e249c154bed"}
i need to use the value of 'key' in the header for next api call as in the format of
.header("Authorization", "Token 4c713e3f5d362f0002f6eef737401e249c154bed")
but it is not getting in this format.where i am going wrong?Can anyone help me.Thanks in advance
Your way of capturing data with a check and re-injecting it with Gatling Expression Language is correct.
Possible reasons your scenario doesn't work:
the "login call" request fails and is not able to capture the data
you use RawFileBody but maybe some data in there needs to be dynamic, just like your Authorization header
If you want to check what's been captured, you can add an extra action before your pause:
.exec { session =>
println(session("tokenId").as[String])
session
}
I`ve used method getCookieValue() for get cookie value from session, it returns an object io.gatling.http.action.cookie.GetCookieValueBuilder#2012cf26
.exec{session => {
val cookie = getCookieValue(CookieKey("CookieKey"))
println("Session cookie is :::::::::::::::::::::::::" + cookie.toString)
session
}}
should get cookie value instead of io.gatling.http.action.cookie.GetCookieValueBuilder#2012cf26
looking at the documentation, getCookieValue is a DSL action that (as you discovered) returns a builder, so you would need to execute it in an 'exec' block - it's designed to get a named cookie and put it into the session.
so if you did
.exec(getCookieValue(CookieKey("CookieKey")))
the value of the cookie named "CookieKey" would be put in the session under the key "CookieKey".
You can't call DSL methods inside a session function - the returned builder won't ever be called
I have some problem with CGI::Session.
I try to create a new session with an existing session id passed with the cgi object. Normally the session should reuse the existing session in the database, but it doesn't. Instead it creates a new session database entry with the exact same session id.
Here are the relevant parts of my code:
CGI::Session->name("DCGISESSID");
$session = CGI::Session->new('driver:mysql', $cgi,
{
TableName=>'DSESSIONS',
IdColName=>'id',
DataColName=>'a_session',
Handle=>$dbh,
});
$sessioncookie = CGI::Cookie->new(-name=>'DCGISESSID', -value=>$session->id, -expires=>'+1h', -path=>'/');
The code works as long as I do not set the cookie name with the name() method and use the default value CGISESSID as cookiename. But for some reason, after changing it to DCGISESSID with CGI::Session->name("DCGISESSID"); it doesn't work.
Does someone got the same problem or has any advice for me?
Solved the problem. I configured the table false, that's why id wasn't a primary key too.
I have a requirement, where I will be getting the Session Id and Server URL(parameters of a webservice method).
Say like this:
Session ID : 00D900000xxxxxx!ARUAQOb4VVoQR1UXlY_Hvuy1DdKdN6nSfnNJKYwPTF9R3tYuA2jzBsWXHIGDQUFL13iebnYSDKKC45H98TzVxxxxxxxxxx
Server URL :
https://ap1.salesforce.com/services/Soap/u/12.0/00D900000xxxxxx
Now i need to get the User Id(or any other user details) from these two.
Thanks in Advance!!
Nitin
You could use the SOAP API, which has a GetUserInfo method. Calling this method will return a GetUserInfoResult object, which will contain the user ID associated to the session.
Adam, GetUserInfo method requires existing connection object which we can create by calling method, so I applied the following code and now everything works fine!
ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint("https://login.salesforce.com/services/Soap/c/24.0/");
config.setServiceEndpoint("https://na14.salesforce.com/services/Soap/c/24.0/00DXXXXXXXXXXXX");
config.setSessionId("00DXXXXXXXXXXXX!1AQ4AQO980Fmu25SOFQxxOlQN8zAaHOlnfdk._rZU2Vkf_CV0HJREqKavMLaPg9jtA9N517MNHLdLeF.aVkoZtnk2eu7u.XNn");
connection = new EnterpriseConnection(config); GetUserInfoResult
userInfo = connection.getUserInfo();
I used the same server URL and sessionId that I have received from query parameter string.