본문 바로가기

개발 공부

Selenium 자바스크립트 테스트 코드

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import static org.junit.Assert.assertEquals;

@Test
public class TestCode {
    public void testJavaScriptCalls() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        JavascriptExecutor js = (JavascriptExecutor) driver;
        String title = (String) js.executeAsyncScript("return document.title");
        assertEquals("Google", title);
        long links = (Long) js.executeScript("var links = document.getElementsByTagName('A'); return links.length");
        assertEquals(42, links);
        driver.close();
    }
}