엄지월드

Chrome Headless 본문

QA/Groovy

Chrome Headless

킨글 2021. 8. 15. 22:34
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.chrome.ChromeDriver as ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions as ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities as DesiredCapabilities
import org.openqa.selenium.remote.CapabilityType as CapabilityType
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType as WebUIDriverType
import com.kms.katalon.core.webui.driver.ExistingRemoteWebDriver as ExistingRemoteWebDriver
import org.openqa.selenium.chrome.ChromeDriverService as ChromeDriverService
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.HttpClient
import org.apache.http.impl.client.HttpClientBuilder
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity

/*
 * Chrome default download path change!
 */
WebUIDriverType BrowserRunType = DriverFactory.getExecutedBrowser()

String str_BrowserRunType = BrowserRunType

//실행 브라우저가 chrome이 아닐경우 종료
if (!((str_BrowserRunType == 'Chrome') || (str_BrowserRunType == 'Chrome (headless)'))) {
    WebUI.comment("${str_BrowserRunType}으로는 실행할 수 없습니다. Chrome, Chrome headless 로 실행하세요.")

    CustomKeywords.'userdefinepkg.forcestop.FORCESTOP'()
}

'크롬 다운로드 경로 변경해서 실행'
System.setProperty('webdriver.chrome.driver', DriverFactory.getChromeDriverPath())

String downloadFilepath = System.getProperty('user.dir') + '\\Data Files\\download\\'

HashMap<String, Object> chromePrefs = new HashMap<String, Object>()

chromePrefs.put('profile.default_content_settings.popups', 0)
chromePrefs.put('download.default_directory', downloadFilepath)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("download.directory_upgrade", true)
chromePrefs.put("safebrowsing.enabled", true)
chromePrefs.put('plugins.always_open_pdf_externally', true)
chromePrefs.put('cmd', 'Page.setDownloadBehavior')
chromePrefs.put('behavior', 'allow')
chromePrefs.put('plugins.plugins_disabled', ["Chrome PDF Viewer"])


ChromeOptions options = new ChromeOptions()

options.setExperimentalOption('prefs', chromePrefs)

//options.addArguments("--disable-notifications")
options.addArguments("--window-size=1920x1280")
options.addArguments("--no-sandbox")
options.addArguments("--test-type")
options.addArguments("--disable-extensions")
//options.addArguments("--disable-infobars")
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true)

//실행타입이 headless 모드일 경우 옵션
if (str_BrowserRunType == 'Chrome (headless)') {
    //WebUI.comment("${a}")
    options.addArguments("--headless")
}

ChromeDriverService driverService = ChromeDriverService.createDefaultService()

ChromeDriver driver = new ChromeDriver(driverService, options)

Map<String, Object> commandParams = new HashMap<>()
commandParams.put("cmd", "Page.setDownloadBehavior")

Map<String, String> params = new HashMap<>()
params.put("behavior", "allow")
params.put("downloadPath", downloadFilepath)
commandParams.put("params", params)

ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();

String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command"

//println u

String command = null
try {
    command = objectMapper.writeValueAsString(commandParams)
} catch(Exception e)
{
    WebUI.comment("Error : ${e}")
    
    CustomKeywords.'userdefinepkg.forcestop.FORCESTOP'()
}

HttpPost request = new HttpPost(u)
request.addHeader("content-type", "application/json")
request.setEntity(new StringEntity(command))
httpClient.execute(request)
    
DriverFactory.changeWebDriver(driver)

'QA > Groovy' 카테고리의 다른 글

API Call  (0) 2021.08.15
Groovy 정규식 사용  (0) 2019.11.20
Comments