|
| 1 | +from PuppeteerLibrary.base.robotlibcore import keyword |
| 2 | +from PuppeteerLibrary.base.librarycomponent import LibraryComponent |
| 3 | +from PuppeteerLibrary.keywords.mockresponse_async import MockResponseKeywordsAsync |
| 4 | + |
| 5 | + |
| 6 | +class MockResponseKeywords(LibraryComponent): |
| 7 | + |
| 8 | + def __init__(self, ctx): |
| 9 | + super().__init__(ctx) |
| 10 | + self.async_func = MockResponseKeywordsAsync(self.ctx) |
| 11 | + |
| 12 | + @keyword |
| 13 | + def mock_current_page_api_response(self, url, mock_response, method='GET', body=None): |
| 14 | + """ |
| 15 | + Mock current page api response. |
| 16 | +
|
| 17 | + The ``mock_response`` is a dictionary which can have the following fields: |
| 18 | + - ``status`` (int): Response status code, defaults to 200. |
| 19 | + - ``headers`` (dict): Optional response headers. |
| 20 | + - ``contentType`` (str): If set, equals to setting ``Content-Type`` response header. |
| 21 | + - ``body`` (str|bytes): Optional response body. |
| 22 | +
|
| 23 | + The ``url`` is request url. url can be partial url match using regexp |
| 24 | + Match Options: |
| 25 | +
|
| 26 | + | Options | Url value | |
| 27 | + | Exact match | ^http://127.0.0.1:7272/ajax_info.json\\?count=3$ | |
| 28 | + | Partial match | /ajax_info.json\\?count=3 | |
| 29 | + | Regular expression | .*?/ajax_info.json\\?count=3 | |
| 30 | +
|
| 31 | + The ``method`` is HTTP Request Methods: |
| 32 | + - GET (default) |
| 33 | + - POST |
| 34 | + - PUT |
| 35 | + - HEAD |
| 36 | + - DELETE |
| 37 | + - PATCH |
| 38 | +
|
| 39 | + The ``body`` is request body message. body can match using regexp |
| 40 | +
|
| 41 | + Example: |
| 42 | +
|
| 43 | + | &{response} | Create Dictionary | body=I'm a mock response | |
| 44 | + | Mock Current Page Api Response | /ajax_info.json\\?count=3 | ${response} | |
| 45 | +
|
| 46 | + """ |
| 47 | + return self.loop.run_until_complete(self.async_func.mock_current_page_api_response_async(url, mock_response, method, body)) |
0 commit comments