Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

How can I automate these CMD commands, so they can be ran together

I am writing automated ui tests for an Android application and after each change in the test code, I have to do the following:

e:
cd adt/sdk/tools/android create uitest-project -n TestAutomation -t 16 -p c:/Users/John/workspace/TestAutomation
c:
cd ../..
cd: Users/John/workspace/TestAutomation
ant build
adb push c:/Users/John/workspace/TestAutomation/bin/TestAutomation.jar /data/local/tmp/
adb shell uiautomator runtest TestAutomation.jar -c com.uia.example.my.TestApp

Is there a way I can configure those scripts to run by themselves together? Its a lot of typing.

Upvotes: 0

Views: 63

Answers (1)

Magoo
Magoo

Reputation: 80203

@ECHO OFF
SETLOCAL
e:
cd \adt\sdk\tools\android create uitest-project -n TestAutomation -t 16 -p c:\Users\John\workspace\TestAutomation
c:
CD \Users\John\workspace\TestAutomation
CALL ant build
CALL adb push c:\Users\John\workspace\TestAutomation\bin\TestAutomation.jar \data\local\tmp\
CALL adb shell uiautomator runtest TestAutomation.jar -c com.uia.example.my.TestApp
GOTO :EOF

You should be able to simply save this file as whatever.bat and then simply execute whatever. Use an editor, not a word-processor.

Note that path-separators are \, not /

I've no idea what adb is - the call should execute it regardless of whether it is a batch or executable.


@ECHO OFF
SETLOCAL
set "instance=%~1"
if not defined instance set /p "instance=What instance ? "
if not defined instance echo no instance set&goto :eof
e:
cd \adt\sdk\tools\android create uitest-project -n %instance% -t 16 -p c:\Users\John\workspace\%instance%
c:
CD \Users\John\workspace\%instance%
CALL ant build
CALL adb push c:\Users\John\workspace\%instance%\bin\%instance%.jar \data\local\tmp\
CALL adb shell uiautomator runtest %instance%.jar -c com.uia.example.my.TestApp
GOTO :EOF

In this example, every mention of TestAutomation has been replaced by %instance%.

By running *thisbatch* TestAutomation then instance will be set to the value TestAutomation and substituted throughout.

By running *thisbatch* then instance will be set to nothing by the first set command (since there is no first parameter, %1 - the added tilde means "and strip out any enclosing quotes") so it will be "not defined" and the set/p instruction allows the value to be input from the keyboard. If nothing is entered, then the procedure reports that and terminates.

Beyond that, follow the bouncing ball. %~2 would provide the second parameter to anothe variable if required for instance. Variables should start with a letter and contain alphanumerics+ a few special characters _#@$ and others - just don't get too clever. %var% accesses the value set. If you want spaces in a pathname, then "enclose the entire pathname in quotes"

Upvotes: 1

Related Questions