The Sahi Java Driver can be used to write Java unit tests and functional tests for web applications.
The Sahi Java Driver is bundled with Sahi’s releases and has been available since 2nd Sep 2009. We are thankful to ThoughtWorks for partially funding its development.
A brief example on the usage is provided below:
.
import net.sf.sahi.client.Browser;
import net.sf.sahi.config.Configuration;
public class SahiDriverDemo {
public static void main(String[] args) {
String sahiBase = "../"; // where Sahi is installed or unzipped
String userDataDirectory = "../userdata"; //path to the userdata directory
Configuration.initJava(sahiBase, userDataDirectory); // Sets up configuration for proxy. Sets Controller to java mode.
String browserPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
String browserProcessName = "firefox.exe";
String browserOption = "-profile $userDir/browser/ff/profiles/sahi0 -no-remote";
// UNCOMMENT for your preferred browser.
// String browserPath = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
// String browserProcessName = "iexplore.exe";
// String browserOption = "-noframemerging -extoff";
// Set browserOption to "-noframemerging -extoff" for IE 8, "" for IE 7
// String browserPath = "C:\\Documents and Settings\\Narayan Raman\\Local Settings" +
"\\Application Data\\Google\\Chrome\\Application\\chrome.exe";
// String browserProcessName = "chrome.exe";
// String browserOption =
// "--user-data-dir=$userDir\\browser\\chrome\\profiles\\sahi$threadNo" +
// " --proxy-server=localhost:9999 --disable-popup-blocking";
// String browserPath = "C:\\Program Files\\Safari\\Safari.exe";
// String browserProcessName = "Safari.exe";
// String browserOption = "";
// Create a browser and open it
Browser browser = new Browser(browserPath, browserProcessName, browserOption);
browser.open();
browser.navigateTo("http://www.google.com");
browser.textbox("q").setValue("sahi forums");
browser.submit("Google Search").click();
browser.link("Sahi - Web Automation and Test Tool").click();
browser.link("Login").click();
System.out.println("Check: " + browser.textbox("req_username").exists());
// close the browser
browser.close();
}
}
There is a sample Eclipse project available in sahi/sample_java_project. One needs to use the correct path for sahi.jar (or copy it into sahi/sample_java_project/lib from sahi/lib). More detailed examples are available in DriverClientTest.java which is in sahi/sample_java_project
Full documentation is available as Javadocs
Make sure that the proxy settings are properly configured on the browser (not needed for firefox) and that the Sahi proxy is running. Have a look at Using Sahi for details on configuring the browser and starting the proxy.