PMC:QA:Junit and Sahi

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

Junit + Sahi

Base on this documentation http://sahi.co.in/forums/viewtopic.php?id=1442

  * Research and document how Junit and Sahi work together
  * Create a working POC 

how Junit and Sahi work together

Sahi Java Driver

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.

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 http://sahi.co.in/java/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.

Sahi Scripting - Calling Java

Sahi scripts run on the Rhino javascript engine. This allows Sahi to call any Java code in the classpath of Sahi.

POC

Sahi Java Driver

A POC to get started with Sahi Java.

Have a look at DriverClientTest.java in sample_java_project dir for more detailed use of APIs.

You need sahi/lib/sahi.jar in your classpath.

My environment

  • OS:linux
  • Browser:firefox-3.0.6

MyJavaClientTest.java

create a class "MyJavaClientTest.java",i put it in sahi/sample_java_project/src/in/co/sahi/ directory

package in.co.sahi;

import junit.framework.TestCase;
import net.sf.sahi.client.Browser;
import net.sf.sahi.client.ExecutionException;
import net.sf.sahi.config.Configuration;

/**
 * 
 * This is a sample class to get started with Sahi Java.<br/> 
 * Have a look at DriverClientTest.java in sample_java_project dir for more detailed use of APIs.<br/>
 * You need sahi/lib/sahi.jar in your classpath.</br>
 * 
 */
public class MyJavaClientTest extends TestCase {
	private Browser browser;
	private String sahiBase;
	private String userDataDirectory;

	/**
	 * This starts the Sahi proxy, toggles the proxy settings on Internet Explorer
	 * and starts a browser instance. This could be part of your setUp method in a JUnit test.
	 * 
	 */
	public void setUp(){
		sahiBase = "/mnt/sda4/Sahi/sahi0321/sahi/"; // where Sahi is installed or unzipped
		userDataDirectory = "myuserdata"; 
		Configuration.initJava(sahiBase, userDataDirectory); // Sets up configuration for proxy. Sets Controller to java mode.

	//	toggleIEProxy(true); // Not needed for Firefox. Needed for IE, Safari, Chrome on Windows
				
		String browserPath = "/usr/bin/firefox";
		String browserProcessName = "firefox";
		String browserOption = "-profile /mnt/sda4/Sahi/sahi0321/sahi/userdata/browser/ff/profiles/sahi1 -no-remote";
		browser = new Browser(browserPath, browserProcessName, browserOption);		
		browser.open();
	}	
	
	public void testGoogle() throws ExecutionException{
		browser.navigateTo("http://www.google.com/");
		browser.textbox("q").setValue("sahi forums");
		browser.submit("Google 搜尋").click();
		browser.waitFor(1000);
		browser.link("Sahi - Web Automation and Test Tool").click();
                browser.navigateTo("http://sahi.co.in/forums/");		
		browser.link("Login").click();
		System.out.println("check: browser.textbox(\"req_username\").exists() = " + browser.textbox("req_username").exists());
	}
	
        public void tearDown(){
                browser.close();
        //      proxy.stop();
        //      toggleIEProxy(false); // Not needed for Firefox. Needed for IE, Safari, Chrome on Wind$
        }

}

compile

create a new directory to store class file.

sample_java_project # mkdir classes

and our junit test need junit.jar,cp it to your classpath

# cd sahi/extlib/
extlib # mkdir test
extlib # cd test/
test # cp ../junit.jar ./
test # ls
junit.jar

Now you can compile the java file.

back to sample_java_project directory,run

javac -cp ../lib/sahi.jar;../extlib/test/junit.jar -d classes src/in/co/sahi/JavaClientTest.java

you can find it in sahi/sample_java_project/compile.bat

Example:

sample_java_project # javac -cp /mnt/sda4/Sahi/sahi0321/sahi/lib/sahi.jar:/mnt/sda4/Sahi/sahi0321/sahi/extlib/test/junit.jar -d classes/ src/in/co/sahi/MyJavaClientTest.java

execute

First you need to start Sahi

bin # ./sahi.sh
--------
SAHI_HOME: ..
SAHI_USERDATA_DIR: ../userdata
SAHI_EXT_CLASS_PATH:
--------
Sahi properties file = /mnt/sda4/Sahi/sahi0321/sahi/config/sahi.properties
Sahi user properties file = /mnt/sda4/Sahi/sahi0321/sahi/userdata/config/userdata.properties
>>>> Sahi started. Listening on port: 9999
>>>> Configure your browser to use this server and port as its proxy
>>>> Browse any page and CTRL-ALT-DblClick on the page to bring up the Sahi Controller

here we maybe need log4j.jar,copy it to sahi/extlib/test

Then can execute class by

java -cp ../lib/sahi.jar;../extlib/test/junit.jar;../extlib/test/log4j.jar;classes; junit.textui.TestRunner in.co.sahi.JavaClientTest

can find it in sahi/sample_java_project/run.bat

Example:

