From 53590088d85d7f80b695f5f84c494d1d1d599345 Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Tue, 12 Feb 2013 09:48:33 -0800 Subject: [PATCH 1/2] allow finding of alerts --- app/uiauto/appium/app.js | 12 +++++-- sample-code/examples/ruby/simple_test.rb | 42 +++++++++++++++--------- test/functional/testapp/findElement.js | 28 ++++++++++++++-- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/app/uiauto/appium/app.js b/app/uiauto/appium/app.js index 771cb466c..374b5f738 100644 --- a/app/uiauto/appium/app.js +++ b/app/uiauto/appium/app.js @@ -74,7 +74,8 @@ $.extend(au, { , lookup: function(selector, ctx) { if (typeof selector === 'string') { - var _ctx = this.web ? this.web : this.mainWindow; + var _ctx = this.mainWindow + , elems = []; if (typeof ctx === 'string') { _ctx = this.cache[ctx]; @@ -83,7 +84,14 @@ $.extend(au, { } this.target.pushTimeout(0); - var elems = $(selector, _ctx); + if (selector === 'alert') { + var alert = this.mainApp.alert(); + if (alert) { + elems = $(alert); + } + } else { + elems = $(selector, _ctx); + } this.target.popTimeout(); return elems; diff --git a/sample-code/examples/ruby/simple_test.rb b/sample-code/examples/ruby/simple_test.rb index 91eab0113..5734992c2 100644 --- a/sample-code/examples/ruby/simple_test.rb +++ b/sample-code/examples/ruby/simple_test.rb @@ -61,28 +61,38 @@ describe "Computation" do @driver.quit end - it "should add two numbers" do - values = [rand(10), rand(10)] - expected_sum = values.reduce(&:+) - elements = @driver.find_elements(:tag_name, 'textField') + #it "should add two numbers" do + #values = [rand(10), rand(10)] + #expected_sum = values.reduce(&:+) + #elements = @driver.find_elements(:tag_name, 'textField') - elements.each_with_index do |element, index| - element.send_keys values[index] - end + #elements.each_with_index do |element, index| + #element.send_keys values[index] + #end - button = @driver.find_element(:tag_name, 'button') - button.click + #button = @driver.find_element(:tag_name, 'button') + #button.click - actual_sum = @driver.find_elements(:tag_name, 'staticText')[0].text - actual_sum.should eq(expected_sum.to_s) - end + #actual_sum = @driver.find_elements(:tag_name, 'staticText')[0].text + #actual_sum.should eq(expected_sum.to_s) + #end - it "should handle alerts" do + #it "should handle alerts" do + #els = @driver.find_elements(:tag_name, 'button') + #els[1].click + #a = @driver.switch_to.alert + #a.text.should eq("Cool title") + #a.accept + #end + + it "should find alerts" do els = @driver.find_elements(:tag_name, 'button') els[1].click - a = @driver.switch_to.alert - a.text.should eq("Cool title") - a.accept + alert = @driver.find_element(:tag_name, 'alert') + buttons = alert.find_elements(:tag_name, 'button') + buttons[0].click + alerts = @driver.find_elements(:tag_name, 'alert') + alerts.should be_empty end end diff --git a/test/functional/testapp/findElement.js b/test/functional/testapp/findElement.js index 52a977886..085fafeb5 100644 --- a/test/functional/testapp/findElement.js +++ b/test/functional/testapp/findElement.js @@ -11,7 +11,7 @@ describeWd('elementByTagName', function(h) { done(); }); }); - return it('should not find any invalid elements on the app and throw error', function(done) { + it('should not find any invalid elements on the app and throw error', function(done) { h.driver.elementByTagName('buttonNotThere', function(err, element) { should.not.exist(element); err.status.should.eql(7); @@ -19,9 +19,33 @@ describeWd('elementByTagName', function(h) { done(); }); }); + it('should find alerts when they exist', function(done) { + h.driver.elementsByTagName('button', function(err, els) { + should.not.exist(err); + els[1].click(function() { + h.driver.elementByTagName('alert', function(err, el) { + should.not.exist(err); + el.elementsByTagName('button', function(err, buttons) { + should.not.exist(err); + buttons.length.should.equal(2); + done(); + }); + }); + }); + }); + }); + it('should not find alerts when they dont exist', function(done) { + h.driver.elementByTagName('alert', function(err, el) { + should.exist(err); + should.not.exist(el); + err.status.should.eql(7); + err['jsonwire-error'].summary.should.eql('NoSuchElement'); + done(); + }); + }); }); -describeWd('elementByTagName', function(h) { +describeWd('elementsByTagName', function(h) { it('should find both elements on the app', function(done) { h.driver.elementsByTagName('button', function(err, elements) { elements.length.should.equal(2); From eee901fc2e3fa3ffca67217518b24d626be327ab Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Tue, 12 Feb 2013 10:06:07 -0800 Subject: [PATCH 2/2] special-case getting the text of a button --- app/ios.js | 2 +- app/uiauto/appium/element.js | 9 ++++++ sample-code/examples/ruby/simple_test.rb | 39 ++++++++++++------------ test/functional/testapp/basic.js | 9 ++++++ 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/ios.js b/app/ios.js index 82d8f4d9f..6b8ec1878 100644 --- a/app/ios.js +++ b/app/ios.js @@ -394,7 +394,7 @@ IOS.prototype.getText = function(elementId, cb) { this.remote.executeAtom('get_text', [atomsElement], cb); } } else { - var command = ["au.getElement('", elementId, "').value()"].join(''); + var command = ["au.getElement('", elementId, "').text()"].join(''); this.proxy(command, cb); } }; diff --git a/app/uiauto/appium/element.js b/app/uiauto/appium/element.js index 0e1ec2e8a..0e5b98606 100644 --- a/app/uiauto/appium/element.js +++ b/app/uiauto/appium/element.js @@ -64,6 +64,15 @@ UIAElement.prototype.hasChildren = function() { type === "UIASwitch" || type === "UIAElementNil"); }; +UIAElement.prototype.text = function() { + var type = this.type(); + if (type === "UIAButton") { + return this.label(); + } else { + return this.value(); + } +}; + UIAElement.prototype.matchesTagName = function(tagName) { var type = this.type(); // i.e. "UIALink" matches "link: diff --git a/sample-code/examples/ruby/simple_test.rb b/sample-code/examples/ruby/simple_test.rb index 5734992c2..1577c48ad 100644 --- a/sample-code/examples/ruby/simple_test.rb +++ b/sample-code/examples/ruby/simple_test.rb @@ -61,35 +61,36 @@ describe "Computation" do @driver.quit end - #it "should add two numbers" do - #values = [rand(10), rand(10)] - #expected_sum = values.reduce(&:+) - #elements = @driver.find_elements(:tag_name, 'textField') + it "should add two numbers" do + values = [rand(10), rand(10)] + expected_sum = values.reduce(&:+) + elements = @driver.find_elements(:tag_name, 'textField') - #elements.each_with_index do |element, index| - #element.send_keys values[index] - #end + elements.each_with_index do |element, index| + element.send_keys values[index] + end - #button = @driver.find_element(:tag_name, 'button') - #button.click + button = @driver.find_element(:tag_name, 'button') + button.click - #actual_sum = @driver.find_elements(:tag_name, 'staticText')[0].text - #actual_sum.should eq(expected_sum.to_s) - #end + actual_sum = @driver.find_elements(:tag_name, 'staticText')[0].text + actual_sum.should eq(expected_sum.to_s) + end - #it "should handle alerts" do - #els = @driver.find_elements(:tag_name, 'button') - #els[1].click - #a = @driver.switch_to.alert - #a.text.should eq("Cool title") - #a.accept - #end + it "should handle alerts" do + els = @driver.find_elements(:tag_name, 'button') + els[1].click + a = @driver.switch_to.alert + a.text.should eq("Cool title") + a.accept + end it "should find alerts" do els = @driver.find_elements(:tag_name, 'button') els[1].click alert = @driver.find_element(:tag_name, 'alert') buttons = alert.find_elements(:tag_name, 'button') + buttons[0].text.should eq("Cancel") buttons[0].click alerts = @driver.find_elements(:tag_name, 'alert') alerts.should be_empty diff --git a/test/functional/testapp/basic.js b/test/functional/testapp/basic.js index 992a9e5a2..d6ba5c2bf 100644 --- a/test/functional/testapp/basic.js +++ b/test/functional/testapp/basic.js @@ -137,6 +137,15 @@ describeWd('calc app', function(h) { //}; //_.each(["PORTRAIT", "LANDSCAPE"], testOrientation); + it('should be able to get text of a button', function(done) { + h.driver.elementsByTagName('button', function(err, els) { + els[0].text(function(err, text) { + text.should.eql("ComputeSumButton"); + done(); + }); + }); + }); + return it('should get an app screenshot', function(done){ h.driver.takeScreenshot(function(err, screenshot){ assert.notEqual(typeof screenshot, "undefined");