How to create a random string everytime a test runs in karate dsl - dsl

The json request I am sending is:
Given url applicationURL
And path 'applications'
And header Authorization = subscribeToken
And request:
{
"throttlingTier": "Unlimited",
"description": "sample app description",
"name": "TestbyKarate",
"callbackUrl": "https:/apistore-dev-dev-a878-14-ams10-nonp.qcpaws.qantas.com.au/callback"
}
When method post
Then status 201
* def applicationId = response.applicationId
* print 'applicationId is ', applicationId
I am sending the name in my request as TestbyKarate but I want to send a unique value every time my test runs.
Is there any way to do it?

Can you please read the docs once. It will really benefit you.
https://github.com/intuit/karate#commonly-needed-utilities
So in the Background or common feature, you have:
* def now = function(){ return java.lang.System.currentTimeMillis() }
Then you can do this:
* def name = 'Test-' + now()
* request { name: '#(name)' }

check below example
Feature: Create User Name
Scenario: UserName with unique Name
def getDate =
"""
function(){ return java.lang.System.nanoTime() }
"""
def time = getDate()
def userName = 'createUser' + time + '_Test'

Related

Locust load testing For API calls

I have written a code for locust load testing for my case, where i can do a token call and then will make feature calls as per below code.
This is working fine with single token and 'n' number of users mentioned in master
I made a token call outside class and sending it as parameter to ENV. The userclass is reading the single token and using the same for all users.
I do not want to make token call inside the class, as it generates new token in every execution.
I'm looking if there is anyway like making token calls based on number of users mentioned on master -u and using only those tokens in User class.
Please suggest me if there is any documentation pointer which i can refer for this usecase
#! /usr/bin/python3.6
import json
from locust import HttpUser, task, constant, tag, events
from locust.log import setup_logging
import os
from datetime import datetime
import requests
setup_logging("INFO", None)
#events.init_command_line_parser.add_listener
def init_parser(parser):
parser.add_argument("--resp-delay", type=str, env_var="LOCUST_RESP_DELAY", default="", help="It's working")
parser.add_argument("--resp-size", type=str, env_var="LOCUST_RESP_SIZE", default="", help="It's working")
parser.add_argument("--env-endpoint", type=str, env_var="LOCUST_ENV_ENDPOINT", default="", help="It's working")
#events.init.add_listener
def _(environment, **kw):
os.environ['resp-delay'] = environment.parsed_options.resp_delay
os.environ['resp-size'] = environment.parsed_options.resp_size
os.environ['env-endpoint'] = environment.parsed_options.env_endpoint
with open("resources/data/" + environment.parsed_options.env_endpoint + '/data.json') as f:
data = json.load(f)
cal_transaction_id = datetime.now().strftime('%Y%m%dT%H%M%S')
#retrive cliend id and client secret from bitbucket repo
dict_car_app_all = data["data"]
print("env-endpoint:" + os.environ.get("env-endpoint"))
token_url = "https://ENDPOINT/auth/token"
#retrive cliend id and client secret from bitbucket repo
token_payload = "client_id=" + dict_car_app_all[0]["client_id"] + "&client_secret=" + dict_car_app_all[0]["client_secret"]
token_headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", token_url, data=token_payload, headers=token_headers,
cert=( 'resources/certs/' + environment.parsed_options.env_endpoint + '/MyCERT.pem',
'resources/certs/' + environment.parsed_options.env_endpoint + '/MYCERT.key'))
result = json.loads(response.text)
token = result["access_token"]
os.environ['access_token'] = token
os.environ['cal_transaction_id'] = cal_transaction_id
#class User_1(User):
class User_0(HttpUser):
wait_time = constant(1)
host = "host.com"
#tag('tag1')
#task
def load_test_api_tag1(self):
token_0 = os.environ.get('access_token')
cal_transaction_id = os.environ.get('cal_transaction_id')
env_endpoint = os.environ.get('env-endpoint')
resp_delay = os.environ.get("resp-delay")
resp_size = os.environ.get("resp-size")
feature_headers = {
'Authorization': "Bearer " + str(token_0),
'sm_transactionID': cal_transaction_id
}
url = "https://ENDPOINT/SERVICE/mytestservice/first_test"
querystring = {"response_delay": resp_delay, "data_size": resp_size}
self.client.request("GET", url, headers=feature_headers, params=querystring,
cert = ('resources/certs/' + env_endpoint + '/MyCERT.pem',
'resources/certs/' + env_endpoint + '/MyCERT.key'))
You can generate tokens in User class's on_start method so each user generates a new token when spawning.
class MyUser(User):
def on_start(self):
#generate token here and assign an instance variable like self.token=abc
super().on_start()
there is a drawback to this though, if your user count is more than your token generating service can handle some users will not be able to spawn, the way I do in my tests is if token generating part is not a part of the system I am testing, I generate tokens beforehand and write it in some file or some external db and read them from there.