sample_java_project # java -cp /mnt/sda4/Sahi/sahi0321/sahi/lib/sahi.jar:/mnt/sda4/Sahi/sahi0321/sahi/extlib/test/junit.jar:/mnt/sda4/Sahi/sahi0321/sahi/extlib/test/log4j-1.2.9.jar:classes junit.textui.TestRunner in.co.sahi.MyJavaClientTest

it starts performing actions on the browser.

The result on the console:

.Sahi properties file = /mnt/sda4/Sahi/sahi0321/sahi/config/sahi.properties
Sahi user properties file = /mnt/sda4/Sahi/sahi0321/sahi/sample_java_project/myuserdata/config/userdata.properties
/usr/bin/firefox -profile /mnt/sda4/Sahi/sahi0321/sahi/userdata/browser/ff/profiles/sahi1 -no-remote http://sahi.example.com/_s_/dyn/Driver_start?startUrl=http%3A%2F%2Fsahi.example.com%2F_s_%2Fdyn%2FDriver_initialized__SahiAmpersandSahi__sahisid=sahi_040afb230e8c404ad30ac820819c8975f1c5
PIDs: [11791, 11925]; 10279 ms
check: browser.textbox("req_username").exists() = false

Time: 36.112

OK (1 test)

here you can find the result "browser.textbox("req_username").exists() = false",the right result is "true",I think that because of browser open two windows,then it can't find the element(the element textbox which named "rep_username") in the new window.

please refer to:http://sahi.co.in/forums/viewtopic.php?id=1471

we can modify code as follow:


public void testGoogle() throws ExecutionException{
		browser.navigateTo("http://www.google.com/");
		browser.textbox("q").setValue("sahi forums");
		browser.submit("Google 搜尋").click();
     		browser.waitFor(1000);
		browser.link("Sahi - Web Automation and Test Tool").click();
        //      browser.navigateTo("http://sahi.co.in/forums/");		
	//	browser.link("Login").click();
      	//	System.out.println("check: browser.textbox(\"req_username\").exists() = " + browser.textbox("req_username").exists());
                Browser newWin = browser.popup("Sahi - Web Automation and Test Tool"); 
                newWin.link("Login").click();
                Browser newloginWin = browser.popup("Login to Sahi - Web Automation and Test Tool - Sahi - Web Automation and Test Tool");
                System.out.println("check: newWin.textbox(\"req_username\").exists() = " +    newloginWin.textbox("req_username").exists());     
	}

recompile it and run,the result:

tp://sahi.example.com/_s_/dyn/Driver_start?startUrl=http%3A%2F%2Fsahi.example.com%2F_s_%2Fdyn%2FDriver_initialized__SahiAmpersandSahi__sahisid=sahi_1fab6fa104c810440f0ad3d0ccfbf61d8fe4
PIDs: [27308, 27416, 27418]; 6776 ms
check: newWin.textbox("req_username").exists() = true

and you can do it follow README.txt

1) Copy sahi/lib/sahi.jar to sample_java_project/lib/sahi.jar 2) Import this project into Eclipse 3) Add sahi.jar to the classpath 4) For Internet Explorer, make sure you have set the proxy settings to localhost:9999 5) Start Sahi's proxy using sahi/userdata/bin/start_sahi.bat 6) Run JavaClientTest as a JUnit testcase

test result:

Jusa.png

Sahi Scripting - Calling Java

Calling Java code only requires prepending the full package name with “Packages.”

Below is a simple example to print on the console.

create a sah file include scripts as follow:

test_calljava.sah

_navigateTo("http://www.google.co.in");
function printThroughJava(s){
    var sysout = Packages.java.lang.System.out;
    // sysout would have been of type java.io.PrintStream in java.
    // but here declared with only var
    sysout.println("Through Java: " + s);
}
printThroughJava("Hi there is a test case of calling java");

start sahi

run

sahi # java -cp lib/sahi.jar:lib/ant-sahi.jar net.sf.sahi.test.TestRunner /mnt/sda4/Sahi/sahi0321/sahi/userdata/scripts/test_calljava.sah "/usr/bin/firefox" http://www.google.co.in default localhost 9999 1 firefox

we can see at the sahi starting console will print the info:

...
>>>>>>                Tests size = 1
#### Running Script: /mnt/sda4/Sahi/sahi0321/sahi/userdata/scripts/test_calljava.sah
/usr/bin/firefox http://sahi.example.com/_s_/dyn/Player_auto?file=%2Fmnt%2Fsda4%2FSahi%2Fsahi0321%2Fsahi%2Fuserdata%2Fscripts%2Ftest_calljava.sah__SahiAmpersandSahi__startUrl=http%3A%2F%2Fwww.google.co.in__SahiAmpersandSahi__sahisid=sahi_e1706fd8036f604df20abc506c57367f7da9sahix729e8024010b704ac208c550c592dcc078a7x
PIDs: [30148, 30150]; 1357 ms
Through Java: Hi there is a test case of calling java
Killing /mnt/sda4/Sahi/sahi0321/sahi/userdata/scripts/test_calljava.sah
...

References

Links

Go back to PMC:QA