ZATS Cookies"

From Documentation
Line 7: Line 7:
  
 
'''cookie.zul'''
 
'''cookie.zul'''
<source lang="java" start="10" high="15,18,19,29">
+
<source lang="xml" start="10" high="15,18,19,29">
 
<zscript><![CDATA[
 
<zscript><![CDATA[
 
public void setCookie(String name, String value) {
 
public void setCookie(String name, String value) {
Line 37: Line 37:
 
'''Test.java'''
 
'''Test.java'''
 
<source lang="java" start="10" high="13,14,15,16,17">
 
<source lang="java" start="10" high="13,14,15,16,17">
Zats.init(".");
+
@Test
try{
+
public void Test() {
 
Client client = Zats.newClient();
 
Client client = Zats.newClient();
 
DesktopAgent desktop = client.connect("/cookie.zul");
 
DesktopAgent desktop = client.connect("/cookie.zul");
Line 45: Line 45:
 
desktop.query("#change").click();
 
desktop.query("#change").click();
 
Assert.assertEquals("hello", client.getCookie("foo"));
 
Assert.assertEquals("hello", client.getCookie("foo"));
}finally{
 
Zats.end();
 
 
}
 
}
 
</source>
 
</source>
Line 56: Line 54:
 
Besides applying cookies from the server-side, developers can also handle cookies from the client-side. In a ZK application, we can achieve the above behavior through the Client-side programming<ref>for more detail. Please refer to [[ZK_Client-side_Reference]]</ref>. Because ZATS Mimic doesn't perform the JavaScript code, it lets developers add, modify or remove cookies through <tt>Client</tt> in test 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 application, we can achieve the above behavior through the Client-side programming<ref>for more detail. Please refer to [[ZK_Client-side_Reference]]</ref>. Because ZATS Mimic doesn't perform the JavaScript code, it lets developers add, modify or remove cookies through <tt>Client</tt> in test code to simulate the Client-side programming.
  
<source lang="java" start="10" high="11, 12">
+
<source lang="java" start="10" high="13, 14">
Client client = Zats.newClient();
+
@Test
DesktopAgent desktop = client.connect("/cookie.zul");
+
public void test() {
client.setCookie("hello", "ZK");
+
Client client = Zats.newClient();
desktop.query("#show").click();
+
DesktopAgent desktop = client.connect("/cookie.zul");
String msg = desktop.query("#msg").as(Label.class).getValue();
+
client.setCookie("hello", "ZK");
Assert.assertTrue(msg.contains("hello=ZK"));
+
desktop.query("#show").click();
 +
String msg = desktop.query("#msg").as(Label.class).getValue();
 +
Assert.assertTrue(msg.contains("hello=ZK"));
 +
}
 
</source>
 
</source>
  
* '''Line 11, 12''': We can directly append a new cookie or replace old value with the same name in the test code. If we pass an cookie with <tt>null</tt> value, it indicates that removing the specific cookie. Notice that the name of cookie can not start with '''$''' character, it would be a reserved name.
+
* '''Line 13, 14''': We can directly append a new cookie or replace old value with the same name in the test code. If we pass an cookie with <tt>null</tt> value, it indicates that removing the specific cookie. Notice that the name of cookie can not start with '''$''' character, it would be a reserved name.
  
 
'''Notes'''
 
'''Notes'''

Revision as of 03:26, 11 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 handling the HTTP cookies for developers, ZATS Mimic introduces a group of methods on Client. ZATS Mimic seamlessly maintains cookies after connecting with a ZK application. We can read the current cookies and verify the behavior of the ZK application.

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

@Test
public void Test() {
	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"));
}
  • Line 13, 14, 15: After connected 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 application, we can achieve the above behavior through the Client-side programming[1]. Because ZATS Mimic doesn't perform the JavaScript code, it lets developers add, modify or remove cookies through Client in test code to simulate the Client-side programming.

@Test
public void test() {
	Client client = Zats.newClient();
	DesktopAgent desktop = client.connect("/cookie.zul");
	client.setCookie("hello", "ZK");
	desktop.query("#show").click();
	String msg = desktop.query("#msg").as(Label.class).getValue();
	Assert.assertTrue(msg.contains("hello=ZK"));
}
  • Line 13, 14: We can directly append a new cookie or replace old value with the same name in the test code. If we pass an cookie with null value, it indicates that removing the specific cookie. Notice that the name of cookie can not start with $ character, it would be a reserved name.

Notes

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



Last Update : 2012/06/11

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