Saturday, November 8, 2008

How to let JAVA play with Selenium Firefox Chrome TestRunner

Selenium Firefox Chrome TestRunner is written by JS/DHTML. When we want to embed Firefox Chrome TestRunner engine into our testing framework. We are asking for a way to let JAVA play with Firefox Chrome TestRunner. You can also refer to Selenium-RC source code to launch a browser. Here is a workaround solution with pseudo code.

1. In & Output of 'Selenium Firefox Chrome TestRunner' component for JAVA IN: it is usually URL trigger for running. Chrome URL with parameters looks like:
chrome://selenium-ide-testrunner/content/selenium/TestRunner.html?test=file:///c:/testing/MyTestSuite.html&auto=true&multiWindow=true&userExtensionsURL=http://localhost/user-extensions.js,http://localhost/ui/my-ui-map.js&close=true&[other self defined parms]
We can see Selenium passed parameters by chrome URL for input. We can add more self defined params by modify Selenium-IDE code.
If we can convert all parameters in JAVA to chrome URL parameters, it's possible to let JAVA invoke Selenium-IDE.

OUTPUT: one output choice provided by S-CORE is using POST data when Selenium closed. It required you create a server side receiver.
One better solution is S-IDE save run snapshot/result/log runtimely to file system directly. We treat these important data as output.

For offical docs about IN&OUTPUT, refer to http://seleniumhq.org/projects/core/usage.html

2. 'IN' Wrapper in JAVA
We create a Java Object as a chrome URL paramters wrapper, simple like:
public class SeleniumRequest implements Serializable {
 private String test; 
 private String auto;
        private String multiWindow;
...
        public String getAuto() {
  return auto;
 } 
 public void setAuto(String auto) {
  this.auto = auto;
 }
...
}
3. 'OUTPUT' Wrapper in JAVA
We also create a Java Object as atomic run result wrapper, simple like:
public class SeleniumResult implements Serializable {
 private int testRuns; 
 private int testFailures; 
        private String resultSummary;
 private String logFilePath;  
...
        public String getResultSummary() {
  return resultSummary;
 } 
 public void setResultSummary(String resultSummary) {
  this.resultSummary= resultSummary;
 }
...
}
Detailed run results are saved on file system by S-IDE. logFilePath is the path for their index file.

4. SeleniumJRunner in Java
Now, we can define a very simple & generic interface for run S-IDE in Java.
public interface SeleniumJRunner {
      public SeleniumResult run(SeleniumRequest request); 
}
We assume this run API is sync mode, which means run() will be blocked in Java thread until S-IDE finished run and generate OUTPUT.

5. SeleniumResultParser.java
S-IDE saved all results in file system, we need a Java class to parse file content and set to SeleniumResult object. This is part of SeleniumJRunner implementation logic. To do html file parser in Java, there are many opensource lib. You can use Nekohtml.

6. SyncExecutor.java
Since S-IDE is an external process for Java. We need launch it with parameters from Java in sync mode. Usually, JAVA API Runtime.getRuntime().exec() meets basic requirement. However, we need a timedout mechanism and safely execution which Runtime.getRuntime() doesn't provide. We create a powerful but simple SyncExecutor util. For related topic, refer to how to safely execute process from java

7. Launch Selenium with profile isolation on the same machine
It is super Firefox can run multiple instances with session/cache isolation concurrently on the same machine by Firefox Profiles mechanism.
We need to add -p ProfileName -no-remote parameters to Firefox cmdline.
For multiple Firefox profiles run together, refer to http://clan.yo2.cn/run-multiple-firefox-profiles-simultaneously.html

8. There is critical issue for launch Chrome TestRunner in cmdline mode
User may encounter weird issue if launch Chrome TestRunner in cmdline like firefox.exe -chrome "chrome://selenium-ide-testrunner/content/selenium/TestRunner.html..." . It's hard to resolve. In JRunner, we must choose a workaround substitution.
For solution, read topic: http://clearspace.openqa.org/message/51801#51801

1 comment:

  1. Thanks for the tutorial. I never knew how to let JAVA play with Selenium Firefox Chrome TestRunner before. Anyway, if you have time, take a visit at my Download Games website.

    ReplyDelete