Inteliij IDEA : Gatling getting j.l.NullPointerException while running simple script - scala

While I as executing below scala code as Gatling script from Intellij IDEA Ultimate
import java.net.URI
import java.util
import java.util.{Collections, Optional}
import com.tools.RequestSigner
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class CheckResponseCode extends Simulation {
val httpConf = http.baseUrl("https://localhost:8080/")
.header("Accept", "application/json")
val jsonFileFeeder = jsonFile("data\\request.json")
val scn = scenario("get data")
.feed(jsonFileFeeder)
.exec(http("Get data")
.put("/test1")
.sign(new com.tools.RequestSigner()
.check(jsonPath("$..operation").saveAs("operationId"))
.check(status.is(202))
.check( bodyString.saveAs( "RESPONSE_DATA" ) ))
.exec( session => {
println("Some Restful Service Response Body:")
println(session("RESPONSE_DATA").as[String])
session
})
setUp(
scn.inject(atOnceUsers(1))
).protocols(httpConf)
}
I get to see below in the result :
---- Errors --------------------------------------------------------------------
> j.l.NullPointerException 1 (100.0%)
What can be reason behind these and where can I find th detailed stack trace ?

In whatever logback appender is configured in your logback.xml configuration file, typically in the console.

Related

Gatling exec with session

I need to make a request in Gatling, in which I'm able to access session items (without the expression language). I need to do this, because I want to inject data into a ByteArrayBody request from a csv feeder. To demonstrate my problem, I have a small example (without the actual need of the session).
The following scenario runs fine:
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(http("Http Test test").get("http://google.de/"))
}
But that one doesn't (I get the exception There were no requests sent during the simulation, reports won't be generated):
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(session => {
http("Http Test test").get("http://google.de/")
session
})
}
I run my simulations in IntelliJ (which worked fine so far) and in the following (here minimized) simulation file:
package test.scala
import java.text.SimpleDateFormat
import java.util.Date
import io.gatling.core.Predef._
import io.gatling.core.body.ByteArrayBody
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import org.slf4j.LoggerFactory
import test.scala.TerminalTesterRequest.url
import test.scala.requests._
import test.scala.util.CharsetConverter
import scala.concurrent.duration._
import scala.language.postfixOps
class MySimulation extends Simulation {
//base URL (actually this URL is different, but it's not important)
val ecmsServerUri = "http://0.0.0.0"
//base Protocol
val httpProtocol: HttpProtocolBuilder = http
.baseUrl(ecmsServerUri)
.inferHtmlResources(BlackList(""".*\.js""", """.*\.css""", """.*\.gif""", """.*\.jpeg""", """.*\.jpg""", """.*\.ico""", """.*\.woff""", """.*\.(t|o)tf""", """.*\.png"""), WhiteList())
.acceptHeader("*/*")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en,en-US;q=0.7,de-DE;q=0.3")
.userAgentHeader("Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8762)")
val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
exec(session => {
http("Http Test test").get("http://google.de/")
session
})
}
setUp(
scnBase.inject(constantUsersPerSec(1) during(1 seconds)).protocols(httpProtocol)
).maxDuration(5 minutes)
}
How can I run an exec request with the information of the session (or at least the data from the feeder)? I'm using Gatling 3.1.1
Build whatever you need in a function and put the result in the session, then refer that value in the actual request
val feeder = csv("foo.csv")
scenario("Test scenario")
.feed(feeder)
.exec(buildPostData)
.exec(http("Http Test test")
.post(createApiURL)
.body(ByteArrayBody("${postData}"))
.check(status.is(200))
)
def buildPostData: Expression[Session] = session => {
val postData: Array[Byte] =
... // getting values from csv record: session("csvHeader").as[String]
session.set("postData", postData)
}

How to use Pause in gatling?

