File tree Expand file tree Collapse file tree
src/main/java/io/swaglabs/portal/qa/browsermanager Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package io .swaglabs .portal .qa .browsermanager ;
22
3+ import io .swaglabs .portal .qa .constants .WebPortalConstants ;
4+
5+ import java .util .HashMap ;
6+ import java .util .Map ;
7+
38public class BrowserFactory {
49
5- public IBrowser createBrowser (String browserName ) {
6- return switch (BrowserName .fromString (browserName )) {
7- case FIREFOX -> new FirefoxBrowser ();
8- case WEBKIT -> new WebkitBrowser ();
9- case MS_EDGE -> new MsEdgeBrowser ();
10- default -> new ChromeBrowser ();
11- };
10+ // Cache browser instances per thread
11+ private static final ThreadLocal <Map <String , IBrowser >> BROWSER_CACHE = ThreadLocal .withInitial (HashMap ::new );
12+
13+ public IBrowser createBrowser () {
14+ return BROWSER_CACHE .get ().computeIfAbsent (WebPortalConstants .BROWSER ,
15+ name -> switch (BrowserName .fromString (name )) {
16+ case MS_EDGE -> new MsEdgeBrowser ();
17+ case FIREFOX -> new FirefoxBrowser ();
18+ case WEBKIT -> new WebkitBrowser ();
19+ default -> new ChromeBrowser ();
20+ });
1221 }
1322}
Original file line number Diff line number Diff line change @@ -11,10 +11,8 @@ public class BrowserManager implements IBrowserManager<Page> {
1111
1212 @ Override
1313 public Page getBrowserPage (Playwright playwright ) {
14- String browserName = WebPortalConstants .BROWSER ;
15- String runMode = WebPortalConstants .RUN_MODE ;
16- boolean isHeadless = runMode .equals ("headless" );
17- BrowserContext browserContext = new BrowserFactory ().createBrowser (browserName ).createSession (playwright , isHeadless );
14+ boolean isHeadless = "headless" .equals (WebPortalConstants .RUN_MODE );
15+ BrowserContext browserContext = new BrowserFactory ().createBrowser ().createSession (playwright , isHeadless );
1816 Objects .requireNonNull (browserContext , "Playwright Browser Context is null!" );
1917 return browserContext .newPage ();
2018 }
Original file line number Diff line number Diff line change 66import com .microsoft .playwright .Playwright ;
77import io .swaglabs .portal .qa .constants .WebPortalConstants ;
88
9+ import java .util .List ;
10+
911public class ChromeBrowser implements IBrowser {
1012
1113 @ Override
1214 public BrowserContext createSession (Playwright playwright , boolean isHeadless ) {
13- return playwright .chromium ().launch (new BrowserType .LaunchOptions ()
14- .setHeadless (isHeadless ))
15- .newContext (new Browser .NewContextOptions ()
16- .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
17- .setTimezoneId (WebPortalConstants .TIME_ZONE ));
15+ BrowserType .LaunchOptions chromeOptions = new BrowserType .LaunchOptions ()
16+ .setHeadless (isHeadless )
17+ .setArgs (List .of (
18+ "--disable-gpu" ,
19+ "--disable-dev-shm-usage" , // Important for Docker/CI
20+ "--no-sandbox" ,
21+ "--disable-setuid-sandbox" ,
22+ "--disable-software-rasterizer" ,
23+ "--disable-notifications"
24+ ));
25+ return playwright .chromium ().launch (chromeOptions ).newContext (new Browser .NewContextOptions ()
26+ .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
27+ .setTimezoneId (WebPortalConstants .TIME_ZONE ));
1828 }
1929}
Original file line number Diff line number Diff line change 66import com .microsoft .playwright .Playwright ;
77import io .swaglabs .portal .qa .constants .WebPortalConstants ;
88
9+ import java .util .List ;
10+
911public class FirefoxBrowser implements IBrowser {
1012 @ Override
1113 public BrowserContext createSession (Playwright playwright , boolean isHeadless ) {
12- return playwright .firefox ().launch (new BrowserType .LaunchOptions ()
13- .setHeadless (isHeadless ))
14- .newContext (new Browser .NewContextOptions ()
15- .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
16- .setTimezoneId (WebPortalConstants .TIME_ZONE ));
14+ BrowserType .LaunchOptions fireOptions = new BrowserType .LaunchOptions ()
15+ .setHeadless (isHeadless )
16+ .setArgs (List .of (
17+ "--disable-gpu" ,
18+ "--no-sandbox"
19+ ));
20+ return playwright .firefox ().launch (fireOptions ).newContext (new Browser .NewContextOptions ()
21+ .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
22+ .setTimezoneId (WebPortalConstants .TIME_ZONE ));
1723 }
1824}
Original file line number Diff line number Diff line change 66import com .microsoft .playwright .Playwright ;
77import io .swaglabs .portal .qa .constants .WebPortalConstants ;
88
9+ import java .util .List ;
10+
911public class MsEdgeBrowser implements IBrowser {
1012 @ Override
1113 public BrowserContext createSession (Playwright playwright , boolean isHeadless ) {
12- return playwright .chromium ().launch (new BrowserType .LaunchOptions ()
13- .setHeadless (isHeadless ).setChannel (BrowserName .MS_EDGE .getBrowserType ()))
14- .newContext (new Browser .NewContextOptions ()
15- .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
16- .setTimezoneId (WebPortalConstants .TIME_ZONE ));
14+ BrowserType .LaunchOptions edgeOptions = new BrowserType .LaunchOptions ()
15+ .setHeadless (isHeadless )
16+ .setChannel (BrowserName .MS_EDGE .getBrowserType ())
17+ .setArgs (List .of (
18+ "--disable-gpu" ,
19+ "--disable-dev-shm-usage" ,
20+ "--no-sandbox"
21+ ));
22+ return playwright .chromium ().launch (edgeOptions ).newContext (new Browser .NewContextOptions ()
23+ .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
24+ .setTimezoneId (WebPortalConstants .TIME_ZONE ));
1725 }
1826}
Original file line number Diff line number Diff line change 66import com .microsoft .playwright .Playwright ;
77import io .swaglabs .portal .qa .constants .WebPortalConstants ;
88
9+ import java .util .List ;
10+
911public class WebkitBrowser implements IBrowser {
1012 @ Override
1113 public BrowserContext createSession (Playwright playwright , boolean isHeadless ) {
12- return playwright .webkit ().launch (new BrowserType .LaunchOptions ()
13- .setHeadless (isHeadless ))
14- .newContext (new Browser .NewContextOptions ()
15- .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
16- .setTimezoneId (WebPortalConstants .TIME_ZONE ));
14+ BrowserType .LaunchOptions webkitOptions = new BrowserType .LaunchOptions ()
15+ .setHeadless (isHeadless )
16+ .setArgs (List .of (
17+ "--disable-gpu" ,
18+ "--no-sandbox"
19+ ));
20+ return playwright .webkit ().launch (webkitOptions ).newContext (new Browser .NewContextOptions ()
21+ .setViewportSize (WebPortalConstants .SCREEN_WIDTH , WebPortalConstants .SCREEN_HEIGHT )
22+ .setTimezoneId (WebPortalConstants .TIME_ZONE ));
1723 }
1824}
You can’t perform that action at this time.
0 commit comments