Skip to content

Commit ab82719

Browse files
committed
feat: with pw_codegen()
1 parent d97f840 commit ab82719

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: pw
22
Title: Test your 'golem' Apps with 'Playwright'
3-
Version: 0.0.0.9005
3+
Version: 0.0.0.9006
44
Authors@R:
55
person("Colin", "Fay", , "contact@colinfay.me", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0001-7343-1846"))
@@ -12,6 +12,7 @@ Imports:
1212
fs,
1313
golem,
1414
here,
15+
pkgload,
1516
withr
1617
Encoding: UTF-8
1718
Roxygen: list(markdown = TRUE)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(npx_is_available)
4+
export(pw_codegen)
45
export(pw_init)
56
export(pw_test)
67
export(stop_if_npx_not_available)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pw (development version)
22

3+
* `pw_codegen()` can be called to launch the code generator
4+
35
* `pw_test()` stops if the playwright skeleton is missing
46

57
* Functions to test if npx is in the PATH

R/pw-codegen.R

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,76 @@
1+
#' Run the code generator
2+
#'
3+
#' This function runs the Playwright code generator.
4+
#'
5+
#' @param where The root directory of your golem project
6+
#' @param R_path The path to the R executable
7+
#' @param ... args passed to npx playwright codegen
8+
#'
9+
#' @importFrom withr with_dir
10+
#'
11+
#' @export
112
pw_codegen <- function(
2-
where = golem::get_golem_wd()
13+
where = golem::get_golem_wd(),
14+
R_path = NULL,
15+
...
316
) {
17+
stop_if_npx_not_available()
18+
# This is from golem::expect_running() but that's
19+
# my own code so that's fine
20+
if (Sys.getenv("CALLR_CHILD_R_LIBS_USER") == "") {
21+
pkg_name <- pkgload::pkg_name()
22+
go_for_pkgload <- TRUE
23+
} else {
24+
pkg_name <- Sys.getenv("TESTTHAT_PKG")
25+
go_for_pkgload <- FALSE
26+
}
27+
if (is.null(R_path)) {
28+
if (tolower(.Platform$OS.type) == "windows") {
29+
r_ <- normalizePath(file.path(
30+
Sys.getenv("R_HOME"),
31+
"bin",
32+
"R.exe"
33+
))
34+
} else {
35+
r_ <- normalizePath(file.path(
36+
Sys.getenv("R_HOME"),
37+
"bin",
38+
"R"
39+
))
40+
}
41+
} else {
42+
r_ <- R_path
43+
}
44+
if (go_for_pkgload) {
45+
shinyproc <- processx::process$new(
46+
command = r_,
47+
c(
48+
"-e",
49+
"options(shiny.port = 3000);pkgload::load_all(here::here());run_app()"
50+
)
51+
)
52+
} else {
53+
shinyproc <- processx::process$new(
54+
echo_cmd = TRUE,
55+
command = r_,
56+
c(
57+
"-e",
58+
sprintf(
59+
"options(shiny.port = 3000); library(%s, lib = '%s');run_app()",
60+
pkg_name,
61+
.libPaths()
62+
)
63+
),
64+
stdout = "|",
65+
stderr = "|"
66+
)
67+
}
68+
69+
if (!shinyproc$is_alive()) {
70+
cli::cli_alert_danger("Failed to start the app, please check the logs")
71+
cat(shinyproc$read_error())
72+
stop()
73+
}
474
with_dir(
575
where,
676
{
@@ -10,11 +80,31 @@ pw_codegen <- function(
1080
with_dir(
1181
"playwright",
1282
{
83+
output_file <- file.path(
84+
"tests",
85+
sprintf(
86+
"%s.test.ts",
87+
uuid::UUIDgenerate()
88+
)
89+
)
90+
fs::file_create(output_file)
91+
output_file <- fs::path_abs(output_file)
92+
on.exit({
93+
cat(
94+
"Code generation done, the file is available at:",
95+
output_file,
96+
"\n"
97+
)
98+
})
1399
system2(
14100
"npx",
15101
c(
16102
"playwright",
17-
"codegen"
103+
"codegen",
104+
"http://localhost:3000",
105+
"--output",
106+
output_file,
107+
...
18108
)
19109
)
20110
}

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ npx playwright test
6565

6666
## Roadmap
6767

68-
- [ ] Add support for the Playwright codegen toolkit.
6968
- [ ] Enable viewing of test reports directly.
7069

7170
**Note:** The full Playwright Node.js API will not be wrapped in R. For advanced use cases, you may need to run commands directly from the terminal.

man/pw_codegen.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)