Ejemplo n.º 1
0
 def fail(self, exception=None):
     if isinstance(exception, str):
         exception = CatchResponseError(exception)
     events.request_failure.fire(request_type="grpc",
                                 name=self.name,
                                 response_time=self.total_time,
                                 exception=exception)
Ejemplo n.º 2
0
    async def pwwrapFunc(user: PlaywrightUser):
        user.browser_context = await user.browser.new_context(
            ignore_https_errors=True, base_url=user.host)
        # await user.browser_context.add_init_script("() => delete window.navigator.serviceWorker")
        user.page = await user.browser_context.new_page()
        user.page.set_default_timeout(60000)

        if isinstance(user, PlaywrightScriptUser):
            name = user.script
        else:
            name = user.__class__.__name__ + "." + func.__name__
        try:
            task_start_time = time.time()
            start_perf_counter = time.perf_counter()
            await func(user, user.page)
            user.environment.events.request.fire(
                request_type="TASK",
                name=name,
                start_time=task_start_time,
                response_time=(time.perf_counter() - start_perf_counter) *
                1000,
                response_length=0,
                context={**user.context()},
                exception=None,
                # url=user.page.url,
            )
        except RescheduleTask:
            pass  # no need to log anything, because an individual request has already failed
        except Exception as e:
            try:
                error = CatchResponseError(
                    re.sub("=======*", "", e.message + user.page.url).replace(
                        "\n", "").replace(" logs ", " "))
            except:
                error = e  # never mind
            if not user.error_screenshot_made:
                user.error_screenshot_made = True  # dont spam screenshots...
                if user.page:  # in ScriptUser runs we have no reference to the page so...
                    await user.page.screenshot(path="screenshot_" +
                                               time.strftime("%Y%m%d_%H%M%S") +
                                               ".png",
                                               full_page=True)
            user.environment.events.request.fire(
                request_type="TASK",
                name=name,
                start_time=task_start_time,
                response_time=(time.perf_counter() - start_perf_counter) *
                1000,
                response_length=0,
                context={**user.context()},
                exception=error,
                url=user.page.url if user.page else None,
            )
        finally:
            await user.page.wait_for_timeout(
                1000)  # give outstanding interactions some time
            await user.page.close()
            await user.browser_context.close()
Ejemplo n.º 3
0
 def failure(self, exc):
     """
     Report the response as a failure.
     
     exc can be either a python exception, or a string in which case it will
     be wrapped inside a CatchResponseError. 
     
     Example::
     
         with self.client.get("/", catch_response=True) as response:
             if response.content == "":
                 response.failure("No data")
     """
     if isinstance(exc, str):
         exc = CatchResponseError(exc)
     self._manual_result = exc
Ejemplo n.º 4
0
    def failure(self, exc):
        """
        Report the response as a failure.

        if exc is anything other than a python exception (like a string) it will
        be wrapped inside a CatchResponseError.

        Example::

            with self.client.get("/", catch_response=True) as response:
                if response.content == "":
                    response.failure("No data")
        """
        if not isinstance(exc, Exception):
            exc = CatchResponseError(exc)
        self._manual_result = exc
Ejemplo n.º 5
0
async def event(
    user: "******",
    name="unnamed",
    request_type="event",
):
    start_time = time.time()
    start_perf_counter = time.perf_counter()
    try:
        yield
        user.environment.events.request.fire(
            request_type=request_type,
            name=name,
            start_time=start_time,
            response_time=(time.perf_counter() - start_perf_counter) * 1000,
            response_length=0,
            context={**user.context()},
            url=user.page.url if user.page else None,
            exception=None,
        )
    except Exception as e:
        try:
            error = CatchResponseError(
                re.sub("=======*", "",
                       e.message).replace("\n", "").replace(" logs ",
                                                            " ")[:500])
        except:
            error = e  # never mind
        if not user.error_screenshot_made:
            user.error_screenshot_made = True  # dont spam screenshots...
            if user.page:  # in ScriptUser runs we have no reference to the page so...
                await user.page.screenshot(path="screenshot_" +
                                           time.strftime("%Y%m%d_%H%M%S") +
                                           ".png",
                                           full_page=False)
        user.environment.events.request.fire(
            request_type=request_type,
            name=name,
            start_time=start_time,
            response_time=(time.perf_counter() - start_perf_counter) * 1000,
            response_length=0,
            url=user.page.url if user.page else None,
            context={**user.context()},
            exception=error,
        )
    await asyncio.sleep(0.1)
Ejemplo n.º 6
0
    def failure(self, exc):
        """
        Report the response as a failure.

        if exc is anything other than a python exception (like a string) it will
        be wrapped inside a CatchResponseError.

        Example::

            with self.client.get("/", catch_response=True) as response:
                if response.content == "":
                    response.failure("No data")
        """
        if not self._entered:
            raise LocustError(
                "Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n    response.failure(...)"
            )
        if not isinstance(exc, Exception):
            exc = CatchResponseError(exc)
        self._manual_result = exc
Ejemplo n.º 7
0
    def failure(self, exc):
        """
        Report the response as a failure.

        exc can be either a python exception, or a string in which case it will
        be wrapped inside a CatchResponseError.

        Example::

            with self.client.get("/", catch_response=True) as response:
                if response.content == "":
                    response.failure("No data")
        """
        if isinstance(exc, six.string_types):
            exc = CatchResponseError(exc)

        fire_failure(self.locust_request_meta, self.task, exc)
        self._is_reported = True
        name = "{} {}".format(self.locust_request_meta["method"],
                              self.locust_request_meta["name"])
        raise RescheduleTask(exc, name)
Ejemplo n.º 8
0
 def failure(self, exc):
     """
     Report the response as a failure.
     if exc is anything other than a python exception (like a string) it will
     be wrapped inside a CatchResponseError.
     Example::
         with self.request(name="helpful_name") as request:
             request.client.get("https://example.com/")
             title = request.client.find_element(By.CSS_SELECTOR, "body > div > h1")
             if title.text != "Example Domain":
                 request.success()
             else:
                 request.failure("Page title didn't match")
     """
     if not self._entered:
         raise LocustError(
             "Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n    response.failure(...)"
         )
     if not isinstance(exc, Exception):
         exc = CatchResponseError(exc)
     self._manual_result = exc
     self.request_meta["response_time"] = (
         time.perf_counter() - self.request_meta["start_time"]) * 1000
Ejemplo n.º 9
0
    def failure(self, exc):
        """
        Report the response as a failure.

        exc can be either a python exception, or a string in which case it will
        be wrapped inside a CatchResponseError.

        Example::

            with self.client.get("/", catch_response=True) as response:
                if response.content == "":
                    response.failure("No data")
        """
        if isinstance(exc, six.string_types):
            exc = CatchResponseError(exc)

        events.request_failure.fire(
            request_type=self.locust_request_meta["method"],
            name=self.locust_request_meta["name"],
            response_time=self.locust_request_meta["response_time"],
            exception=exc,
        )
        self._is_reported = True