Selenium Java class file Execution from Command Prompt

1,243 views
Skip to first unread message

Raju

unread,
Mar 26, 2012, 2:11:26 PM3/26/12
to Selenium Users
Hello,

Has anyone tried executing selenium java scripts (Coded in Eclipse
editor) class file through command prompt ? If Yes, please let me know
the steps to be followed for the same, When I am trying the same it is
giving the issues with IMPORTS.

Also, has anyone tried creating BAT files to execute selenium class
file and schedule the same BAT file through the WINDOWS SCHEDULER for
execution ? If YES please share your experience and difficulties faced
also steps to be followed to achieve this.


Thanks
Raju

Jayaraman - Inspired to become Software Architect

unread,
Mar 27, 2012, 12:18:59 AM3/27/12
to seleniu...@googlegroups.com
It is good to use the build tool to achieve this. Either ANT or MAVEN.

-Jay!!




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Mike Riley

unread,
Mar 27, 2012, 12:10:54 PM3/27/12
to Selenium Users
Yes I have done that and which editor you use doesn't matter.

Using ANT or Maven, as Jayaraman said, would still mean you need
something to fire it off. I prefer not to use either because it is
just one more layer to configure where things could go wrong and I
would need to do it for each program. With the batch files I simply
copy the section where I invoked one program and change the name to
add a new program to the mix. Nice and easy.

I am using the TestNG framework programmatically to run my tests:
http://testng.org/doc/documentation-main.html#running-testng-programmatically

This means I can take command line arguments to modify the task
behavior. My main() method sets up everything, including the TestNG
class instance and parsing the command line options and then tells
TestNG to start running the tests. I use Windows Task Manager to run
them starting after midnight every night and it starts up the
appropriate batch files. It also starts up the servers (Grid and
Node) on the various VMs just prior to running the tests.

I restart the servers each night and again in the morning, because I
have found that just leaving them up they will fail due to memory
leaks (which are better, but not 100% resolved). So having a fresh
server to run the tests on is worth it.

You problem with your imports is likely an issue with your CLASSPATH.
Something to note about CLASSPATH: If you are using a JAR file it is
ignored! The Manifest file in the JAR file supersedes any CLASSPATH
setting. If you are executing an actual .class file then CLASSPATH
will be used.

I am using Netbeans, which sets the Manifest file to expect all JAR
files the code depends on to be in a lib directory under the current
directory by default. It turns out that Selenium, which puts
everything in a libs directory, will work fine if I rename that to lib
(at least when using my Manisfest file from Netbeans - YMMV with
Eclipse). So I put the selenium-server-standalone.jar file in a
directory where I also put all my test program jar files. I then have
a lib directory underneath that where I put my own jar files and those
from the Selenium release, including selenium-java.jar (very
important, since it is a dependency, too). I then have my BAT file CD
to that directory and run the programs from there.

Note that I remove any version number from the selenium-server-
standalone and selenium-java JAR files. This allows my batch files to
execute them without having to know a specific version number,
allowing me to upgrade or downgrade at any time. I do keep the
CHANGELOG file in there so I know which version is in there though. I
also put the chromedriver.exe file in there and have my PATH
environment variable set to find it.

I hope that help you.

Mike

Raju

unread,
Mar 27, 2012, 1:03:26 PM3/27/12
to Selenium Users
Thanks a lot for your detailed explanation.

This is what i thought till yesterday.

Option 1 - Install ANT ( I did successfully ) and prepare BAT files
and trigger those using window scheduler. - ( JUNIT4 )
Option 2 - Create BAT files for the eclipse java classes and run them
using window scheduler. ( JUNIT 4 ).

I have near about 300+ Java classes to execute, As of now Daily I am
running those from eclipse, just triggering one main master class
which triggers all other classes. Now I want to do this
automatically, so for this i am thinking of above two option.

I have modified the CLASSPATH for JAR file after i posted this
question, since then, i am able to run successfully from CMD.

Mike, I can still use your approach right, i.e. I ll create one BAT
file and in that I ll call only one main MASTER class that MASTER
class will call all other classes. or do i need to call all classes
separately ?
And can you give provide your BAT file content by masking your company
specific test class names ? I just want to see, whether you are doing
compile for every run ( or ) you are directly giving the eclipse/
netBeans CLASS file path to run.

Thanks

On Mar 27, 9:10 am, Mike Riley <lvskip...@cox.net> wrote:
> Yes I have done that and which editor you use doesn't matter.
>
> Using ANT or Maven, as Jayaraman said, would still mean you need
> something to fire it off.  I prefer not to use either because it is
> just one more layer to configure where things could go wrong and I
> would need to do it for each program.  With the batch files I simply
> copy the section where I invoked one program and change the name to
> add a new program to the mix.  Nice and easy.
>
> I am using the TestNG framework programmatically to run my tests:http://testng.org/doc/documentation-main.html#running-testng-programm...

Ross Patterson

unread,
Mar 27, 2012, 1:27:52 PM3/27/12
to seleniu...@googlegroups.com
True, although you can treat the JAR file as part of your classpath instead of as a JAR archive, and then it will be honored. Instead of this:

java ... -jar C:\some\dir\blah.jar ...

do this:

java ... -classpath C:\some\dir\blah.jar com.example.package.mainClass ...

You need to peek inside the JAR's manifest file to learn the starting class name, but other than that it's simple.

Ross
-----Original Message-----
From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Mike Riley
Sent: Tuesday, March 27, 2012 12:11 PM
To: Selenium Users
Subject: [selenium-users] Re: Selenium Java class file Execution from Command Prompt

...

You problem with your imports is likely an issue with your CLASSPATH.
Something to note about CLASSPATH: If you are using a JAR file it is
ignored! The Manifest file in the JAR file supersedes any CLASSPATH
setting. If you are executing an actual .class file then CLASSPATH
will be used.

...

Mike Riley

unread,
Mar 28, 2012, 12:55:20 PM3/28/12
to Selenium Users
Certainly you can have it invoke ANT if you wish. While I have an ANT
script for use within the Netbeans IDE, and ANT is available to run
outside the IDE, I did not bother exploring if the script would run
independently or if I would have to write my own. For me using the
command line option was more natural to the way I have written code in
the past.

I can give you an example here that doesn't give away anything. This
runs my test suite that tests the administration functions on the
site. It is called from another batch file that invokes each test
suite. As you will see there are several environment variables being
used. Most are defined by the calling batch file. SELENIUM_ROOT is
defined in the system environment list, but I have one batch file that
redefines it so that I can test with newer versions of Selenium. That
way I can verify that a new release doesn't break anything. I also
have two websites that is test with and simply uncommenting one and
commenting out the other suffice to change. One is the development
site and the other is the staging site for regression testing.

Enjoy!

rem @echo off

rem 1 2 3 4 5 6
7 8
rem
5678901234567890123456789012345678901234567890123456789012345678901234567890
rem ****
rem * This batch file runs all the Selenium test suites and you can
rem * select where to place the report and which browser to use.
rem *
rem * Syntax: AdminTestSuite <Base Report Dir> <End Report Dir>
<Browser> [<Version>]
rem *
rem * Browser choices: *iexplore *firefox googlechrome opera htmlunit
rem * Browser version is a whole number: 6, 7, 8, etc. and is optional
rem * Example to run IE9: ..\..\Reports IExplore9 *iexplore 9
rem * Example to run any IE: ..\..\Reports IExplore *iexplore
rem *
rem * NOTE 1: Report directory will be created if it does not exist.
All tests
rem * will be written to %1\Current\TestName\%2. After the
test has
rem * completed it will create a directory with the date in
YYYY-MM-DD
rem * format where Current was in the previous path and copy
the report
rem * to it. A task is run prior to any test batch files to
insure that
rem * %1\Current is empty.
rem *
rem * NOTE 2: The htmlunit browser choice fails to run DOTN due to
javascript
rem * issues.
rem ****

rem ****
rem * Uncomment the correct URL and MERCHANT below to set which system
to run
rem * the tests on.
rem ****

set URL=http://warthog-vm1/otn
set SERIAL=322
set MERCHANT="Test Bed - Retail"
rem set URL=http://iiscf1a/otn
rem set SERIAL=302
rem set MERCHANT="Retail 2 '11"

V:
cd %SELENIUM_ROOT%\Selenium\selenium

rem
============================================================================

set REPORTDIR=%1\Current\Admin\%2
if NOT EXIST "%REPORTDIR%" (
mkdir "%REPORTDIR%"
)

date /T > "%REPORTDIR%"\LastRun.TXT
time /T >> "%REPORTDIR%"\LastRun.TXT

if "%4"=="" (
java -jar Admin.jar -url %URL% -serial %SERIAL% -merchant %MERCHANT% -
username SeleniumUser -password certifymega -browser %3 -host
10.0.2.208 -reportdir "%REPORTDIR%"
) else (
java -jar Admin.jar -url %URL% -serial %SERIAL% -merchant %MERCHANT% -
username SeleniumUser -password certifymega -browser %3 -version %4 -
host 10.0.2.208 -reportdir "%REPORTDIR%"
)

if NOT "%errorlevel%"=="0" (
echo One or more tests failed! Check report for details. >>
"%REPORTDIR%"\LastRun.TXT
rem We could possibly E-mail failed test info at this point, print
it, or some such.
)


set TODAYDIR=%1\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%\Admin\%2
if NOT EXIST "%TODAYDIR%" (
mkdir "%TODAYDIR%"
)

xcopy "%REPORTDIR%"\*.* "%TODAYDIR%" /E/C/Q/Y
rem copy CON: NUL:

rem
============================================================================

Raju

unread,
Mar 28, 2012, 1:12:03 PM3/28/12
to Selenium Users
Thank you!

Seenivasaga M

unread,
Mar 9, 2015, 4:35:04 AM3/9/15
to seleniu...@googlegroups.com
Hi Raju,

I am facing the exact issues what you have mentioned and i need the same to be done as per your requirement.

Kindly suggest me.

Hi Babu,

I am running  the selenium java file.

1. javac -classpath "d:\parllay\libs\*" filename.java - This command is compiling.
2. java -classpath "d:\parllay\libs\*" filename - giving error could not load main class file name

Serguei Kouzmine

unread,
Mar 10, 2015, 8:33:36 PM3/10/15
to seleniu...@googlegroups.com
To launch Selenium hub/node on Windows, place the below in the directory Selenium and (in my case log4j) are located,


@echo OFF
setlocal
pushd %~dp0
set SELENIUM_HOME=%CD:\=/%

set NODE_HOST=%1
set NODE_HTTP_PORT=%2

if "%NODE_HTTP_PORT%" equ "" set NODE_HTTP_PORT=5555
REM NOTE - DNS may not be available
if "%NODE_HOST%" equ "" set NODE_HOST=%COMPUTERNAME%
if "%HUB_HOST%" equ "" set HUB_HOST=127.0.0.1
set HUB_HTTP_PORT=4444

set HTTPS_PORT=-1


set SELENIUM_VERSION=2.44.0
set GROOVY_VERSION=2.3.8
set JAVA_VERSION=1.6.0_45
set MAVEN_VERSION=3.2.1
set JAVA_HOME=c:\java\jdk%JAVA_VERSION%
set GROOVY_HOME=c:\java\groovy-%GROOVY_VERSION%
set M2_HOME=c:\java\apache-maven-%MAVEN_VERSION%
set M2=%M2_HOME%\bin
set MAVEN_OPTS=-Xms256m -Xmx512m

PATH=%JAVA_HOME%\bin;%PATH%;%GROOVY_HOME%\bin;%M2%

PATH=%PATH%;c:\Program Files\Mozilla Firefox

IF "%PROCESSOR_ARCHITECTURE%"=="x86" GOTO :PATH_x86
PATH=%PATH%;%ProgramFiles(x86)%\Google\Chrome\Application
PATH=%PATH%;%ProgramFiles(x86)%\Mozilla Firefox
PATH=%PATH%;%ProgramFiles(x86)%\Internet Explorer
GOTO :END_PATH
:PATH_x86
REM Browsers are installed in WOW6432
PATH=%PATH%;%ProgramFiles%\Google\Chrome\Application
PATH=%PATH%;%ProgramFiles%\Mozilla Firefox
PATH=%PATH%;%ProgramFiles%\Internet Explorer
GOTO :END_PATH
:END_PATH
PATH=%PATH%;%LOCALAPPDATA%\Mozilla Firefox

where.exe firefox.exe
where.exe chrome.exe
where.exe iexplore.exe
CHOICE /T 1 /C ync /CS /D y



set MAX_MEMORY=-Xmx256m
set STACK_SIZE=-Xss8m

rem Need to keep 1.7 and 1.6 both installed
set HUB_URL=http://127.0.0.1:4444/grid/register

REM cannot use paths
set NODE_CONFIG=NODE.json

set LAUNCHER_OPTS=-XX:MaxPermSize=1028M -Xmn128M
set LAUNCHER_OPTS=%MAX_MEMORY% %STACK_SIZE%

REM java %LAUNCHER_OPTS% ^
REM -jar selenium-server-standalone-%SELENIUM_VERSION%.jar ^

set LOGFILE=node.log
type NUL  > %LOGFILE%

