skip to Main Content

When using playwright-java https://github.com/microsoft/playwright-java
in Java/Spring

  @PostConstruct
  void init(){
    LOGGER.info("Initializinig Playwright with Chromium ...");
    Playwright playwright = Playwright.create();
    browser = playwright.chromium().launch(); // Page.pdf only supported in headless Chromium
  }

I get Exception (See log below). I understand that I need to find a way to look inside running container temp .sh file, I would expect Java app using playwright-java running inside microsoft.com/playwright/java container (of the same v1.35.0) should just work.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'playwrightPdfConverterService': Invocation of init method failed; nested exception is java.lang.RuntimeException: Failed to create driver
2023-06-14T15:05:31.174236127Z  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
2023-06-14T15:05:31.174238335Z  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440)
2023-06-14T15:05:31.174240377Z  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
2023-06-14T15:05:31.174242293Z  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
2023-06-14T15:05:31.174244168Z  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
2023-06-14T15:05:31.174246085Z  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
2023-06-14T15:05:31.174250960Z  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
2023-06-14T15:05:31.174286252Z  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
2023-06-14T15:05:31.174289335Z  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
2023-06-14T15:05:31.174291252Z  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
2023-06-14T15:05:31.174293168Z  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
2023-06-14T15:05:31.174296127Z  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
2023-06-14T15:05:31.174298168Z  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
2023-06-14T15:05:31.174300085Z  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
2023-06-14T15:05:31.174302293Z  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
2023-06-14T15:05:31.174304168Z  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
2023-06-14T15:05:31.174306002Z  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
2023-06-14T15:05:31.174307793Z  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
2023-06-14T15:05:31.174309543Z  at com.company.pdf.Application.main(Application.java:12)
2023-06-14T15:05:31.174311293Z  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2023-06-14T15:05:31.174313085Z  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2023-06-14T15:05:31.174314918Z  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2023-06-14T15:05:31.174316752Z  at java.base/java.lang.reflect.Method.invoke(Method.java:568)
2023-06-14T15:05:31.174318460Z  at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
2023-06-14T15:05:31.174320293Z  at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
2023-06-14T15:05:31.174322085Z  at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
2023-06-14T15:05:31.174323793Z  at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
2023-06-14T15:05:31.174325585Z Caused by: java.lang.RuntimeException: Failed to create driver
2023-06-14T15:05:31.174327293Z  at com.microsoft.playwright.impl.driver.Driver.createAndInstall(Driver.java:105)
2023-06-14T15:05:31.174330835Z  at com.microsoft.playwright.impl.driver.Driver.ensureDriverInstalled(Driver.java:57)
2023-06-14T15:05:31.174334460Z  at com.microsoft.playwright.impl.PlaywrightImpl.createImpl(PlaywrightImpl.java:45)
2023-06-14T15:05:31.174340127Z  at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:35)
2023-06-14T15:05:31.174343460Z  at com.microsoft.playwright.Playwright.create(Playwright.java:110)
2023-06-14T15:05:31.174345335Z  at com.microsoft.playwright.Playwright.create(Playwright.java:114)
2023-06-14T15:05:31.174347085Z  at com.company.pdf.converter.PlaywrightPdfConverterService.init(PlaywrightPdfConverterService.java:29)
2023-06-14T15:05:31.174348835Z  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2023-06-14T15:05:31.174429543Z  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2023-06-14T15:05:31.174448960Z  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2023-06-14T15:05:31.174451293Z  at java.base/java.lang.reflect.Method.invoke(Method.java:568)
2023-06-14T15:05:31.174453127Z  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
2023-06-14T15:05:31.174455127Z  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
2023-06-14T15:05:31.174457127Z  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
2023-06-14T15:05:31.174458960Z  ... 26 common frames omitted
2023-06-14T15:05:31.174460460Z Caused by: java.io.IOException: Cannot run program "/tmp/playwright-java-7667674547892745859/playwright.sh": error=0, Failed to exec spawn helper: pid: 36, exit value: 1
2023-06-14T15:05:31.174462377Z  at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
2023-06-14T15:05:31.174464043Z  at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
2023-06-14T15:05:31.174465585Z  at com.microsoft.playwright.impl.driver.jar.DriverJar.installBrowsers(DriverJar.java:93)
2023-06-14T15:05:31.174467252Z  at com.microsoft.playwright.impl.driver.jar.DriverJar.initialize(DriverJar.java:69)
2023-06-14T15:05:31.174468877Z  at com.microsoft.playwright.impl.driver.Driver.initialize(Driver.java:64)
2023-06-14T15:05:31.174472752Z  at com.microsoft.playwright.impl.driver.Driver.createAndInstall(Driver.java:101)
2023-06-14T15:05:31.174475377Z  ... 39 common frames omitted
2023-06-14T15:05:31.174477002Z Caused by: java.io.IOException: error=0, Failed to exec spawn helper: pid: 36, exit value: 1
2023-06-14T15:05:31.174478668Z  at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
2023-06-14T15:05:31.174480335Z  at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
2023-06-14T15:05:31.174482127Z  at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
2023-06-14T15:05:31.174496252Z  at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
2023-06-14T15:05:31.174498960Z  ... 44 common frames omitted
2023-06-14T15:05:31.174500585Z 

Dockerfile

FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-amd64
# Ubuntu 22.04-based image with java 17, mvn 3.8.8
# and chromium-1064  ffmpeg-1009  firefox-1408  webkit-1848
# See https://github.com/microsoft/playwright-java/blob/main/utils/docker/Dockerfile.jammy
# For tags and available platform see
# https://mcr.microsoft.com/en-us/product/playwright/java/tags

WORKDIR /opt

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} ./company-pdf-service.jar



# run server
EXPOSE 8300
ENTRYPOINT ["java","-jar","./company-pdf-service.jar"]

2

Answers


  1. Chosen as BEST ANSWER

    The problem was because I was trying to run linux/amd64 images on M1 mac (Apple MacBook with ARM-based Apple Silicon).

    Once switchedc to run everything as ARM architecture: linux/arm64, the problem went away. The exact cause is unclear, but most likely pure JDK issue.

    Some hints for using docker images:

    Having envvar DEBUG=pw:install gives more Playwright logging related to browser install and connection.

    ARG BASE_IMAGE_ARCH=amd64
    FROM mcr.microsoft.com/playwright/java:v1.35.0-jammy-${BASE_IMAGE_ARCH}
    
    ...
    
    ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=true
    ENV DEBUG=pw:install
    
    

    With such Dockerfile it is still easy to try both arm64 and amd64


  2. please refer to this framework: https://github.com/susnigdha1/PlaywrightTestAutomationFramework, I have added example of how to use Springboot, Cucumber, Junit5, Maven and Docker.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search