How to create global variable in karate? [duplicate] - web-services

This question already has an answer here:
How to set and retrieve the global variable's values in the feature file in karate?
(1 answer)
Closed 1 year ago.
I have configured base URL, and now I have to configure one variable(storied), which will be used in the many of the requests. for running test 2-3 times for different stores I need to manually go to every feature and modify storied.
So how would I declare a global variable and how to bind it to a request?

The same way like you did baseUrl, just add a variable in karate-config.js and it will be available in all tests.
If you set a variable using def it will be available to the rest of the scenario. If you want re-use, use a Background section or look at the documentation for call.
(edit:)
Refer to the documentation please: https://github.com/intuit/karate#configuration
If this is confusing, just spend 5 minutes with anyone who knows JavaScript and make them read the above section. You will get a solution in no time. All the best !

Related

Method calling using variable in object name

I'm trying to increase the ease of use of my project at work. I'm a test engineer, not a programmer. The creator won't give me any source files and said if I had ideas to forward them to him and he will code it. Well, I have a lot of time and I want to learn how my project works under the hood and also save him time. I'll start out by saying I know next to nothing about C++ and honestly don't want to learn an entire language to fix this one issue.
The base functions are load() and start(). There are multiple games on 15 racks.
For instance, if I want to load game3 on rack 2, the current "call" is game3_rack2_.load(); and game3_rack2_.start()
Basically, I want the user to select the rack and game. I'm not super concerned with the game part, but let's say the user inputs rack 6 and this 6 is stored to variable num. Is there a simple way to do game3_rack<num>_.load() and game3_rack<num>_.start() ? I could use if statements, but with the amount of games/racks and multiple calls per game/rack, it seems unfeasible. In another language we have (native to company), the above case works. The variable is evaluated before the statement is executed. I think this is known as reflection? but I'm not entirely sure.
Can I utilize a basic array?
game3[N] {game3_rack0_, game3_rack1_, game3_rack2,...game3_rackN};
then just call the user input?
num = 2; //user wants rack 2
game3[num].load(); game3[num].start(); //loads and starts game3_rack2_
Thanks in advance and apologies for my poor understanding of C++.

How do I access the luaL_loadbuffer name parameter?

I've been using the luaL_loadbuffer for many years to load Lua code from within a C++ program. Suddenly I find I need the script to know its own name. Sure, the script in an anonymous function as far as the Lua context is concerned but the C++ framework around it keeps it in a hashmap with a name, the name of the file from which it was loaded to be precise.
I passed that file name into luaL_loadbuffer when I originally wrote the code but I never actually used it. I now need that name so I can have the script compute metrics about its own execution.
luaL_loadbuffer(LuaContext, code, strlen(code), name)
I now need to use that name from with the Lua context. What's the easiest way to do that?
I'm going to tap the Lua debug function documentation in the meantime while waiting for an answer.
When that code is running, debug.getinfo(1).src will give you name.

How to share data between two Lua scripts

I'm not even sure if my plan for doing things is the best way, so I apologize if this post is a bit vague. Also, I understand that similar questions have been asked before. However, I haven't been able to find anything that pertained to my situation and that made sense to me.
So me and my friends from school are building arcade machine, and I'm planning on putting together the main GUI that allows the user to select different games and load them if they have enough tokens. However, these separate windows will have to share some variables, mainly the number of tokens in the machine. I figured a separate Lua program could store such variable, and also have requests sent to it to perform other functions like opening and closing the different windows. Also, in case it's important to note, we will be using the Love2D engine for the games and be running all this on a Linux machine.
By what I've read, there seems to be some C and C++ code involved in this. I know next to nothing about C or C++, and we're trying to get this project moving along, so if you could include some code in your answer and instruct me on how to use it, that'd would be amazing. I can come back later and learn some C or C++, but right now Lua is my top priority.
My questions:
Is there a better way to accomplish what I'm trying to do?
How should I go about doing this?
Can this be done solely with Lua, or is some C, C++, or any other external programming language/utility/etc. required?
Also, incase anyone brings it up, I have tried using global variables, but I couldn't seem to get two programs/scripts to use the same variable at once.
Again, sorry if I'm being a bit vague.
Thanks in advance!
(this method is a combination of #Puzzlem00n's suggestion and reading the comments, so I shouldn't take much credit from this at all)
Putting multiple games in one lua program!
In main.lua:
require("spaceInvaders") --require all the other Lua files
-- without the ".lua" at the end!
gamestate=spaceInvaders
function love.load()
--all the setup things
end
function love.update()
gamestate.update()
end
function love.draw()
gamestate.draw()
end
function love.keypressed(key)
gamestate.keypressed(key)
end
--and so on for the rest of the love callbacks that you need (such as love.keyreleased())
Then in each game (which is a separate Lua file such as spaceInvaders.lua):
spaceInvaders = {}
function spaceInvaders.draw()
--your draw code
end
function spaceInvaders.update()
--your update code
end
--and so on for every other callback you want to use
What this code does is it gives each game its own set of love functions. When you want to play that game, you should set gamestate to that game's name. The spaceInvaders={} line defines spaceInvaders as a table, which stores each function in place. When you define a variable as an existing variable, you are actually creating a reference to it, for example:
t = {}
table.insert(t,1) --place 1 inside t
table.insert(t,3) --also place 3 inside t
s = t
print(s[1]) --will print 1, the first value of t
t[1]=2
print(s[1]) --will print 2, as it refers to t[1], which has just been changed
Sharing variables! (I worked out this bit!)
NOW, this means you can send variables around the program with another function. If you want to share score between games, you could do the following:
in main.lua:
function love.update()
gamestate.update()
score = gamestate.returnScore() --score will always equal to the current score being returned by the game
end
in a game, such as spaceInvaders.lua:
function spaceInvaders.returnScore()
return score --or whatever the score is stored in
end
This will allow you to get a variable, such as score, from one game to main.lua!
I'm sorry if this is a little confusing but hopefully this is what you're looking for! :)

Qt C++: Save project in file [duplicate]

This question already has answers here:
Qt - How to save a configuration file on multiple platforms
(4 answers)
Closed 9 years ago.
I would like to save data from memory to some file. This includes QFileSystemModel and others which are important for program to create some progress. How to create some export file from where those settings would be read and how to read it?
Create some schema for storing all the important persistent variables for your class/file.
Make a save function that pushes all these variables out to a file.
Make a load function that retrieves all these variables from a file.
If your file has a lot of binary data or a large structure you probably should make your own.
Qt has XML and JSON available if one of those make sense to use. (Tree's or node based information).
http://qt-project.org/doc/qt-4.8/examples-xml.html
http://qt-project.org/doc/qt-4.8/xmlpatterns-filetree.html
http://qt-project.org/doc/qt-5.0/qtcore/json.html
If they are a few settings variables, that are short, such as a string or two, or native Qt objects, like a QRect, a QPoint, a QStringList, etc, you can do it with QSettings.
Also QSettings has a really nifty way of handling defaults, and fall-backs, and using the application name and the organization name. I like the INI-format.
http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html
Hope that helps.

C++/MFC - How to export window contents to Excel? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Excel files and C++
I am using an API called UltimateGrid which allows me to easily create Excel-like grids using C++/MFC. I need a way to export all of the contents of these grids to an actual Excel document. I could not find anything about this in the documentation for this API. Then I got to thinking ... exporting contents to an Excel document is a common requirement in applications. I was thinking about creating a right-click menu option for exporting a grid to Excel. I feel like I am reinventing the wheel as I start to code this. I googled around and saw a lot of common ways of dealing with this when database tables are involved. But that is not the case here. My underlying "datasource" is not a database table, but rather just some business objects internal to the application. Am I going to have to just write my own custom handler that iterates through the business objects corresponding to the rows and output them to a CSV file to be used by Excel?
I'm not sure how automated you want this to be. I did this a while back (as in years ago), so forgive me if this info is out of date. Here's what I remember:
CSV is fine, as long as you don't want to export formats, graphs, etc...
You should be able to access the Excel object from your C++ code, and Microsoft should have documentation on how to do this.
It may be easier to have your C++ generate a VBScript to access the component, since last I checked, there was much less red tape to doing it that way, but you could run into permissions/security issues with this method.
Also, have you checked to see if UltimateGrid supports other export formats that are compatible with Excel?
If there are no other options, I think you might want to look into DDE (Dynamic Data Exchange).
There could be a limitation on that OS should be Microsoft Windows, however you might be able to find some open source/runtime that supports other OSs as well.
One that I used long ago was NDde.