Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new manager to interact with WebStorage (#29)
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "../spec_helper" | ||
|
||
module Selenium::Command | ||
describe "WebStorageManager", tags: "feature" do | ||
it "works for localStorage" do | ||
TestServer.route "/home", "<h1>The Title</h1>" | ||
|
||
with_session do |session| | ||
session.navigate_to("http://localhost:3002/home") | ||
local_storage_manager = session.local_storage_manager | ||
|
||
local_storage_manager.item("foo", "barr") | ||
item = local_storage_manager.item("foo") | ||
item.should eq("barr") | ||
|
||
local_storage_manager.size.should eq(1) | ||
|
||
local_storage_manager.item("fizz", "buzz") | ||
item = local_storage_manager.item("fizz") | ||
item.should eq("buzz") | ||
|
||
local_storage_manager.size.should eq(2) | ||
|
||
local_storage_manager.item("foo", "bazz") | ||
item = local_storage_manager.item("foo") | ||
item.should eq("bazz") | ||
|
||
names = local_storage_manager.keys | ||
names.should eq(["foo", "fizz"]) | ||
|
||
local_storage_manager.clear | ||
local_storage_manager.size.should eq(0) | ||
end | ||
end | ||
|
||
it "works for sessionStorage" do | ||
TestServer.route "/home", "<h1>The Title</h1>" | ||
|
||
with_session do |session| | ||
session.navigate_to("http://localhost:3002/home") | ||
session_storage_manager = session.session_storage_manager | ||
|
||
session_storage_manager.item("foo", "barr") | ||
item = session_storage_manager.item("foo") | ||
item.should eq("barr") | ||
|
||
session_storage_manager.size.should eq(1) | ||
|
||
session_storage_manager.item("fizz", "buzz") | ||
item = session_storage_manager.item("fizz") | ||
item.should eq("buzz") | ||
|
||
session_storage_manager.size.should eq(2) | ||
|
||
session_storage_manager.item("foo", "bazz") | ||
item = session_storage_manager.item("foo") | ||
item.should eq("bazz") | ||
|
||
names = session_storage_manager.keys | ||
names.should eq(["foo", "fizz"]) | ||
|
||
session_storage_manager.clear | ||
session_storage_manager.size.should eq(0) | ||
end | ||
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
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,50 @@ | ||
class Selenium::WebStorageManager | ||
enum StorageType | ||
LocalStorage | ||
SessionStorage | ||
end | ||
|
||
getter document_manager : DocumentManager | ||
|
||
forward_missing_to @document_manager | ||
|
||
def initialize(@document_manager, @storage : StorageType = StorageType::LocalStorage) | ||
end | ||
|
||
# Clear all items from the Storage | ||
def clear | ||
document_manager.execute_script("#{storage_type_in_js}.clear()") | ||
end | ||
|
||
# Retrieve and insert item from/into the Storage. | ||
# If no value is given, it will return the item specified in `name`. | ||
# For a given `value`, it will be added or updated for the item specified in `name`. | ||
def item(name : String, value = nil) : String | Nil | ||
if value.nil? | ||
document_manager.execute_script("return #{storage_type_in_js}.getItem('#{name}')") | ||
else | ||
document_manager.execute_script("#{storage_type_in_js}.setItem('#{name}', '#{value}')") | ||
end | ||
end | ||
|
||
# Retrieve the list of keys from the Storage | ||
def keys : JSON::Any | ||
str = document_manager.execute_script("return Object.keys(#{storage_type_in_js})") | ||
JSON.parse(str) | ||
end | ||
|
||
# Remove the item specified in `name` from the Storage | ||
def remove(name : String) | ||
document_manager.execute_script("#{storage_type_in_js}.removeItem('#{name}')") | ||
end | ||
|
||
# Retrieve the number of items in the Storage | ||
def size : Int | ||
document_manager.execute_script("return #{storage_type_in_js}.length").to_i | ||
end | ||
|
||
# TODO: Add doc | ||
private def storage_type_in_js | ||
"window.#{@storage.to_s.camelcase(lower: true)}" | ||
end | ||
end |