How to start windows service using webdriver

1,523 views
Skip to first unread message

Amit

unread,
Mar 30, 2012, 5:54:00 AM3/30/12
to webdriver
My aim is to start and stop selenium RC server on remote machine using
webdriver. So I have created windows service for Selenium RC server.

However, I donot know how to start and stop windows service using
webdriver.

Please help me to solve this.

If any one know another solution to start/stop RC sever using
webdriver, please let me know.

Thanks
Amit

Krishnan Mahadevan

unread,
Mar 30, 2012, 5:55:06 AM3/30/12
to webd...@googlegroups.com
If you are working with WebDriver why do you need the Selenium RC server in the first place ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


Amit

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


SANTHOSH BABY

unread,
Mar 30, 2012, 6:24:58 AM3/30/12
to webd...@googlegroups.com
Hmm why you need selenium server  , Are you using 

HtmlUnit Driver  i that case we need i feel , Since i have not worked in this yet .

Thank you

Krishnan Mahadevan

unread,
Mar 30, 2012, 7:34:49 AM3/30/12
to webd...@googlegroups.com
None of the webdrivers require selenium server for its functioning. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


darrell

unread,
Mar 30, 2012, 9:11:35 AM3/30/12
to webd...@googlegroups.com
WebDriver is for web browsing. If we remove WebDriver from the equation, would you use a web browser to start a Windows service? I wouldn't. Use the right tool for the job. 

There are a few ways to start a Windows service. The first is make the service start automatically. Rebooting the computer than starts the service. However, if you don't want the service running all the time this is not a good solution. 

The second way is to go into the GUI for accessing services, right click the Selenium Server entry and select Start off the popup menu. If the machine is set up correctly, you can use the 'services.msc' application on one computer to access another computer. However, automating GUI is more difficult that command line. So is there a command line option?

The third way is issue a command line option to start and stop the service. The command line program to do this on Windows is 'sc.exe'. So if your service_name is SeleniumServer, you can start it using:

        sc start SeleniumServer

The best thing is you can use sc to start and stop services on remote machines. If the machine with the SeleniumServer service installed is called ss.mytestmachine.com then I can start it using:

        sc \\ss.mytestmachine.com start SeleniumServer

So rather that using WebDriver to start and stop a service, use sc.exe to start and stop the service. If you are using Java then look at the Runtime class. If you are using Python then look at the subprocess module. Other languages would have some way of running a command line executable. So you might do something like:

public void startServerService(String host) {
Runtime r = Runtime.getRuntime();
String executable = "sc.exe";
String server = "\\\\" + host;
String command = "start";
String service = "SeleniumServer";
String[] cmdarray = { executable, server, command, service };
try {
Process p = r.exec(cmdarray);
} catch (IOException e) {
e.printStackTrace();
}
}

If you want to confirm it started okay you can use p to get the data streams and see what the sc command outputs.

NOTE: I use "\\\\" in Java because this will become "\\" by the time it reaches the sc.exe command.

Darrell

Krishnan Mahadevan

unread,
Mar 30, 2012, 9:14:55 AM3/30/12
to webd...@googlegroups.com
Darrell,
Am still intrigued here as to what is the role of a Selenium RC when one is dealing with all the concrete versions of WebDriver (except for RemoteWebDriver).

All the concrete implementations of WebDriver by themselves startup a jetty server and make sure tests are run against it. Isn't that the case ?

If the question is dealing with how to go about maintaining a RemoteWebDriver a.k.a the Grid, then I understand the relevance of depending on a windows service or a cron job for that matter to maintain the hub and the node.

Am I missing something obvious here ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/6yZZEtht3VIJ.

darrell

unread,
Apr 2, 2012, 7:56:33 AM4/2/12
to webd...@googlegroups.com
Krishnan,

You are missing something obvious. In the very first sentence of Amit's posting it states , "My aim is to start and stop selenium RC server on remote machine using webdriver." Therefore the question is about maintaining a RemoteWebDriver.

Darrell
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Krishnan Mahadevan

unread,
Apr 2, 2012, 8:27:11 AM4/2/12
to webd...@googlegroups.com
Darrell,
My bad. Perhaps it would have been more clear had it been merely mentioned as "start and stop the Grid/hub" :)
Not used to seeing references to the grid as "selenium rc server on remote machine using webdriver" :)


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/qWpucHReqX8J.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Amit

unread,
Apr 3, 2012, 12:49:30 AM4/3/12
to webdriver
Thank you for reply.

I have not tried with sc.exe. But I have used below code to start and
stop web service.

import java.io.*;

