Interface Worker

  • All Known Implementing Classes:
    WorkerImpl

    public interface Worker
    The Worker class represents a WebWorker. worker event is emitted on the page object to signal a worker creation. close event is emitted on the worker object when the worker is gone.
    
     page.onWorker(worker -> {
       System.out.println("Worker created: " + worker.url());
       worker.onClose(worker1 -> System.out.println("Worker destroyed: " + worker1.url()));
     });
     System.out.println("Current workers:");
     for (Worker worker : page.workers())
       System.out.println("  " + worker.url());
     
    • Method Detail

      • onClose

        void onClose​(java.util.function.Consumer<Worker> handler)
        Emitted when this dedicated WebWorker is terminated.
      • offClose

        void offClose​(java.util.function.Consumer<Worker> handler)
        Removes handler that was previously added with onClose(handler).
      • evaluate

        default java.lang.Object evaluate​(java.lang.String expression)
        Returns the return value of expression.

        If the function passed to the Worker.evaluate() returns a Promise, then Worker.evaluate() would wait for the promise to resolve and return its value.

        If the function passed to the Worker.evaluate() returns a non-[Serializable] value, then Worker.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

        Parameters:
        expression - JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
        Since:
        v1.8
      • evaluate

        java.lang.Object evaluate​(java.lang.String expression,
                                  java.lang.Object arg)
        Returns the return value of expression.

        If the function passed to the Worker.evaluate() returns a Promise, then Worker.evaluate() would wait for the promise to resolve and return its value.

        If the function passed to the Worker.evaluate() returns a non-[Serializable] value, then Worker.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

        Parameters:
        expression - JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
        arg - Optional argument to pass to expression.
        Since:
        v1.8
      • evaluateHandle

        JSHandle evaluateHandle​(java.lang.String expression,
                                java.lang.Object arg)
        Returns the return value of expression as a JSHandle.

        The only difference between Worker.evaluate() and Worker.evaluateHandle() is that Worker.evaluateHandle() returns JSHandle.

        If the function passed to the Worker.evaluateHandle() returns a Promise, then Worker.evaluateHandle() would wait for the promise to resolve and return its value.

        Parameters:
        expression - JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
        arg - Optional argument to pass to expression.
        Since:
        v1.8
      • url

        java.lang.String url()
        Since:
        v1.8
      • waitForClose

        default Worker waitForClose​(java.lang.Runnable callback)
        Performs action and waits for the Worker to close.
        Parameters:
        callback - Callback that performs the action triggering the event.
        Since:
        v1.10
      • waitForClose

        Worker waitForClose​(Worker.WaitForCloseOptions options,
                            java.lang.Runnable callback)
        Performs action and waits for the Worker to close.
        Parameters:
        callback - Callback that performs the action triggering the event.
        Since:
        v1.10