Sahi Pro - Sahi Java Driver
The Sahi Java Driver can be used to write Java unit tests and functional tests for web applications.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 browserType = "firefox";
// You can specify the browser you want to run the tests on.
// browserType can take any value defined in
// sahi/userdata/config/browser_types.xml
// Create a browser and open it
Browser browser = new Browser(browserType);
browser.open();
browser.navigateTo("http://www.google.com");
browser.textbox("q").setValue("sahi forums");
browser.submit("Google Search").click();
browser.waitFor(1000);
browser.link("Forums ? Sahi ? Web Automation and Test Tool").click();
browser.link("Login").click();
System.out.println(":: browser.textbox(\"req_username\").exists() = "
+ 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
and add lib/sahi.jar
to the project classpath).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.
Recording Java code
- Open sahi/config/sahi.properties and set controller.mode=java
- Restart Sahi
- Open a fresh browser with the proxy configured and navigate to any website.
- Press CTRL-ALT and DblClick on the page to bring up the Sahi Java Controller.
- Click on the record button and start performing actions on the browser. Steps will be visible in the "Recorded Steps" section.
- Copy the code from the Sahi console and paste it into your class.