์น ๋ธ๋ผ์ฐ์ ์์๋ ๋ชจ๋ฐ์ผ ํ๊ฒฝ์ฒ๋ผ ํ ์คํธ๋ฅผ ์งํํ ์ ์๋ค. ๊ฐ๋ฐ์๋๊ตฌ์๋ ๋ค์ํ ๊ธฐ๋ฅ์ด ํ์ฌ๋์ด ์๋ค. ์ค์ ๋จ๋ง ์์ด๋ ๋ธ๋ผ์ฐ์ ์์ ์ค๋งํธํฐ ๋จ๋ง์ ํ๊ฒฝ์ ํ ์คํธํด ๋ณผ ์ ์๋ค. ๋ฌผ๋ก ์ฑ ํํ์ ์๋น์ค๋ผ๋ฉด ์ด ๋ฐฉ๋ฒ์ผ๋ก๋ ๋ถ๊ฐ๋ฅํ๋ค. ์น ํํ์ ์๋น์ค์์๋ ์ ์ฉํด ๋ณผ ์ ์๋ค.
Chrome์์ ๋ชจ๋ฐ์ผ ์ ๋ฎฌ๋ ์ด์ ์ฌ์ฉ
๋ชจ๋ฐ์ผ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ํ ์คํธ๋ฅผ ์ํ ๋ชจ๋ฐ์ผ ์ ๋ฎฌ๋ ์ด์ ์ฌ์ฉ๋ฒ
F12๋ก ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ผ ๋ค. ๊ฐ๋ฐ์ ๋๊ตฌ ์ข์ธก ์๋จ์ ๋ชจ๋ฐ์ผ๊ธฐ๊ธฐ ์์ด์ฝ์ ํด๋ฆญํ๋ค.
์๋จ์ ์ต์ ๋ค์ ๋ณ๊ฒฝํด๋ณด๋ฉด์ ํ ์คํธํ ์ ์๋ค. ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๊ฐ๋ฐ์์ ํ ์คํฐ๋ ์ค์ ๊ธฐ๊ธฐ ์์ด๋ ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ์น์ฌ์ดํธ๊ฐ ์ด๋ป๊ฒ ๋ณด์ด๋์ง ํ์ธ ๊ฐ๋ฅํ๋ค.
![](https://blog.kakaocdn.net/dn/udex5/btsa9zYhZTj/TVzgYEPff4YkkeaPJMLXZ0/img.png)
์ ๋ ๋์์์ ๋ชจ๋ฐ์ผ ์๋ฎฌ๋ ์ดํฐ ํ๊ฒฝ ํ ์คํธ
ChromeOptions ์ค์ ์ผ๋ก ์ ๋ ๋์ ์น๋๋ผ์ด๋ฒ ํ ์คํธ์์๋ ๋ชจ๋ฐ์ผ ์๋ฎฌ๋ ์ด์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค. ์๋๋ ๊ฒ์ ์์ ํ ์คํธ๋ฅผ ๊ตฌ๊ธ ํฝ์ 2์์ ์คํํ๋๋ก ํ๋ ์ฝ๋์ด๋ค.
package com.example;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import java.util.HashMap;
import java.util.Map;
public class ChromeHeadlessTest {
WebDriver driver;
@Test
public void f() {
//ํ
์คํธํ ์ฝ๋
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/drivers/chromedriver");
// ๋๋น์ ๋์ด ๊ฐ์ ๊ธฐ๊ธฐ ์ ๋ณด๋ฅผ ์ค์ ํ๋ค.
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 411);
deviceMetrics.put("heght", 823);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 8.0.0;" +
"Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/67.0.3396.99 Mobile Safari/537.36");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(chromeOptions);
driver.get("http://www.naver.com/");
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
}
๋ค์ด๋ฒํ์ ๋ชจ๋ฐ์ผ ์ ๋ฎฌ๋ ์ดํฐ๋ก ์ง์ ํ๋ ๋ชจ๋ฐ์ผurl๋ก ์ฐ๊ฒฐ๋๋ค. (m.naver.com)
๊ทธ๋์ ๋ค์ ์๋ฆฌ๋จผํธid๋ฅผ ๊ตฌํด์ค์ผ ํ๋ค. ๋, ๋ค์ด๋ฒ๋ชจ๋ฐ์ผ์ ๊ฒ์์ฐฝ์ ํ๋ฒ ํด๋ฆญํ ๋ค์์ ๊ฒ์์ด ๊ฐ๋ฅํ๋ค. ์ด๋ฐ ๋ถ๋ถ๋ค์ ๋ง์ถฐ ์คํฌ๋ฆฝํธ๋ ๋ค์ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
๊ฐ๋ฐ์ ๋๊ตฌ > Network ํญ์์ ์ํ๋ ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์ userAgent ๋ฌธ์์ด์ ํ์ธํ ์ ์๋ค. ์๋๋ ์์ดํฐ12pro์ด๋ค.