ZATS Cookies"

From Documentation
Line 54: Line 54:
  
 
== Cookie handling from client-side ==
 
== Cookie handling from client-side ==
Besides handling cookies from server-side, developers can also handle cookies from client-side. In a ZK web application, we can achieve the previous behavior through the Client-side programming<ref>for more detail. Please refer to [[ZK_Client-side_Reference]]</ref>. ZATS Mimic lets developers add, modify or remove cookies in testing code to simulate the Client-side programming.
+
Besides applying cookies from the server-side, developers can also handle cookies from the client-side. In a ZK web application, we can achieve the previous behavior through the Client-side programming<ref>for more detail. Please refer to [[ZK_Client-side_Reference]]</ref>. ZATS Mimic lets developers add, modify or remove cookies in testing code to simulate the Client-side programming.
  
<source lang="java" start="10" high="">
+
<source lang="java" start="10" high="11, 12">
 
Client client = Zats.newClient();
 
Client client = Zats.newClient();
 
DesktopAgent desktop = client.connect("/cookie.zul");
 
DesktopAgent desktop = client.connect("/cookie.zul");
Line 64: Line 64:
 
Assert.assertTrue(msg.contains("hello=ZK"));
 
Assert.assertTrue(msg.contains("hello=ZK"));
 
</source>
 
</source>
* '''Line  
+
 
 +
* '''Line 11, 12''': We can append a new cookie in the test code directly. If we pass an cookie with <tt>null</tt> value, it indicates that remove the specific cookie. Notice that the name of cookie can not start with '''$''' character.
  
 
'''Notes'''
 
'''Notes'''

Revision as of 09:01, 10 June 2012

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

aowang



Since 1.1.0


In order to provide developers handle the HTTP cookies, ZATS Mimic introduces a group of methods on Client. ZATS Mimic seamless maintains cookies after connected with a ZK web application. We can read and verify the values of cookies.

cookie.zul

<zscript><![CDATA[
	public void setCookie(String name, String value) {
		javax.servlet.http.HttpServletResponse resp = Executions.getCurrent().getNativeResponse();
		resp.addCookie(new javax.servlet.http.Cookie(name, value));
	}
	setCookie("foo", "bar");
]]>
</zscript>
<button id="change" label="change" onClick='setCookie("foo", "hello");' />
<button id="show" label="show">
	<attribute name="onClick"><![CDATA[
		javax.servlet.http.HttpServletRequest req = Executions.getCurrent().getNativeRequest();
		StringBuilder sb = new StringBuilder();
		for (javax.servlet.http.Cookie c : req.getCookies())
			sb.append(c.getName()).append("=").append(c.getValue()).append(";");
		msg.setValue(sb.toString());
	]]>
	</attribute>
</button>
<label id="msg" />
  • Line 15: This will add a cookie when beginning.
  • Line 18: It changes the cookie from server-side when the user clicking the button.
  • Line 19, 29: If we click the button, web application will show all received cookies.


Test.java

Zats.init(".");
try{
	Client client = Zats.newClient();
	DesktopAgent desktop = client.connect("/cookie.zul");
	Assert.assertEquals("bar", client.getCookie("foo"));
	Assert.assertEquals(null, client.getCookie("not existed"));
	desktop.query("#change").click();
	Assert.assertEquals("hello", client.getCookie("foo"));
}finally{
	Zats.end();
}
  • Line 13, 14, 15: After connecting to a ZUL page, we can get the cookies and verify them.
  • Line 16, 17: ZATS Mimic maintains all cookies during any operations.

Cookie handling from client-side

Besides applying cookies from the server-side, developers can also handle cookies from the client-side. In a ZK web application, we can achieve the previous behavior through the Client-side programming[1]. ZATS Mimic lets developers add, modify or remove cookies in testing code to simulate the Client-side programming.

Client client = Zats.newClient();
DesktopAgent desktop = client.connect("/cookie.zul");
client.setCookie("hello", "ZK");
desktop.query("#show").click();
String msg = desktop.query("#msg").as(Lable.class).getValue();
Assert.assertTrue(msg.contains("hello=ZK"));
  • Line 11, 12: We can append a new cookie in the test code directly. If we pass an cookie with null value, it indicates that remove the specific cookie. Notice that the name of cookie can not start with $ character.

Notes

  1. for more detail. Please refer to ZK_Client-side_Reference



Last Update : 2012/06/10

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.