I want to use normalPausesWithPercentageDuration where do I declare it and use it in my gatling script. Any examples how to use it will be more helpful?
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class temp extends Simulation {
val httpProtocol = http
.baseURL(testServerUrl)
val scn = scenario("Test data")
.feed(csvFeeder)
.exec(http("My Request")
.get("""/api/xyz/${ID}""")
)
.normalPausesWithPercentageDuration(2) // This is not working
setUp(scn.inject(constantUsersPerSec(userCount) during(duration minutes))).protocols(httpProtocol)
}
Thank you :-)
This is how to pause ,some other useful code as well
exec( http("Poll")
.get(Configuration.URL)
.queryParam("requestor", "TEST")
.check(status.is(200))
.check(status.saveAs("statusCode"))
.check(xpath("//status").saveAs("responseStatus"))
)
.pause(Configuration.THINK_TIME second)

Authentication Issue & Check Regex Issue Using Gatling

Want to Perform Load testing on Salesforce Platform. But it seems there is an error in authentication as well as regex part.
I am using Gatling testing tool & scala programming here.
If you guide me how to do load testing on salesforce that would be plus.
This is my code:
package default
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.core.Predef._
class requestUser extends Simulation {
val httpProtocol = http.baseURL("http://login.salesforce.com")
val Test = exec(http("Login")
.get("/")
.basicAuth("abc#gmail.com", "Password+Token")
.formParam("sessionId","*******")
.formParam("orgId","***********")
)
.exec(http("acc")
.get("/path")
.basicAuth("abc#gmail.com", "Password+Token")
.check(status.is(200))
**.check(css("#content"))
.check(regex("""<div id ="header">Choose a Username</div>"""))
.check(regex("""<td class = "datacell">Jaipur</td>"""))**
)
.exec(http("home")
.post("/home/home.jsp")
.formParam("Post", "Test from Gatling")
.check(regex())
)
var scn = scenario("scn").exec(Test)
setUp(
scn.inject(rampUsers(1) over (10 seconds))
).protocols(httpProtocol)
}

Gatling Get web service

I have tried to create a simple Gatling Script mentioned below,
package computerdatabase.advanced
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.util.matching.Regex
import scala.concurrent.duration._
class getSampleTest extends Simulation{
val httpProtocol = http
.baseURL("https://xyz.com")
.header("Content-Type","application/json")
.header("Accept"," application/json ")
.header("Accept-Charset","utf-8n")
.acceptLanguageHeader("en-us","en;q=0.5")
.acceptEncodingHeader("gzip", "deflate")
.connection("keep-alive")
val scn = scenario("XYZ")
.group("XYZ Group") {
exec(http("XYZ-PAge").get("/profile/services").check(status.is(200)))
}
setUp(scn.inject(
rampUsersPerSec(1) to(10) during(5),
constantUsersPerSec(10) during(5)
).protocols(httpProtocol))
}
but i am getting an error saying that -->
value header is not a member of io.gatling.http.config.httpProtocolBuilder
may be a semicolon is missing before'value header'
.header("Content-Type","application/json")
No, this is not the compiler error message you get with such code (this is the error you got with the first tentative you posted on the Gatling mailing list).
Here, you get "too many arguments for method acceptLanguageHeader" (and acceptEncodingHeader) as those take only one parameter:
.acceptLanguageHeader("en-us, en;q=0.5")
.acceptEncodingHeader("gzip, deflate")

scala code can be ran in command line but eclipse

import com.twitter.finagle.{Http, Service}
import com.twitter.util.{Await, Future}
import java.net.InetSocketAddress
import org.jboss.netty.handler.codec.http._
object Client extends App {
//#builder
val client: Service[HttpRequest, HttpResponse] =
Http.newService("www.google.com:80")
//#builder
//#dispatch
val request = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, "/")
val response: Future[HttpResponse] = client(request)
//#dispatch
//#callback
response onSuccess { resp: HttpResponse =>
println("GET success: " + resp)
}
Await.ready(response)
//#callback
}
I have this code
when I run it from command line, it works
./sbt 'run-main Proxy'
But when I run it from scala ide(eclipse)
it gave me
Error: Could not find or load main class Server
why is that? how can I fix this?
thanks