How to automate application based on command line? - shell

I have a Java based application which I run using command prompt. I am currently testing it manually. I want to automate the process. What tool or scripting should I use?
For example:
First I will run the java command to run the .java file.
My application will give me 5 options. For example
i. Add
ii. Subtract
iii. Multiply
iv. Divide.
It should select one of the options and add the number and verify the result with the expected result. All this will be done using command prompt.

I realize that you're asking for a command prompt solution, but you really should look into JUnit. It will be a much more maintainable solution for a larger variety of test conditions. If you use the command line, you'll have to parse all of the input and output. In order to truly test your code, you shouldn't have to go through the intermediate of the command line (stdin, stdout); you should have Java code that runs your Java code, since it can access the variables.
If you really are set on using the command line, you would have to give us more details on how your program runs. This is just a shot in the dark:
inputs.txt (These are the inputs that will be fed to your program)
Add
4
6
test.bat
#ECHO OFF
:: I just assume that your program prompts for input and waits for stdin
java YourMainClass < inputs.txt > testOutput.txt
:: Program prompts for an operation; "Add" is read from inputs.txt
:: Program prompts for values; "4" and "6" are read from inputs.txt
:: Program prints out the result, which is redirected to testOutput.txt
:: Now you have to read in the testOutput.txt file and look at the last line
FOR /F %%i IN (testOutput.txt) DO SET RESULT=%%i
:: The variable RESULT now contains your program's answer
:: Create a variable to compare your actual result with the expected result
SET EXPECTED=10
IF %RESULT% == %EXPECTED% ECHO "You were correct"
IF NOT %RESULT% == %EXPECTED% ECHO "You were not correct"

You need to use a batch file. Put your commands in a notepad file and save it as .bat.
If they are not native batch commands though you may need to change some things, but you would need to post you commands before I can confirm.

Standard input can be fed into java application. If you're using bash it can be done like the following:
java myapp.java < test1
Where myapp.java is your application with main-method, and test1 being your test input (a plain old textfile).

Related

Windows batch file - automatic answer to all prompts

I am running a program that generates a batch file. The batch file has a bunch of prompts that I want to answer with RETURN:
Example:
Type X to quit or <RETURN> to proceed
I have tried:
echo. | batchfile.bat
This only answers the first prompt. I want to answer all prompts, and there are a lot of prompts.
You have 3 ways to do it.
1. As #MarcB mentioned in the comment you can create a file with as much returns as you need, then use the type file.txt| batchfile.bat command
2. Similar like the 1st one, but the command is: batchfile.bat < file.txt
3. Chaining commands (answer with 3 returns): (echo.&echo.&echo.) | batchfile.bat normally, this also can be formatted like this:
(
echo.
echo.
echo.
) | batchfile.bat
Also if your answer (in your case not) contains some special character which is an operation like & | > < you will need to escape them with ^ sign. Like (echo Stan ^& Pan&echo old series) result will be in this case:
Stan & Pan
old series
Side note: Also I don't know what was your usage, but if I had access to modify that batch file, than I would add a new option (like y) and if that option is present, I don't ask anything in the batch file and I answer the question inside the batch file (if there's other programs which asks something). This will make a bit easier the script usage (like: batchfile.bat y).

Stata command line arguments in batch mode

A helpful FAQ from Stata describes that arguments can be passed to do files. My do file looks like this:
* program.do : Program to fetch information from main dataset
args inname outname
save `outname', emptyok // file to hold results
insheet using `inname', comma clear names case
// a bunch of processing
save `outname', replace
According to the FAQ, this script can be run using do filename.csv result.dta. When I run this command from within Stata, everything works fine. The program is long, however, so I want to run it in batch mode. Stata has another FAQ about batch mode.
Combining the information from these webpages, I type the following at my Unix prompt:
$ nohup stata -b do program.do filename.csv result.dta &
Stata starts up, but it terminates with the following error:
. save `outname', emptyok // file to hold results
invalid file specification
r(198);
A little experimentation tells me that Stata is never receiving the two arguments when I run the program in batch mode. What is the solution to this problem? (i.e. how do you pass arguments to a do file when running it in batch mode?)
The thread below may be helpful:
http://www.stata.com/statalist/archive/2012-09/msg00609.html
In Windows, if my program Test.do is:
args a b
display "`a'"
display "`b'"
I can run it in batch mode in Windows by simply typing:
"c:\Stata13\stata.exe" /e do "c:\Scripts\Test.do" Test Script
And it will display (within Stata):
Test
Script
So I wonder whether the nohup is what's preventing your program from working.

Use parameter as intermediate input in Batch File

In a batch file, I am running exe which takes input from user. I want to hardcode value and continue process. check following example:
In Bat File(GetData.bat):
set /p UserInput = Enter a number?
%1
To call bat file:
GetData 5
but it's waiting for input instead of setting 5.
Note: It's just for example actually i am calling exe which takes input in process. I can't modify exe.
Command line parameters are not the same thing as standard input (stdin). There are some programs that can accept input both as command arugments or via stdin, but that is not the norm.
To get your batch script to work, you must ECHO the data to stdout, and then pipe the results to GetData.bat
echo 5|GetData
If you must provide multiple lines of input, then you can do something like
(
echo line1
echo line2
echo line3 etc.
) | yourProgram.exe
Or you can put the input in a text file and then pipe the contents of the file to your command.
type commands.txt | yourProgram.exe
Or you can redirect the input to your file
<commands.txt yourProgram
Note that some programs read the keyboard directly, and or flush the input buffer before prompting for input. You will not be able to pipe input into such a program.
Update - exe not using piped input
Most database command line programs that I have seen have an option to specify the username and password as command line arguments. You should read the documentation, or google your exe for command line arguments.
If your exe does not have an option to specify the values on the command line, then your only option is to use a 3rd party tool like AutoHotKey that can be scripted to pass keystrokes to a running program.
Look at this example:
echo Enter a number?
set UserInput=%1
echo %UserInput%
Then if you type "GetData 5", the output will look like this:
Enter a number?
--empty line to execute "set"
5

Is there any way to trace through the execution of a batch file?

I inherited some large batch files, and I'd like to rewrite them to a more "developer friendly" language.
I'd like to find out the following things:
what other scripts it calls
what other processes it starts
what files does it write to
what environment variables it uses, which ones does it set
For the last point, I'm aware I can do this before I start:
set > original_environment.txt
And after I run it, I can do this:
set > new_environment.txt
and just do a diff between them ... but I'll probably miss some variables that may be unset when the script finishes ( or even all of them if the script's ran under setlocal ).
Is there any way of finding all those things without me adding tons of echo statements throughout the script code?
Is there such a tool that can monitor the process started by the batch file and tell me everything it did?
You can just look inside them and figure out what they do.
You can also remove any echo off statements and # preceding commands; that way every command is output before it's run and you can redirect the output to a file to study it later.
There is no debugging tool for batch files that I am aware of—but I contemplated writing one once.
There is no direct way to do it. But it's not impossible to create one.
Since Windows XP/Vista/7 came out the good'ole set of DOS batch commands has been greatly upgraded, although not many uses them or even RTFM (FOR /??)
So here I give you, a simple pure-batch TRACER that utilizes the FOR /F line-parsing switch:
#ECHO OFF
FOR /F "delims=" %%L IN (%1) DO (
CLS
ECHO __________________________________________________
ECHO ENV. VARIABLES *BEFORE*
SET
ECHO __________________________________________________
ECHO LINE
ECHO %%L
ECHO __________________________________________________
ECHO Hit any key to execute the line ...
PAUSE > NUL
ECHO __________________________________________________
ECHO EXECUTE
%%L
ECHO __________________________________________________
ECHO Hit any key to fetch the next line...
PAUSE > NUL
)
ECHO END OF FILE
You can take it as a start and modify it as you go.
Here's how you'd use it:
DEBUG.BAT TEST.BAT
And I'll also give you a test file to try it out:
#ECHO OFF
ECHO Hello World!
SET aaa=1
SET bbb=2
ECHO Doing step 2
SET aaa=
SET ccc=3
ECHO Doing step 3
SET bbb=
SET ccc=
ECHO Finished!
This DEBUG.BAT thing, however, due of its simplicity, has some limitations BUT which can be worked around if you slap enough BATCH-fu in there.
It cannot process multi-line blocks :: This can be worked around by having the FOR commands parse tokens and build the lines as they come in, and IF it encountered an open parenthesis, just dump the parenthesis block content to a temporary file and then call itself on the tempfile e.g. DEBUG tempfile.bat
It cannot process jumps :: You can of course, do an IF check for a GOTO label then do a FOR /F to parse out label itself, then maybe utilize the second argument %2of DEBUG.BAT to specify the label to jump to, in which case if this very argument is specified you'd just spin the FOR /F until the desired label came into view and then proceeds with normal debugging on the next line.
There is too much information from a single SET :: Just do what you did with the SET > before.txt and after thing but do it on each line and then run a cmd-line DIFF tools on the files (plenty are available on the net). Then you'll get a DIFF of each variable that has changed since last step. You might even be able to avoid the env. variables mess altogether by slapping in SETLOCAL and ENDLOCAL in there and then you'd get just the local SETs ... but YMMV.
Those are some. If you've found some show-stopping limitation or whatever enhancements would help you nail that last bug, feel free to just let me know (via the comments) and I'll try to help you if I can.
Hope this helps.
No, there is no way to debug old style batch files. You can, however, just get rid of all the ECHO OFF statements, and then all the commands and output will be echo'd to the console when you run them.
If you are willing to spend some money you should take a look at the Running Steps batch file IDE and its debugging capabilities.
I didn't test it, but it has some features that might help you in your task:
...
Visual Studio-like debugging environment.
Rich set of debugging commands (step into, step over, step out, and more)
Rich Project analyzer to find your errors and warnings in no time.
Integrated support for delayed-expanded environment
variables.
Multi-type breakpoint definitions to fit your multiple debugging needs.
Complex pipeline and redirection support with multi-color highlighting.
Environment variable visualization and modification support.
Expanded information window for true variable definition visualization.
Impressive 'For command' unrolling feature.
Interactive callstack and Parameters window.
...
They also offer a trial version.