How to get total time spend in Gitlab?

Is there a way to get the total time spend on all issues that a user have spend with the time tracking /spend slash command?
Time tracking stats with the API gets just a small amount of data: https://docs.gitlab.com/ce/api/issues.html#get-time-tracking-stats
Gitlab CE 9.1.4
As I see it is possible to parse comments from API v3 and calculate total.
For example,
https://gitlab.com/api/v3/projects/:id/issues/:issue_id/notes?private_token=your_token
{
id: 73113225,
body: "added 1h of time spent at 2018-05-15",
attachment: null,
author: {
...
username: "mnvxxx",
},
...
}
More info:
https://docs.gitlab.com/ee/api/notes.html
UPDATE
Currently I has created instrument for calculating spent time by every contributor. I hope it will be helpful:
https://github.com/zubroide/gitpab
Here is a very simple Python script that works with API v4:
import requests
API_KEY = "" # Enter your API key here
BASE_URL = "https://{{enter gitlab url here}}/api/v4/"
item_counter = 0
total_seconds = 0
for i in range(1, 57): # manually set range of issues here. All issues doesn't work well.
issue = requests.get(BASE_URL + 'projects/2/issues/' + str(i) + '/time_stats')
total_seconds += issue.json()['total_time_spent']
item_counter += 1
print("Hours on all issues: %.2f" % float((total_seconds / 60) / 60))
print("Total issues: " + str(item_counter))
I'm posting to this thread because this is the first answer that comes up on Google, and there isn't really any other ready-made solutions to be found.
Building on top of what #josh-harkema has provided, this is a version that lists all closed issues that were assigned to a specific username that have been updated in a given time period (and do not have the label 'paid' set):
import requests
import os
username = os.environ.get('GITLAB_REPORTING_USERNAME')
project_id = os.environ.get('GITLAB_REPORTING_PROJECTID') # in the top of your project page
access_token = os.environ.get('GITLAB_REPORTING_TOKEN') # https://gitlab.com/profile/personal_access_tokens
base_url = "https://gitlab.com/api/v4"
updated_after = "2019-06-01T00:00:00.00Z"
updated_before = "2019-07-01T00:00:00.00Z"
item_counter = 0
total_seconds = 0
headers = { 'Private-Token': access_token }
url_template = "{base_url}/projects/{project_id}/issues?" \
"state=closed&assignee_username={username}&updated_after={updated_after}&updated_before={updated_before}"
url = url_template.format(base_url=base_url, project_id=project_id, username=username,
updated_after=updated_after, updated_before=updated_before)
# call API
issues = requests.get(url, headers = headers)
total_seconds = 0
issues_to_pay = []
line_template = "id: {id} closed: {closed_at} time spent: {time}\ttitle: {title}\turl: {url}"
print("Issue statistics for {u} from {f} to {t}:\n".format(u=username,f=updated_after, t=updated_before))
for issue in issues.json():
time_val = issue['time_stats']['human_total_time_spent']
already_paid = u'paid' in issue['labels'] # you can put a label 'paid' to exclude an issue
if already_paid:
time_val = time_val + " *"
else:
# if the issue has been paid, already, don't add the time, and don't list as to be paid
total_seconds += issue['time_stats']['total_time_spent']
issues_to_pay.append(str(issue['id']))
line = line_template.format(
id=issue['id'],
closed_at=issue['closed_at'],
title=issue['title'],
time=time_val,
url=issue['web_url']
)
print(line)
print("")
print("Hours to pay on all issues: %.2f" % float((float(total_seconds) / 60) / 60))
print("")
print("* = issue has been paid for, already")
print("All issue to pay: {issues}".format(issues=",".join(issues_to_pay)))
Note: You need to have environment variables set for the GITLAB_REPORTING_USERNAME, the GITLAB_REPORTING_PROJECTID, as well as the GITLAB_REPORTING_TOKEN.
We use this to pay contractors. Hope this helps!
I was myself looking for the same and after more searching, I found this excellent CLI tool called gitlab-time-tracker. It generates comprehensive reports of tracked-time that you can customise by multiple options and can print them even as PDFs!
For keeping this answer relevant to OP's question, you can print (in your terminal) total time spent by a user using following command**:
gtt report "namespace/project" --user username --closed --from="2017-03-01" --to="2017-04-01"
** This assumes that you've installed this tool (gtt) and setup your Gitlab PAT (with "api" scope activated) in its config file.
You'll have to use both the Gitlab Commits API and the GraphQL API to achieve it. Below is some code that's been shorted for brevity.
You'll need to specify a Gitlab instance flag and your personal token.
Say you have a function used to capture all users in your Gitlab instance called "GetUsers":
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
address = kingpin.Flag("address", "The Gitlab URL to use").Required().String()
token = kingpin.Flag("token", "Project token").String()
)
func main () {
timeTracker()
}
func timeTracker(git *gitlab.Client) {
names := GetUsers(git)
for _, name := range names {
jsonData := map[string]interface{}{
"query": `
query($user: String!) {
timelogs(username: $user ) {
edges {
node {
id
user {
id
username
}
timeSpent
issue{
labels{
nodes{
title
}
}
}
}
}
}
}
`, "variables": fmt.Sprintf(`{"user":"%s"}`, name),
}
jsonValue, _ := json.Marshal(jsonData)
request, err := http.NewRequest("POST", "https://" +*address + "/api/graphql", bytes.NewBuffer(jsonValue))
if err != nil {
logrus.Error(err)
}
request.Header.Set("Authorization", "Bearer "+*token)
request.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Do(request)
response.Body.Close()
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
}
logrus.Print(ioutil.ReadAll(response.Body))
Result (when decode with JSONDecoder):
INFO[0000] User: user1, Time spent: 300 (s)
INFO[0000] User: user2, Time spent: 120 (s)
You can then take this data and decode it into a struct (I would copy and paste the post request to an autogenerator for sanity's sake), then do what you want with it. Or change the POST request to capture users by Project if you're more interested in something granular.
Source: https://docs.gitlab.com/ee/api/graphql/getting_started.html

Why does HTTPBuilder throw "HttpResponseException: Bad Request"?

Http-builder: 0.7.1 |
Language: Groovy |
Framework: Spock
Testing Code
import groovyx.net.http.HTTPBuilder
import spock.lang.Specification
/**
* Created by Long Nguyen on 4/11/2017.
*
* Chatwork api documentation: http://developer.chatwork.com/ja/index.html
*/
class ChatworkApiSpec extends Specification {
// https://api.chatwork.com/v2/contacts
def apiRoot = "http://api.chatwork.com/v2"
def contactsPath = "/contacts"
def apiToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx"
def http = new HTTPBuilder(apiRoot)
/**
* Endpoint: /contacts
* You can access the list of users who are in contact with you.
*/
def "Get your contact list"() {
when:
def response = http.get(path: contactsPath, headers: ["X-ChatWorkToken": apiToken])
def responseData = response.responseData
then:
println responseData
}
}
It always throws below exception. I don't know what i was wrong with setting headers.
(Of course this api works for me when I use Postman)
Message:
groovyx.net.http.HttpResponseException: Bad Request
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
at groovy.lang.Closure.call(Closure.java:414)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:292)
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:262)
at testcase.ChatworkApiSpec.Get your contact list(ChatworkApiSpec.groovy:24)
Sorry my bad question. I release that my root link also included path in it.
Replace:
def apiRoot = "http://api.chatwork.com/v2"
def contactsPath = "/contacts"
by
def apiRoot = "http://api.chatwork.com"
def contactsPath = "/v2/contacts"
can resolve this problem.

