Skip to content

Commit 356aaa6

Browse files
authored
Merge pull request #550 from calabash/feature/DeviceAgent-add-support-for-query-star
DeviceAgent::Client: add support for query("*") and query("all *")
2 parents 9c4c190 + f17aa9f commit 356aaa6

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

lib/run_loop/device_agent/client.rb

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def server_version
317317

318318
# @!visibility private
319319
def tree
320-
options = http_options
320+
options = tree_http_options
321321
request = request("tree")
322322
client = http_client(options)
323323
response = client.get(request)
@@ -397,6 +397,12 @@ def enter_text_without_keyboard_check(string)
397397
# query({text: "\"To know is not enough.\""})
398398
# query({text: %Q["To know is not enough."]})
399399
#
400+
# # Equivalent to Calabash query("*")
401+
# query({})
402+
#
403+
# # Equivalent to Calabash query("all *")
404+
# query({all: true})
405+
#
400406
# Querying for text with newlines is not supported yet.
401407
#
402408
# The query language supports the following keys:
@@ -469,45 +475,40 @@ def query(uiquery)
469475
raise ArgumentError, %Q[
470476
Unsupported key or keys found: '#{unknown_keys}'.
471477
472-
Allowed keys for a query are: #{keys}
473-
474-
]
475-
end
476-
477-
has_any_key = (allowed_keys & uiquery.keys).any?
478-
if !has_any_key
479-
keys = allowed_keys.map { |key| ":#{key}" }.join(", ")
480-
raise ArgumentError, %Q[
481-
Query does not contain any keysUnsupported key or keys found: '#{unknown_keys}'.
478+
Allowed keys for a query are:
482479
483-
Allowed keys for a query are: #{keys}
480+
#{keys}
484481
485482
]
486483
end
487484

488-
parameters = merged_options.dup.tap { |hs| hs.delete(:all) }
489-
if parameters.empty?
490-
keys = allowed_keys.map { |key| ":#{key}" }.join(", ")
491-
raise ArgumentError, %Q[
485+
if _wildcard_query?(uiquery)
486+
elements = _flatten_tree
487+
else
488+
parameters = merged_options.dup.tap { |hs| hs.delete(:all) }
489+
if parameters.empty?
490+
keys = allowed_keys.map { |key| ":#{key}" }.join(", ")
491+
raise ArgumentError, %Q[
492492
Query must contain at least one of these keys:
493493
494494
#{keys}
495495
496496
]
497-
end
497+
end
498498

499-
request = request("query", parameters)
500-
client = http_client(http_options)
499+
request = request("query", parameters)
500+
client = http_client(http_options)
501501

502-
RunLoop.log_debug %Q[Sending query with parameters:
502+
RunLoop.log_debug %Q[Sending query with parameters:
503503
504504
#{JSON.pretty_generate(parameters)}
505505
506506
]
507507

508-
response = client.post(request)
509-
hash = expect_300_response(response)
510-
elements = hash["result"]
508+
response = client.post(request)
509+
hash = expect_300_response(response)
510+
elements = hash["result"]
511+
end
511512

512513
if merged_options[:all]
513514
elements
@@ -1050,6 +1051,18 @@ def http_options
10501051
end
10511052
end
10521053

1054+
# @!visibility private
1055+
#
1056+
# Tree can take a very long time.
1057+
def tree_http_options
1058+
timeout = DEFAULTS[:http_timeout] * 6
1059+
{
1060+
:timeout => timeout,
1061+
:interval => 0.1,
1062+
:retries => (timeout/0.1).to_i
1063+
}
1064+
end
1065+
10531066
# @!visibility private
10541067
def server_pid
10551068
options = http_options
@@ -1414,6 +1427,16 @@ def _dismiss_springboard_alerts
14141427
response = client.post(request)
14151428
expect_300_response(response)
14161429
end
1430+
1431+
# @!visibility private
1432+
# Private method. Do not call.
1433+
def _wildcard_query?(uiquery)
1434+
return true if uiquery.empty?
1435+
return false if uiquery.count != 1
1436+
1437+
uiquery.has_key?(:all)
1438+
end
1439+
14171440
end
14181441
end
14191442
end

spec/lib/device_agent/client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,19 @@ def inspect; to_s; end
451451
expect(actual.all? { |element| element["children"] == nil }).to be_truthy
452452
end
453453
end
454+
455+
context "#_wildcard_query?" do
456+
it "returns true if uiquery is empty" do
457+
expect(client.send(:_wildcard_query?, {})).to be_truthy
458+
end
459+
460+
it "returns true if uiquery contains only the :all key" do
461+
expect(client.send(:_wildcard_query?, {:all => true})).to be_truthy
462+
expect(client.send(:_wildcard_query?, {:all => false})).to be_truthy
463+
end
464+
465+
it "return false otherwise" do
466+
expect(client.send(:_wildcard_query?, {:type => "Button"})).to be_falsey
467+
end
468+
end
454469
end

0 commit comments

Comments
 (0)