Send commands to other command-line programs

Is there a way, to send commands to another command-line program?
'Cause i have a special command-line program, but I can't send commands to it using syntax like program.exe something_to_do
the program executes something like this: ("here syntax" is where i want to input text to and also enter to start)
TheWhateverCommandLineProgram
Version 1.1
Give an option: "here syntax"
the program in code looks something like this:
echo TheWhateverCommandLineProgram
echo Version 1.1
Set opt=
set /p opt=Give an option:
if %opt%==command1 goto com1
if %opt%==command2 goto com2
...
Well, i guess so cause it wasnt me who made it (btw: off course its not called TheWhateverCommandLineProgram)
If you just want to give keyboard input to a commandline program you can just use echo and pipe it:
echo some text | program.exe
If you need more lines, then write them to a file and use input redirection:
echo one line > file
echo second line >> file
program.exe < file
I'm not 100% sure I understand what you're looking for. Here's two options:
You have two windows, each running a batch program. Let's say they are called myscript1.bat and myscript2.bat. You want to send a set of commands from myscript1.bat to be executed by myscript2.bat
You have a single batch script named myscript.bat, which executes a single program named program.exe. You want program.exe to execute some commands, or do some something.
Are either of these what you're looking for? Here's some idea:
Make myscript1.bat create a third file, mycommands.bat. Once myscript2.bat sees the file mycommands.bat exists, it will execute it and delete it. (Wow. Lame.)
Use Windows Scripting Host command (it's built in to Windows since Win2K) or Powershell (usually on most computers nowadays, if they have been updated). Either of these can send keystrokes to another program. Using those keystrokes, you can control the other program.
In what form does the other program take input? From the command prompt?
If the latter then I recommend Autohotkey: http://www.autohotkey.com/
You can use Autohotkey as a bridge and it will send the command as keypresses to the window of the other batch file.
You can ask for help in their forum. They are quite helpful.

Resources