By Sadhanandhan B

During the last week of February, one of our customers required us to include a feature in our test automation offering for the customer. The customer wanted to execute tests from a remote machine. The customer had a Linux box, which was hosting the Jenkins Continuous Integration.
The challenge was to execute the tests on a Windows box with the required browsers and to call the execution from the Linux box using Jenkins and Apache Maven.
According to Wikipedia, “Jenkins is an open source continuous integration tool written in Java. The project was forked from Hudson after a dispute with Oracle. Jenkins provides continuous integration services for software development. It is a server-based system running in a servlet container such as Apache Tomcat”.
“Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.”
The test environment was setup like below:
Figure 1: Test
Environment Setup
There was one Linux server, which was running the Jenkins CI
server and there were two Windows boxes running the various browsers used for
test execution.
All our tests were created within the AFiS framework on
Java, using the Page Object Model in Selenium webdriver for test creation. TestNG was used as the Test Runner and
ReportNG was used for captivating and concise
reporting. Apache Maven was used for the
project object model for better structure and flexibility for our test
projects. Maven comes in handy when
distributing source, as the libraries can be downloaded on individual machines
when the tests are run, rather than bundling the library files with the
source. This allowed us to save on
uploads and downloads of the sources. Also, all source files were stored in the
Subversion server as part of the SCM requirements.
The tests were executed by schedule – to run every day at a
specified time, and the results were stored in the Jenkins server.
Code inside the BeforeSuite or BeforeTest annotations of the webdriver test:
String browserName = System.getProperty(“BrowserName”);
String remoteExecution = System.getProperty(“RunRemotely”);
String remoteMacIP = System.getProperty(“RemoteMacIP”);
String reportMacPort = System.getProperty(“RemoteMacPort”);
if (remoteExecution.equals(“true”) {
if (browserName.equals(“firefox”))
browser = DesiredCapabilities.firefox();
else if (browserName.equals(“chrome”))
browser=DesiredCapabilities.chrome();
else if (browserName.equals(“internet explorer”))
browser=DesiredCapabilities.internetExplorer();
RemoteWebDriver driver = new RemoteWebDriver (new URL(“http://” +remoteHost +remotePort+”/wd/hub”, browser);
} else {
// Use normal settings for firefox, chrome and IE
}
In POM.XML the following will need to be set up:
<systemPropertyVariables>
<BrowserName>firefox</BrowserName>
<RunRemotely>false</RunRemotely>
<RemoteMacIP>localhost</RemoteMacIP>
<RemoteMacPort>4444</RemoteMacPort>
</systemPropertyVariables>
Finally, calling Maven to test:
mvn -DBrowserName=firefox -DRunRemotely=true -DRemoteMacIP=10.10.0.20 -DRemoteMacPort=4444 test
Modus Operandi
The design of the POM file, for use in Maven, was changed to include variables such as name of the browser, remote host name, port number, and whether remote execution was required. These were setup as System Property values in the POM surefire test plugins.
Once the variables were setup as System Property values, maven command line calling was implemented with the various property variables to facilitate remote execution. These System Property values are used in the Selenium webdriver framework to run the tests with RemoteWebdriver with the DesiredCapabilities class. Based on the inputs from the browser name, the DesiredCapabilities class can use either .firefox(), .chrome() or .internetExplorer() for the remote browser. And, the RemoteWebdriver needs the remote host and port to initialize the class.
Conclusion
To conclude, Apache Maven can be used from the command line to trigger System Property values which can then be used inside the Java code in Selenium webdriver tests to run tests on remote machines, which uses RemoteWebDriver and the DesiredCapabilities classes as the base classes to run these tests on remote machines.