how to write all Soap Request available in Project using Soap UI Groovy

I have a soap project with 4 Test Suite,each Test Suite has some Test Case and each Test case has some test steps[Soap Request,Groovy Script]
I am able to access all the properties using mentioned code below , HOWEVER,code is writing blank request/response file in local system **
def Project = testRunner.testCase.testSuite.project;
for(def i=0;i<Project.testSuiteCount;i++)
{
log.info Project.getTestSuiteAt(i).name
def Suite = Project.getTestSuiteAt(i)
for(def j=0;j<Suite.testCaseCount;j++)
{
log.info Suite.getTestCaseAt(j).name
def TCase = Suite.getTestCaseAt(j)
for(def k=0;k < TCase.testStepCount;k++)
{
def TStep= TCase.getTestStepAt(k)
def req = context.expand('${'+TStep.name+'#Request}')
new File("D:/Directory/"+Suite.name+"_"+TCase.name+"_"+TStep.name+"_"+k+".txt").write( req )
}
}
}
** plz help to solve this, **
Actually there are multiple approaches to achieve this.
Below is the one of the approach.
Here is Groovy Script which writes the requests and responses.
This script assumes that user has already run the test suites so that responses are available to be saved.
Create a new test suite -> test case -> add Groovy Script test step and copy the below script into it.
Groovy Script:
/**
* This groovy script saves the request and response
* And this script will be able to write the responses
* into files if and only if there is response available
**/
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
//Change direcoty path if required
def directoryToSave = 'D:/directory'
//Not required to change the script beyond this point
//date time is appended to the file name
def dt = new Date().format('yyyyMMdd_HHmmss')
//Closure to save the file
def saveToFile(file, content) {
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
log.info "Directory did not exist, created"
}
if (content) {
log.info "Writing the content into file :${file.name}"
file.write(content)
assert file.exists(), "${file.name} not created"
} else {
log.warn "the content is empty, not writing the content into file"
}
}
//Get the project object
def project = context.testCase.testSuite.project
//Loop thru the project and save the request and responses
project.testSuiteList.each { suite ->
suite.testCaseList.each { kase ->
kase.testStepList.each { step ->
if (step instanceof WsdlTestRequestStep) {
def reqFilePath = new File("${directoryToSave}/${suite.name}_${kase.name}_${step.name}_request${dt}.xml")
def resFilePath = new File("${directoryToSave}/${suite.name}_${kase.name}_${step.name}_response${dt}.xml")
saveToFile(reqFilePath, step.testRequest.requestContent)
saveToFile(resFilePath, step.testRequest.responseContent)
saveToFile(step)
} else {
log.info "Ignoring as the step type is not Soap request"
}
}
}
}