public class WindowsServicesManager {
static final String CMD_START = "cmd /c net start \"";
static final String CMD_STOP = "cmd /c net stop \"";

public static int startService(String serviceName) throws Exception {
return execCmd(CMD_START + serviceName + "\"");
}

public static int stopService(String serviceName) throws Exception {
return execCmd(CMD_STOP + serviceName + "\"");
}

static int execCmd(String cmdLine) throws Exception {
Process process = Runtime.getRuntime().exec(cmdLine);
StreamPumper outPumper = new StreamPumper(process.getInputStream(),
System.out);
StreamPumper errPumper = new StreamPumper(process.getErrorStream(),
System.err);

outPumper.start();
errPumper.start();
process.waitFor();
outPumper.join();
errPumper.join();

return process.exitValue();
}

static class StreamPumper extends Thread {
private InputStream is;
private PrintStream os;

public StreamPumper(InputStream is, PrintStream os) {
this.is = is;
this.os = os;
}

public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;

while ((line = br.readLine()) != null)
os.println(line);
}
catch (Exception e) {
e.printStackTrace();
}
}

This is working fine only problem is that automation execution happens
in hide mode (Not actually open the browser). I am trying to find
another way as well to overcome this problem.

Thanks again for reply.

Amit

On Apr 2, 5:27 pm, Krishnan Mahadevan
<krishnan.mahadevan1...@gmail.com> wrote:
> Darrell,
> My bad. Perhaps it would have been more clear had it been merely mentioned
> as "start and stop the Grid/hub" :)
> Not used to seeing references to the grid as "selenium rc server on remote
> machine using webdriver" :)
>
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
>
>
>
> On Mon, Apr 2, 2012 at 5:26 PM, darrell <darrell.grain...@gmail.com> wrote:
> > Krishnan,
>
> > You are missing something obvious. In the very first sentence of Amit's
> > posting it states , "My aim is to start and stop selenium RC server on *remote
> > machine* using webdriver." Therefore the question is about maintaining a
> >>> To view this discussion on the web visithttps://groups.google.com/d/**
> >>> msg/webdriver/-/6yZZEtht3VIJ<https://groups.google.com/d/msg/webdriver/-/6yZZEtht3VIJ>
> >>> .
>
> >>> To post to this group, send email to webd...@googlegroups.com.
> >>> To unsubscribe from this group, send email to webdriver+unsubscribe@**
> >>> googlegroups.com <webdriver%2Bunsu...@googlegroups.com>.
> >>> For more options, visit this group athttp://groups.google.com/**
> >>> group/webdriver?hl=en <http://groups.google.com/group/webdriver?hl=en>.

darrell

unread,
Apr 3, 2012, 9:25:55 AM4/3/12
to webd...@googlegroups.com
The sc.exe command is better than the net.exe command because it will allow you to interact with remote machines.

If you are not seeing the browser then you need to look at how you implemented the service. By default, services are tasks which run in the background. If a service launches something else, that secondary task will also default to running in the background. You'll need to look into how Windows services work and how you might be able to configure it to have access to the current user's desktop. The service also needs to start when the user logs in. If it starts up with the operating system, i.e. before anyone logged in, then there is no desktop to interact with.

Personally, I like to set up a computer so it automatically logs in a local user with internet access. I then put a Selenium Server start up script in the user's Startup folder. Even if the machine is rebooted, it will automatically log the user in and run the server. Running the server as a Windows service seems cleaner but there is a whole other level of complexity to deal with when you do this.

Darrell
> >>> For more options, visit this group athttp://groups.google.com/**
> >>> group/webdriver?hl=en <http://groups.google.com/group/webdriver?hl=en>.
>
> >>  --
> > You received this message because you are subscribed to the Google Groups
> > "webdriver" group.
> > To view this discussion on the web visit
> >https://groups.google.com/d/msg/webdriver/-/qWpucHReqX8J.
>
> > To post to this group, send email to webd...@googlegroups.com.
> > To unsubscribe from this group, send email to

Amit

unread,
Apr 4, 2012, 2:32:32 AM4/4/12
to webdriver
Currently I am trying through windows service. But for this windows
service need to be created for RC server and if remote machine will
change in future, automation will be failed.

Is there any other way to start and stop Selenium RC server on remote
machine to avoid dependency on windows service?

Thanks
Amit
> > > >>> googlegroups.com <webdriver%2Bunsu...@googlegroups.com>.
> > > >>> For more options, visit this group athttp://groups.google.com/**
> > > >>> group/webdriver?hl=en <
> >http://groups.google.com/group/webdriver?hl=en>.
>
> > > >>  --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "webdriver" group.
> > > > To view this discussion on the web visit
> > > >https://groups.google.com/d/msg/webdriver/-/qWpucHReqX8J.
>
> > > > To post to this group, send email to webd...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > webdriver+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages