Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xpath argument to LiveHTML click() method #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Config/testthat/parallel: true
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
25 changes: 19 additions & 6 deletions R/live.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,19 @@ LiveHTML <- R6::R6Class(


#' @description Simulate a click on an HTML element.
#' @param css CSS selector or xpath expression.
#' @param css,xpath CSS selector or xpath expression.
#' @param n_clicks Number of clicks
click = function(css, n_clicks = 1) {
click = function(css, xpath, n_clicks = 1) {
private$check_active()
check_exclusive(css, xpath)

check_number_whole(n_clicks, min = 1)

# Implementation based on puppeteer as described in
# https://medium.com/@aslushnikov/automating-clicks-in-chromium-a50e7f01d3fb
# With code from https://github.com/puppeteer/puppeteer/blob/b53de4e0942e93c/packages/puppeteer-core/src/cdp/Input.ts#L431-L459

node <- private$wait_for_selector(css)
node <- private$wait_for_selector(css, xpath)
self$session$DOM$scrollIntoViewIfNeeded(node)

# Quad = location of four corners (x1, y1, x2, y2, x3, y3, x4, y4)
Expand Down Expand Up @@ -277,17 +279,28 @@ LiveHTML <- R6::R6Class(
}
},

wait_for_selector = function(css, timeout = 5) {
wait_for_selector = function(css, xpath, timeout = 5) {
check_exclusive(css, xpath)

done <- now() + timeout
while(now() < done) {
nodes <- private$find_nodes(css)
if (!missing(css)) {
nodes <- private$find_nodes(css = css)
} else {
nodes <- private$find_nodes(xpath = xpath)
}
if (length(nodes) > 0) {
return(nodes)
}

Sys.sleep(0.1)
}
cli::cli_abort("Failed to find selector {.str {css}} in {timeout} seconds.")

if(!missing(css)){
cli::cli_abort("Failed to find selector {.str {css}} in {timeout} seconds.")
} else {
cli::cli_abort("Failed to find xpath {.str {xpath}} in {timeout} seconds.")
}
},

find_nodes = function(css, xpath) {
Expand Down
4 changes: 2 additions & 2 deletions man/LiveHTML.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions tests/testthat/test-live.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ test_that("can click a button", {
skip_if_no_chromote()

sess <- read_html_live(html_test_path("click"))
sess$click("button")
sess$click(css = "button")
expect_equal(html_text(html_element(sess, "p")), "clicked")

sess$click("button", 2)
sess <- read_html_live(html_test_path("click"))
sess$click(xpath = "//button")
expect_equal(html_text(html_element(sess, "p")), "clicked")

sess$click(css = "button", n_clicks = 2)
expect_equal(html_text(html_element(sess, "p")), "double clicked")
})

Expand Down
Loading