addAssertion() not recognizing string value of "Contains" type

I have a groovy script that I've added code to to check if a value exists in an XML response I'm getting within SoapUI. I've been dealing with this for a few days and could use some help.
Here is the code:
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
// read your request template
def requestFile = new File("C:/XMLRequestScript/file.xml");
// parse it as xml
def requestXml = new XmlHolder(requestFile.text)
// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the testStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Template");
// loop to create # of testSteps for each application
for(int i = 1; i < 3; i++) {
// xpath expression to get applicationNumber attribute in root node
def xpathNodeAttr = "/*/#ApplicationNumber";
// get the root node attribute applicationNumber throught an XPATH
int appId = Integer.parseInt(requestXml.getNodeValue(xpathNodeAttr));
// add 1 to appId
appId++;
// set the value again in the attribute
requestXml.setNodeValue(xpathNodeAttr,appId);
// create next testStepName for new Application
def testStepName = "TestStep_ApplicationNumber_" + String.valueOf(appId)
log.info testStepName;
log.info testStepName.getClass().getName()
log.info tc.getClass().getName()
// create a new testStepConfig
def testStepFactory = new RestRequestStepFactory();
def testStepConfig = testStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName )
// add the new testStep to TestCase
def newTestStep = tc.insertTestStep( testStepConfig, -1 )
// set the request
newTestStep.getTestRequest().setRequestContent(requestXml.getXml())
// add Assertions here
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
if (assertion.contains("Champion5")) {
newTestStep.addAssertion("Champion5")
log.info("REST response assertion created into: " + newTestStep)
} else {
log.info("REST response assertion not found in " + newTestStep)
}
if (assertion.contains("Challenger5")) {
newTestStep.addAssertion("Challenger5")
log.info("REST response assertion created into: " + newTestStep)
} else {
log.info("REST response assertion not found in " + newTestStep)
}
// execute the request
newTestStep.run(testRunner, context)
}
In the section above called "// add Assertions here", the line of code that I'm having a problem with is:
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
The error states:
Thu Sep 18 14:27:57 CDT 2014:ERROR:An error occurred [Cannot invoke method addAssertion() on null object], see error log for details
My understanding is that I have to pass an Assertion Type that is contained in the SoapUI GUI and just put it in as a string value in order to check if the values that I'm checking are asserted.
Any suggestions/direction would be appreciated. I have no idea what I'm doing wrong. Thanks.
I think the problem is your are calling the function tc.getTestStepByName(String name) with a wrong name, because the newTestStep.toString() doesn't return the Test step name instead it returns the class name (it really returns 'classname' + '#' + 'Integer.toHexString(hashCode())' if it's not override it, see Object.toString()) .
Anyway what you want is to add an assertion for a test step which is just created so add the assertion directly to it instead to find it by name, use:
def assertion = newTestStep.addAssertion("Contains")
instead of:
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
The second problem you have is that you're using the assertion incorrectly. The assertion object in your case is an instance of SimpleContainsAssertion and contains() method doesn't exist in this class, to set the string to check in contains assert use setToken() method, I think that you need this to add the two assertions:
def assertion = newTestStep.addAssertion("Contains")
// add token to check
assertion.setToken("Champion5")
// change assert name in order to avoid the popup
// when we create the second one.
assertion.setName("CONTAINS 1")
// create a second assertion
assertion = newTestStep.addAssertion("Contains")
assertion.setToken("Challenger5")
assertion.setName("CONTAINS 2")
instead of :
// add Assertions here
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
if (assertion.contains("Champion5")) {
newTestStep.addAssertion("Champion5")
log.info("REST response assertion created into: " + newTestStep)
} else {
log.info("REST response assertion not found in " + newTestStep)
}
if (assertion.contains("Challenger5")) {
newTestStep.addAssertion("Challenger5")
log.info("REST response assertion created into: " + newTestStep)
} else {
log.info("REST response assertion not found in " + newTestStep)
}
This code will result creating this two asserts, one named "CONTAINS 1" and check that response contains the string "Champion5", and a second one named "CONTAINS 2" and check that response contains the string "Challenge5".
Hope this helps,

Resources