java %LAUNCHER_OPTS% ^
-classpath %SELENIUM_HOME%/log4j-1.2.17.jar;%SELENIUM_HOME%/selenium-server-standalone-%SELENIUM_VERSION%.jar; ^-Dlog4j.configuration=node.log4j.properties ^
org.openqa.grid.selenium.GridLauncher ^
-role node ^
-host %NODE_HOST% ^
-port %NODE_HTTP_PORT% ^
-hub http://%HUB_HOST%:%HUB_HTTP_PORT%/hub/register ^
-Dwebdriver.ie.driver=IEDriverServer.exe ^
-Dwebdriver.chrome.driver=chromedriver.exe ^
-nodeConfig %NODE_CONFIG%  ^
-browserTimeout 120000 -timeout 120000 ^
-ensureCleanSession true ^
-trustAllSSLCertificates

REM Keep the Blank line above intact
goto :EOF


for hub , all the same except the bootstrap command that will become
java %LAUNCHER_OPTS% ^
-classpath %SELENIUM_HOME%/log4j-1.2.17.jar;^
%SELENIUM_HOME%/ConsoleServlet-1.0-SNAPSHOT.jar;^
%SELENIUM_HOME%/json-20080701.jar;^
%SELENIUM_HOME%/selenium-server-standalone-%SELENIUM_VERSION%.jar; ^
-Dlog4j.configuration=hub.log4j.properties ^
org.openqa.grid.selenium.GridLauncher ^
-port %HTTP_PORT% ^
-role hub ^
-ensureCleanSession true ^
-trustAllSSLCertificates true ^
-maxSession 20 ^
-newSessionWaitTimeout 600000 ^

REM Keep the Blank line above intact



To run the tests outside of maven, assuming you have the pom building your code int an app-1.1-SNAPSHOT.jar
and the entry is com.mycompany.app.App main()

@echo OFF

call mvn clean package install
set TARGET=%CD%\target

java -cp target\app-1.1-SNAPSHOT.jar;c:\java\selenium\selenium-server-standalone-2.44.0.jar;target\lib\*  com.mycompany.app.App

goto :EOF


I use these to actually test  against IE...

Thanks

Serguei Kouzmine

Krishnan Mahadevan

unread,
Mar 10, 2015, 11:59:48 PM3/10/15
to Selenium Users
Wow..! That's an elaborate batch script there Serguei!
Maybe you should just gist it and share the gist link. It would be easier to find it that way. !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Serguei Kouzmine

unread,
Mar 16, 2015, 9:43:39 PM3/16/15
to seleniu...@googlegroups.com

Aluchia

unread,
Jan 12, 2016, 10:22:49 PM1/12/16
to Selenium Users
Serguei,

your method of running java files assume there is a main() class.
My settings consist no main class in my Maven Project. I have a framework built using selenium with @Test notations.  I have 2 different directories created in the project.
One- with framework files, that consist of code for common functionalities to be used by any of the @Tests.
Second- classes with @Tests in them, that run certain scenarios to be tested on.  
How do I execute my Test.classes one at a time from command line in this case ?  
java -jar filename.jar   doesn't work for me, as it is looking for main() class in the jar. I do not have Main. 
thank you,
AB

Mark Collin

unread,
Jan 13, 2016, 5:12:47 AM1/13/16
to Selenium Users
You will need to add a main method.

Aluchia

unread,
Jan 13, 2016, 9:59:22 AM1/13/16
to Selenium Users
Thank you Mark,  I was thinking this way too, and didn't know how to and where to add main method. It is Maven project and those don't have main. 
I also learned that I can run my @tests via mvn as maven tests without main method.
So, for those that have the same issue and cannot figure out how to add main method, you can just run from command line or create a batch :    "mvn surefire:test -Dtest=<test_class_name>;
It worked very well for me, launched  my browser and executed all the @Tests in the class.  Hope it helped. 
many thanks again.
AB

Serguei Kouzmine

unread,
Jan 20, 2016, 2:11:11 PM1/20/16
to seleniu...@googlegroups.com
Hello

I believe You need to add the TestNG jar to the command line together with the Jar containing your test classes  which do not have its own entry point.



Serguei Kouzmine

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/FeKGnbBkQ7I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

jagrut...@gmail.com

unread,
Jan 19, 2018, 10:31:10 AM1/19/18
to Selenium Users

I want to Run my Eclipse selenium demo project from command line and i want to make Jar file of that

how can i achieve this  i have attached my project structure here...


Src->
Congif pkg
-> Control.java
->Variable.java
OR path
-> Xpath properties file
Testcase pkg
->4 different file include main file
utility pkg
-> common files here


Thank you

selenium.PNG
Reply all
Reply to author
Forward
0 new messages