-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mechanical-orchard/implement-keyboard
Implement basic versions of Page.Keyboard
- Loading branch information
Showing
7 changed files
with
139 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
defmodule Playwright.Keyboard do | ||
defmodule Playwright.Page.Keyboard do | ||
@moduledoc false | ||
|
||
# @spec down(t(), binary()) :: :ok | ||
# def down(keyboard, key) | ||
use Playwright.SDK.ChannelOwner | ||
alias Playwright.Page | ||
|
||
# @spec insert_text(t(), binary()) :: :ok | ||
# def insert_text(keyboard, text) | ||
# API | ||
# --------------------------------------------------------------------------- | ||
|
||
# @spec press(t(), binary(), options()) :: :ok | ||
# def press(keyboard, key, options \\ %{}) | ||
@spec down(Page.t(), binary()) :: :ok | ||
def down(page, key) do | ||
channel_post(page, :keyboard_down, %{key: key}) | ||
end | ||
|
||
# @spec type(t(), binary(), options()) :: :ok | ||
# def type(keyboard, text, options \\ %{}) | ||
@spec insert_text(Page.t(), binary()) :: :ok | ||
def insert_text(page, text) do | ||
channel_post(page, :keyboard_type, %{text: text}) | ||
end | ||
|
||
# @spec up(t(), binary()) :: :ok | ||
# def up(keyboard, key) | ||
@spec press(Page.t(), binary()) :: :ok | ||
def press(page, key) do | ||
channel_post(page, :keyboard_press, %{key: key}) | ||
end | ||
|
||
@spec type(Page.t(), binary()) :: :ok | ||
def type(page, text) do | ||
channel_post(page, :keyboard_type, %{text: text}) | ||
end | ||
|
||
@spec up(Page.t(), binary()) :: :ok | ||
def up(page, key) do | ||
channel_post(page, :keyboard_up, %{key: key}) | ||
end | ||
|
||
# private | ||
# --------------------------------------------------------------------------- | ||
|
||
defp channel_post(%Page{} = page, action, data) do | ||
Channel.post(page.session, {:guid, page.guid}, action, data) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule Playwright.Page.KeyboardTest do | ||
use Playwright.TestCase, async: true | ||
|
||
alias Playwright.Page | ||
alias Playwright.Page.Keyboard | ||
|
||
describe "type" do | ||
test "keyboard type into a textbox", %{page: page} do | ||
Page.evaluate(page, """ | ||
const textarea = document.createElement('textarea'); | ||
document.body.appendChild(textarea); | ||
textarea.focus(); | ||
""") | ||
|
||
text = "Hello world. I am the text that was typed!" | ||
|
||
Keyboard.type(page, text) | ||
|
||
assert Page.evaluate(page, ~s[document.querySelector("textarea").value]) == text | ||
end | ||
end | ||
|
||
describe "insert_text" do | ||
test "should send characters inserted", %{page: page} do | ||
Page.evaluate(page, """ | ||
const textarea = document.createElement('textarea'); | ||
document.body.appendChild(textarea); | ||
textarea.focus(); | ||
""") | ||
|
||
text = "Hello world. I am the text that was typed!" | ||
|
||
Keyboard.insert_text(page, text) | ||
|
||
assert Page.evaluate(page, ~s[document.querySelector("textarea").value]) == text | ||
end | ||
end | ||
|
||
describe "up" do | ||
test "sends proper code", %{page: page, assets: assets} do | ||
Page.goto(page, assets.prefix <> "/input/keyboard.html") | ||
|
||
Keyboard.up(page, ";") | ||
|
||
assert Page.evaluate(page, "() => getResult()") == | ||
"Keyup: ; Semicolon 186 []" | ||
end | ||
end | ||
|
||
describe "down" do | ||
test "sends proper code", %{page: page, assets: assets} do | ||
Page.goto(page, assets.prefix <> "/input/keyboard.html") | ||
|
||
Keyboard.down(page, "Control") | ||
|
||
assert Page.evaluate(page, "() => getResult()") == | ||
"Keydown: Control ControlLeft 17 [Control]" | ||
end | ||
end | ||
|
||
describe "press" do | ||
test "test should press plus", %{page: page, assets: assets} do | ||
Page.goto(page, assets.prefix <> "/input/keyboard.html") | ||
|
||
Keyboard.press(page, "+") | ||
|
||
assert Page.evaluate(page, "() => getResult()") == | ||
[ | ||
# 192 is ` keyCode | ||
"Keydown: + Equal 187 []", | ||
# 126 is ~ charCode | ||
"Keypress: + Equal 43 43 []", | ||
"Keyup: + Equal 187 []" | ||
] | ||
|> Enum.join("\n") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters