How to use clear_cookies method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

resource.py

Source: resource.py Github

copy
1# Licensed to Cloudera, Inc. under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  Cloudera, Inc. licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import logging
18import posixpath
19import time
20
21from desktop.lib.i18n import smart_unicode
22
23
24LOG = logging.getLogger(__name__)
25
26
27class Resource(object):
28  """
29  Encapsulates a resource, and provides actions to invoke on it.
30  """
31  def __init__(self, client, relpath="", urlencode=True):
32    """
33    @param client: A Client object.
34    @param relpath: The relative path of the resource.
35    @param urlencode: percent encode paths.
36    """
37    self._client = client
38    self._path = relpath.strip('/')
39    self._urlencode = urlencode
40
41  @property
42  def base_url(self):
43    return self._client.base_url
44
45  def _join_uri(self, relpath):
46    if relpath is None:
47      return self._path
48    return self._path + posixpath.normpath('/' + relpath)
49
50  def _format_response(self, resp):
51    """
52    Decide whether the body should be a json dict or string
53    """
54
55    if resp.headers.get('location') and resp.headers.get('location').startswith('http://localhost:8080/'):
56      return resp.headers.get('location').split('sessions/')[1]
57
58    if len(resp.content) != 0 and resp.headers.get('content-type') and \
59          'application/json' in resp.headers.get('content-type'):
60      try:
61        return resp.json()
62      except Exception, ex:
63        self._client.logger.exception('JSON decode error: %s' % resp.content)
64        raise ex
65    else:
66      return resp.content
67
68  def invoke(self, method, relpath=None, params=None, data=None, headers=None, files=None, allow_redirects=False, clear_cookies=False, log_response=True):
69    """
70    Invoke an API method.
71    @return: Raw body or JSON dictionary (if response content type is JSON).
72    """
73    path = self._join_uri(relpath)
74    start_time = time.time()
75    resp = self._client.execute(method,
76                                path,
77                                params=params,
78                                data=data,
79                                headers=headers,
80                                files=files,
81                                allow_redirects=allow_redirects,
82                                urlencode=self._urlencode,
83                                clear_cookies=clear_cookies)
84
85    if log_response and self._client.logger.isEnabledFor(logging.DEBUG):
86      self._client.logger.debug(
87        "%s %s Got response%s: %s%s" % (
88          method,
89          smart_unicode(path, errors='ignore'),
90           ' in %dms' % ((time.time() - start_time) * 1000),
91           smart_unicode(resp.content[:1000], errors='replace'),
92           len(resp.content) > 1000 and "..." or "")
93      )
94
95    return self._format_response(resp)
96
97  def get(self, relpath=None, params=None, headers=None, clear_cookies=False):
98    """
99    Invoke the GET method on a resource.
100    @param relpath: Optional. A relative path to this resource's path.
101    @param params: Key-value data.
102    @param clear_cookies: Optional. A flag to force any session cookies to be reset on the request.
103
104    @return: A dictionary of the JSON result.
105    """
106    return self.invoke("GET", relpath, params, headers=headers, allow_redirects=True, clear_cookies=clear_cookies)
107
108
109  def delete(self, relpath=None, params=None, headers=None, clear_cookies=False):
110    """
111    Invoke the DELETE method on a resource.
112    @param relpath: Optional. A relative path to this resource's path.
113    @param params: Key-value data.
114    @param headers: Optional. Base set of headers.
115    @param clear_cookies: Optional. A flag to force any session cookies to be reset on the request.
116
117    @return: A dictionary of the JSON result.
118    """
119    return self.invoke("DELETE", relpath, params, headers=headers, clear_cookies=clear_cookies)
120
121
122  def post(self, relpath=None, params=None, data=None, contenttype=None, headers=None, files=None, allow_redirects=False,
123           clear_cookies=False, log_response=True):
124    """
125    Invoke the POST method on a resource.
126    @param relpath: Optional. A relative path to this resource's path.
127    @param params: Key-value data.
128    @param data: Optional. Body of the request.
129    @param contenttype: Optional.
130    @param headers: Optional. Base set of headers.
131    @param allow_redirects: Optional. Allow request to automatically resolve redirects.
132    @param clear_cookies: Optional. A flag to force any session cookies to be reset on the request.
133
134    @return: A dictionary of the JSON result.
135    """
136    return self.invoke("POST", relpath, params, data, headers=self._make_headers(contenttype, headers), files=files,
137                       allow_redirects=allow_redirects, clear_cookies=clear_cookies, log_response=log_response)
138
139
140  def put(self, relpath=None, params=None, data=None, contenttype=None, allow_redirects=False, clear_cookies=False, headers=None):
141    """
142    Invoke the PUT method on a resource.
143    @param relpath: Optional. A relative path to this resource's path.
144    @param params: Key-value data.
145    @param data: Optional. Body of the request.
146    @param contenttype: Optional.
147    @param allow_redirects: Optional. Allow request to automatically resolve redirects.
148    @param clear_cookies: Optional. A flag to force any session cookies to be reset on the request.
149
150    @return: A dictionary of the JSON result.
151    """
152    return self.invoke("PUT", relpath, params, data, headers=self._make_headers(contenttype, headers),
153                       allow_redirects=allow_redirects, clear_cookies=clear_cookies)
154
155
156  def _make_headers(self, contenttype=None, headers=None):
157    if headers is None:
158      headers = {}
159
160    if contenttype:
161      headers.update({'Content-Type': contenttype})
162
163    return headers
164
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)