| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('browser_dom() executes JS, returns rendered DOM, and strips scripts', { |
| 4 |
f = tempfile(fileext = '.html') |
| 5 |
on.exit(unlink(f), add = TRUE) |
| 6 |
writeLines(c( |
| 7 |
'<html><body><div id="out"></div>', |
| 8 |
'<script>document.getElementById("out").textContent = 1 + 1;</script>', |
| 9 |
'</body></html>' |
| 10 |
), f) |
| 11 |
(unclass(browser_dom(f)) %==% '<html><head></head><body><div id="out">2</div></body></html>') |
| 12 |
}) |
| 13 |
|
| 14 |
assert('browser_dom() saves to output file', { |
| 15 |
f = tempfile(fileext = '.html') |
| 16 |
out = tempfile(fileext = '.html') |
| 17 |
on.exit(unlink(c(f, out)), add = TRUE) |
| 18 |
writeLines(c( |
| 19 |
'<html><body><p>static</p>', |
| 20 |
'<script>\ndocument.body.insertAdjacentHTML("beforeend", "<p>" + (2+3) + "</p>")</script>', |
| 21 |
'</body></html>' |
| 22 |
), f) |
| 23 |
res = browser_dom(f, out) |
| 24 |
(res %==% out) |
| 25 |
(unclass(file_string(out)) %==% '<html><head></head><body><p>static</p><p>5</p>\n\n</body></html>') |
| 26 |
}) |
| 27 |
|
| 28 |
assert('browser_dom(fragment = TRUE) returns body content only', { |
| 29 |
f = tempfile(fileext = '.html') |
| 30 |
on.exit(unlink(f), add = TRUE) |
| 31 |
writeLines('<html><head><title>test</title></head>\n<body>\n <p>content\n</p>\t\n</body></html>', f) |
| 32 |
res = browser_dom(f, fragment = TRUE) |
| 33 |
(unclass(res) %==% '<p>content\n</p>') |
| 34 |
}) |