OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 | 7 |
8 import pyauto_functional # Must be imported before pyauto | 8 import pyauto_functional # Must be imported before pyauto |
9 import pyauto | 9 import pyauto |
10 | 10 |
11 | 11 |
12 class CookiesTest(pyauto.PyUITest): | 12 class CookiesTest(pyauto.PyUITest): |
13 """Tests for Cookies.""" | 13 """Tests for Cookies.""" |
14 | 14 |
15 def _CookieCheckIncognitoWindow(self, url): | 15 def _CookieCheckIncognitoWindow(self, url): |
16 """Check the cookie for the given URL in an incognito window.""" | 16 """Check the cookie for the given URL in an incognito window.""" |
17 # Navigate to the URL in an incognito window and verify no cookie is set. | 17 # Navigate to the URL in an incognito window and verify no cookie is set. |
18 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) | 18 self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW) |
19 self.NavigateToURL(url, 1, 0) | 19 self.NavigateToURL(url, 1, 0) |
20 self.assertFalse(self.GetCookie(pyauto.GURL(url))) | 20 self.assertFalse(self.GetCookie(pyauto.GURL(url)), |
| 21 msg='Unable to get cookie in incognito window.') |
21 | 22 |
22 def testSetCookies(self): | 23 def testSetCookies(self): |
23 """Test setting cookies and getting the value.""" | 24 """Test setting cookies and getting the value.""" |
24 cookie_url = pyauto.GURL(self.GetFileURLForDataPath('title1.html')) | 25 cookie_url = pyauto.GURL(self.GetFileURLForDataPath('title1.html')) |
25 cookie_val = 'foo=bar' | 26 cookie_val = 'foo=bar' |
26 self.SetCookie(cookie_url, cookie_val) | 27 self.SetCookie(cookie_url, cookie_val) |
27 self.assertEqual(cookie_val, self.GetCookie(cookie_url)) | 28 self.assertEqual(cookie_val, self.GetCookie(cookie_url), |
| 29 msg='Could not find the cookie value foo=bar') |
28 | 30 |
29 def testCookiesHttp(self): | 31 def testCookiesHttp(self): |
30 """Test cookies set over HTTP for incognito and regular windows.""" | 32 """Test cookies set over HTTP for incognito and regular windows.""" |
31 http_url = 'http://www.google.com' | 33 http_url = 'http://www.google.com' |
32 self.assertFalse(self.GetCookie(pyauto.GURL(http_url))) | 34 self.assertFalse(self.GetCookie(pyauto.GURL(http_url)), |
| 35 msg='No cookies found after navigating to %s' % http_url) |
33 # Incognito window | 36 # Incognito window |
34 self._CookieCheckIncognitoWindow(http_url) | 37 self._CookieCheckIncognitoWindow(http_url) |
35 # Regular window | 38 # Regular window |
36 self.NavigateToURL(http_url) | 39 self.NavigateToURL(http_url) |
37 cookie_data = self.GetCookie(pyauto.GURL(http_url)) | 40 cookie_data = self.GetCookie(pyauto.GURL(http_url)) |
38 self.assertTrue(cookie_data) | 41 self.assertTrue(cookie_data, |
| 42 msg='Cookie did not exist after loading %s' % http_url) |
39 # Restart and verify that the cookie persists. | 43 # Restart and verify that the cookie persists. |
40 self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(http_url))) | 44 self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(http_url)), |
| 45 msg='Cookie did not persist after restarting session.') |
41 | 46 |
42 def testCookiesHttps(self): | 47 def testCookiesHttps(self): |
43 """Test cookies set over HTTPS for incognito and regular windows.""" | 48 """Test cookies set over HTTPS for incognito and regular windows.""" |
44 https_url = 'https://www.google.com' | 49 https_url = 'https://www.google.com' |
45 self.assertFalse(self.GetCookie(pyauto.GURL(https_url))) | 50 self.assertFalse(self.GetCookie(pyauto.GURL(https_url)), |
| 51 msg='No cookies found after navigating to %s' % https_url) |
46 # Incognito window | 52 # Incognito window |
47 self._CookieCheckIncognitoWindow(https_url) | 53 self._CookieCheckIncognitoWindow(https_url) |
48 # Regular window | 54 # Regular window |
49 self.NavigateToURL(https_url) | 55 self.NavigateToURL(https_url) |
50 cookie_data = self.GetCookie(pyauto.GURL(https_url)) | 56 cookie_data = self.GetCookie(pyauto.GURL(https_url)) |
51 self.assertTrue(cookie_data) | 57 self.assertTrue(cookie_data, |
| 58 msg='Unable to get cookie after navigating to page') |
52 # Restart and verify that the cookie persists. | 59 # Restart and verify that the cookie persists. |
53 self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(https_url))) | 60 self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(https_url)), |
| 61 msg='Cookie did not persist after restarting session.') |
54 | 62 |
55 def testCookiesFile(self): | 63 def testCookiesFile(self): |
56 """Test cookies set from file:// url for incognito and regular windows.""" | 64 """Test cookies set from file:// url for incognito and regular windows.""" |
57 file_url = self.GetFileURLForDataPath('setcookie.html') | 65 file_url = self.GetFileURLForDataPath('setcookie.html') |
58 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 66 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 67 msg='Did not find cookie for file url %s' % file_url) |
59 # Incognito window | 68 # Incognito window |
60 self._CookieCheckIncognitoWindow(file_url) | 69 self._CookieCheckIncognitoWindow(file_url) |
61 # Regular window | 70 # Regular window |
62 self.NavigateToURL(file_url) | 71 self.NavigateToURL(file_url) |
63 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url))) | 72 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), |
| 73 msg='Cookie does not exist after navigating to the page.') |
64 # Restart and verify that cookie persists | 74 # Restart and verify that cookie persists |
65 self.RestartBrowser(clear_profile=False) | 75 self.RestartBrowser(clear_profile=False) |
66 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url))) | 76 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), |
| 77 msg='Cookie did not persist after restarting session.') |
67 | 78 |
68 def testBlockCookies(self): | 79 def testBlockCookies(self): |
69 """Verify that cookies are being blocked.""" | 80 """Verify that cookies are being blocked.""" |
70 file_url = self.GetFileURLForDataPath('setcookie.html') | 81 file_url = self.GetFileURLForDataPath('setcookie.html') |
71 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 82 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 83 msg='Did not find cookie for url %s' % file_url) |
72 | 84 |
73 # Set the preference to block all cookies. | 85 # Set the preference to block all cookies. |
74 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | 86 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) |
75 # Regular window | 87 # Regular window |
76 self.NavigateToURL('http://www.google.com') | 88 self.NavigateToURL('http://www.google.com') |
77 self.AppendTab(pyauto.GURL('https://www.google.com')) | 89 self.AppendTab(pyauto.GURL('https://www.google.com')) |
78 self.AppendTab(pyauto.GURL(file_url)) | 90 self.AppendTab(pyauto.GURL(file_url)) |
79 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), | 91 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
80 msg='Cookies are not blocked.') | 92 msg='Cookies are not blocked.') |
81 | 93 |
82 # Incognito window | 94 # Incognito window |
83 self._CookieCheckIncognitoWindow('http://www.google.com') | 95 self._CookieCheckIncognitoWindow('http://www.google.com') |
84 self.GetBrowserWindow(1).GetTab(0).Close(True) | 96 self.GetBrowserWindow(1).GetTab(0).Close(True) |
85 | 97 |
86 # Restart and verify that cookie setting persists and there are no cookies. | 98 # Restart and verify that cookie setting persists and there are no cookies. |
87 self.SetPrefs(pyauto.kRestoreOnStartup, 1) | 99 self.SetPrefs(pyauto.kRestoreOnStartup, 1) |
88 self.RestartBrowser(clear_profile=False) | 100 self.RestartBrowser(clear_profile=False) |
89 self.assertEquals({u'cookies': 2}, | 101 self.assertEquals({u'cookies': 2}, |
90 self.GetPrefsInfo().Prefs(pyauto.kDefaultContentSettings)) | 102 self.GetPrefsInfo().Prefs(pyauto.kDefaultContentSettings)) |
91 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 103 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 104 msg='Cookie setting did not persist after restarting ' |
| 105 'session.') |
92 | 106 |
93 def testClearCookiesOnEndingSession(self): | 107 def testClearCookiesOnEndingSession(self): |
94 """Verify that cookies are cleared when the browsing session is closed.""" | 108 """Verify that cookies are cleared when the browsing session is closed.""" |
95 file_url = self.GetFileURLForDataPath('setcookie.html') | 109 file_url = self.GetFileURLForDataPath('setcookie.html') |
96 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 110 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 111 msg='Unable to set cookie.') |
97 | 112 |
98 # Set the option to clear cookies when the browser session is closed. | 113 # Set the option to clear cookies when the browser session is closed. |
99 self.SetPrefs(pyauto.kClearSiteDataOnExit, True) | 114 self.SetPrefs(pyauto.kClearSiteDataOnExit, True) |
100 | 115 |
101 self.NavigateToURL(file_url) | 116 self.NavigateToURL(file_url) |
102 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url))) | 117 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), |
| 118 msg='Unable to retrieve the cookie name=Good') |
103 | 119 |
104 # Restart and verify that cookie does not persist | 120 # Restart and verify that cookie does not persist |
105 self.RestartBrowser(clear_profile=False) | 121 self.RestartBrowser(clear_profile=False) |
106 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 122 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 123 msg='Cookie persisted after restarting session.') |
107 | 124 |
108 def testAllowCookiesUsingExceptions(self): | 125 def testAllowCookiesUsingExceptions(self): |
109 """Verify that cookies can be allowed and set using exceptions for | 126 """Verify that cookies can be allowed and set using exceptions for |
110 particular website(s) when all others are blocked.""" | 127 particular website(s) when all others are blocked.""" |
111 file_url = self.GetFileURLForDataPath('setcookie.html') | 128 file_url = self.GetFileURLForDataPath('setcookie.html') |
112 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 129 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 130 msg='Unable to set cookie.') |
113 | 131 |
114 # Set the preference to block all cookies. | 132 # Set the preference to block all cookies. |
115 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | 133 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) |
116 | 134 |
117 self.NavigateToURL(file_url) | 135 self.NavigateToURL(file_url) |
118 # Check that no cookies are stored. | 136 # Check that no cookies are stored. |
119 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 137 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 138 msg='A cookie was found when it should not have been.') |
120 | 139 |
121 # Creating an exception to allow cookies from http://www.google.com. | 140 # Creating an exception to allow cookies from http://www.google.com. |
122 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | 141 self.SetPrefs(pyauto.kContentSettingsPatternPairs, |
123 {'[*.]google.com,*': { 'cookies': 1}}) | 142 {'[*.]google.com,*': { 'cookies': 1}}) |
124 # Navigate to google.com and check if cookies are set. | 143 # Navigate to google.com and check if cookies are set. |
125 self.NavigateToURL('http://www.google.com') | 144 self.NavigateToURL('http://www.google.com') |
126 self.assertTrue(self.GetCookie(pyauto.GURL('http://www.google.com')), | 145 self.assertTrue(self.GetCookie(pyauto.GURL('http://www.google.com')), |
127 msg='Cookies are not set for the exception.') | 146 msg='Cookies are not set for the exception.') |
128 | 147 |
129 def testBlockCookiesUsingExceptions(self): | 148 def testBlockCookiesUsingExceptions(self): |
130 """Verify that cookies can be blocked for a specific website | 149 """Verify that cookies can be blocked for a specific website |
131 using exceptions.""" | 150 using exceptions.""" |
132 file_url = self.GetFileURLForDataPath('setcookie.html') | 151 file_url = self.GetFileURLForDataPath('setcookie.html') |
133 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 152 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 153 msg='Unable to set cookie.') |
134 | 154 |
135 # Create an exception to block cookies from http://www.google.com | 155 # Create an exception to block cookies from http://www.google.com |
136 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | 156 self.SetPrefs(pyauto.kContentSettingsPatternPairs, |
137 {'[*.]google.com,*': { 'cookies': 2}}) | 157 {'[*.]google.com,*': { 'cookies': 2}}) |
138 | 158 |
139 # Navigate to google.com and check if cookies are blocked. | 159 # Navigate to google.com and check if cookies are blocked. |
140 self.NavigateToURL('http://www.google.com') | 160 self.NavigateToURL('http://www.google.com') |
141 self.assertFalse(self.GetCookie(pyauto.GURL('http://www.google.com')), | 161 self.assertFalse(self.GetCookie(pyauto.GURL('http://www.google.com')), |
142 msg='Cookies are being set for the exception.') | 162 msg='Cookies are being set for the exception.') |
143 | 163 |
144 # Check if cookies are being set for other websites/webpages. | 164 # Check if cookies are being set for other websites/webpages. |
145 self.AppendTab(pyauto.GURL(file_url)) | 165 self.AppendTab(pyauto.GURL(file_url)) |
146 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url))) | 166 self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)), |
| 167 msg='Unable to find cookie name=Good') |
147 | 168 |
148 def testAllowCookiesForASessionUsingExceptions(self): | 169 def testAllowCookiesForASessionUsingExceptions(self): |
149 """Verify that cookies can be allowed and set using exceptions for | 170 """Verify that cookies can be allowed and set using exceptions for |
150 particular website(s) only for a session when all others are blocked.""" | 171 particular website(s) only for a session when all others are blocked.""" |
151 file_url = self.GetFileURLForPath('setcookie.html') | 172 file_url = self.GetFileURLForPath('setcookie.html') |
152 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 173 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 174 msg='Unable to set cookie.') |
153 | 175 |
154 # Set the preference to block all cookies. | 176 # Set the preference to block all cookies. |
155 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) | 177 self.SetPrefs(pyauto.kDefaultContentSettings, {u'cookies': 2}) |
156 | 178 |
157 self.NavigateToURL(file_url) | 179 self.NavigateToURL(file_url) |
158 # Check that no cookies are stored. | 180 # Check that no cookies are stored. |
159 self.assertFalse(self.GetCookie(pyauto.GURL(file_url))) | 181 self.assertFalse(self.GetCookie(pyauto.GURL(file_url)), |
| 182 msg='Cookies were found for the url %s' % file_url) |
160 | 183 |
161 # Creating an exception to allow cookies for a session for google.com. | 184 # Creating an exception to allow cookies for a session for google.com. |
162 self.SetPrefs(pyauto.kContentSettingsPatternPairs, | 185 self.SetPrefs(pyauto.kContentSettingsPatternPairs, |
163 {'[*.]google.com,*': { 'cookies': 4}}) | 186 {'[*.]google.com,*': { 'cookies': 4}}) |
164 | 187 |
165 # Navigate to google.com and check if cookies are set. | 188 # Navigate to google.com and check if cookies are set. |
166 self.NavigateToURL('http://www.google.com') | 189 self.NavigateToURL('http://www.google.com') |
167 self.assertTrue(self.GetCookie(pyauto.GURL('http://www.google.com')), | 190 self.assertTrue(self.GetCookie(pyauto.GURL('http://www.google.com')), |
168 msg='Cookies are not set for the exception.') | 191 msg='Cookies are not set for the exception.') |
169 | |
170 # Restart the browser to check that the cookie doesn't persist. | 192 # Restart the browser to check that the cookie doesn't persist. |
171 self.RestartBrowser(clear_profile=False) | 193 self.RestartBrowser(clear_profile=False) |
172 self.assertFalse(self.GetCookie(pyauto.GURL('http://www.google.com')), | 194 self.assertFalse(self.GetCookie(pyauto.GURL('http://www.google.com')), |
173 msg='Cookie persisted after restarting session.') | 195 msg='Cookie persisted after restarting session.') |
174 | 196 |
175 | 197 |
176 if __name__ == '__main__': | 198 if __name__ == '__main__': |
177 pyauto_functional.Main() | 199 pyauto_functional.Main() |
OLD | NEW |