2020년 1월 22일 수요일

Selenium WebDriver setting


public class LaunchWebDriver {

    private static final Logger logger = LoggerFactory.getLogger(UserPageTest.class);

    WebDriver driver;

    public LaunchWebDriver(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public WebDriver launchWebDriverMode(String headlessOnOff) {

        Path path = Paths.get("");
        String pathStr = path.toAbsolutePath().toString(); 
        logger.debug("pathStr={}", pathStr);

  /* MAC 과 WINDOWS chromedriver 분기 */
        if (isWindows()) {
            System.setProperty("webdriver.chrome.driver", pathStr + "//driver//chromedriver.exe");
        } else if (isMac()) {
            System.setProperty("webdriver.chrome.driver", pathStr + "//driver//chromedriver");
        } else {
            logger.error("지원하지 않는 OS 입니다.");
            return null;
        }

        ChromeOptions co = new ChromeOptions();
        logger.debug("computername={}", System.getenv("computername"));
        /* 공통 ChromeOptions 값. */
        co.addArguments("start-maximized");
        co.addArguments("enable-automation");
        co.addArguments("--disable-gpu");
        if (isWindows() && "NG-BF".equals(System.getenv("computername"))) {
            /* Jenkins 서버(windows)는 항상 headless 모드이다 (java.awt.headless = true) */
            logger.debug("headless Mode : NG-BF is Always headlessMode");
            /* jenkins 에만 추가되는 ChromeOptions 값. */
            co.addArguments("--no-sandbox");

        } else {
            if ("On".equals(headlessOnOff)) { // On / Off
                /* headless chrome */
                co.addArguments("--headless");
   }
        }

        driver = new ChromeDriver(co);

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 기본 대기 시간
        driver.manage().window().maximize();
        return driver;
    }

    public static boolean isWindows() {
        return (OS.isFamilyWindows());
    }
    public static boolean isMac() {
        return (OS.isFamilyMac());
    }
}



public abstract class BaseTest {
    public WebDriver driver;
    public LaunchWebDriver launchWebDriver;

    @BeforeClass
    public void setUp(){
        System.out.println("*** before class ****");
        String headlessOnOff ="On"; // headless = On or Off
        launchWebDriver = new LaunchWebDriver(driver);
        driver = launchWebDriver.launchWebDriverMode(headlessOnOff);
        if(driver == null){
            Assert.assertTrue(false);
        }
    }

    @AfterClass
    public void tearDown(){
        System.out.println("*** after class ****");
        if( driver != null)
            driver.quit();
    }

    @AfterTest
    public void afterTest() throws IOException {
        System.out.println("*** after test ****");
        if( LaunchWebDriver.isWindows() ){
            Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe /T");
        }
    }
}

댓글 없음:

댓글 쓰기