Sahi Java Driver
The Sahi Java Driver can be used to write Java unit tests and functional tests for web applications.warning
We recommend using the JS (Sahi) scripting language. Read our recommendations before deciding on the Java driver.
A brief example on the usage is provided below:
import com.sahipro.lang.java.client.Browser;
public class SahiDriverDemo {
public static void main(String[] args) {
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 add sahi_java_driver.jar
and js.jar
to the project classpath. Both of the jars are located at sahi/sample_java_project/lib
More detailed examples are available in DriverClientTestNew.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 controller and paste it into your class.