Advertisement
Guest User

BaseTest

a guest
Nov 14th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | Software | 0 0
  1. package com.example.applications;
  2.  
  3. import com.microsoft.playwright.*;
  4. import com.example.config.ConfigurationManager;
  5. import org.junit.jupiter.api.AfterEach;
  6. import org.junit.jupiter.api.BeforeEach;
  7.  
  8. public abstract class BaseTest {
  9.     protected Playwright playwright;
  10.     protected Browser browser;
  11.     protected Page page;
  12.  
  13.     @BeforeEach
  14.     void setUp() {
  15.         playwright = Playwright.create();
  16.  
  17.         String browserType = System.getProperty("browserType", "chromium");
  18.         BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions().setHeadless(false);
  19.  
  20.         // Check if SSL errors should be ignored from properties file
  21.         boolean ignoreSSL = Boolean.parseBoolean(ConfigurationManager.getProperty("ignoreSSLErrors"));
  22.  
  23.         switch (browserType) {
  24.             case "firefox":
  25.                 browser = playwright.firefox().launch(launchOptions);
  26.                 break;
  27.             case "webkit":
  28.                 browser = playwright.webkit().launch(launchOptions);
  29.                 break;
  30.             case "chromium": // default
  31.             default:
  32.                 browser = playwright.chromium().launch(launchOptions);
  33.         }
  34.  
  35.         Browser.NewContextOptions contextOptions = new Browser.NewContextOptions().setIgnoreHTTPSErrors(ignoreSSL);
  36.         BrowserContext context = browser.newContext(contextOptions); // Create the browser context
  37.  
  38.         page = context.newPage(); // Create a new page
  39.         page.navigate(getAppUrl()); // Navigate to the application URL
  40.     }
  41.  
  42.     protected abstract String getAppUrl();
  43.  
  44.     @AfterEach
  45.     void tearDown() {
  46.         if (page != null) {
  47.             page.close();
  48.         }
  49.         if (browser != null) {
  50.             browser.close();
  51.         }
  52.         if (playwright != null) {
  53.             playwright.close();
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement