Skip to content

Commit

Permalink
Update Ruby sample code.
Browse files Browse the repository at this point in the history
Change from tag_name to class_name for u\_i\_catalog, simple\_test,
cucumber tests.

Changed to iOS 7.1 in u\_i\_catalog, simple\_test, cucumber tests.

Changed `capabilities` to `desired_caps` to avoid some weirdo
collisions.

Remove Debugging output from Cucumber.

Add example of send\_keys as an action chain for u\_i\_catalog.
  • Loading branch information
DylanLacey committed Apr 1, 2014
1 parent 8ff9a04 commit 633c2ed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@


Given /^I have entered (\d+) into field (\d+) of the calculator$/ do |value, field|
puts "Called: #{value} #{field}"
elements = selenium.find_elements(:tag_name, "textField")
elements = selenium.find_elements(:class_name, "UIATextField")
elements[field.to_i - 1].send_keys value
end

And /^I press button (\d+)$/ do |button_index|
button = selenium.find_elements(:tag_name, "button")[button_index.to_i - 1 ]
button = selenium.find_elements(:class_name, "UIAButton")[button_index.to_i - 1 ]
button.click
end

Then /^the result should be displayed as (\d+)$/ do |expected|
result = selenium.find_element(:tag_name, "staticText")
result = selenium.find_element(:class_name, "UIAStaticText")
result.attribute("value").should eq expected
end
6 changes: 3 additions & 3 deletions sample-code/examples/ruby/cucumber/features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
APP_PATH = '../../../../../apps/TestApp/build/release-iphonesimulator/TestApp.app'

# What we need as a capability --> iOS device, where our app is, ect.
def capabilities
def desired_caps
{
'browserName' => '',
'platform' => 'Mac',
'device' => 'iPhone Simulator',
'version' => '6.0',
'version' => '7.1',
'app' => absolute_app_path
}
end
Expand All @@ -36,7 +36,7 @@ def server_url

# Set up a driver or, if one exists, return it
def selenium
@driver ||= Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)
@driver ||= Selenium::WebDriver.for(:remote, :desired_capabilities => desired_caps, :url => server_url)
end

After { @driver.quit }
30 changes: 16 additions & 14 deletions sample-code/examples/ruby/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ def absolute_app_path
file
end

capabilities = {
desired_caps = {
'browserName' => '',
'platform' => 'Mac',
'device' => 'iPhone Simulator',
'version' => '6.0',
'version' => '7.1',
'app' => absolute_app_path
}

server_url = "http://127.0.0.1:4723/wd/hub"

describe "Computation" do
before :all do
@driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)
@driver = Selenium::WebDriver.for(:remote, :desired_capabilities => desired_caps, :url => server_url)
@driver.manage.timeouts.implicit_wait = 10 # seconds
end

Expand All @@ -63,45 +63,47 @@ def absolute_app_path
it "should add two numbers" do
values = [rand(10), rand(10)]
expected_sum = values.reduce(&:+)
elements = @driver.find_elements(:tag_name, 'textField')
elements = @driver.find_elements(:class_name, 'UIATextField')

elements.each_with_index do |element, index|
element.send_keys values[index]
end

button = @driver.find_element(:tag_name, 'button')
button = @driver.find_element(:class_name, 'UIAButton')
button.click

actual_sum = @driver.find_elements(:tag_name, 'staticText')[0].text
actual_sum = @driver.find_elements(:class_name, 'UIAStaticText')[0].text
actual_sum.should eq(expected_sum.to_s)
end

it "should handle alerts" do
els = @driver.find_elements(:tag_name, 'button')
els = @driver.find_elements(:class_name, 'UIAButton')
els[1].click
a = @driver.switch_to.alert
a.text.should eq("Cool title")
a.text.should include("Cool title") # Returns title & text
a.accept
end

it "should find alerts" do
els = @driver.find_elements(:tag_name, 'button')
els = @driver.find_elements(:class_name, 'UIAButton')
els[1].click
alert = @driver.find_element(:tag_name, 'alert')
buttons = alert.find_elements(:tag_name, 'button')
buttons[0].text.should eq("Cancel")
alert = @driver.find_element(:class_name, 'UIAAlert')
buttons = alert.find_elements(:class_name, 'UIATableCell')
buttons[0].attribute('label').should eq "Cancel"
#.should.equal("Cancel")

buttons[0].click
wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds
wait.until {
alerts = @driver.find_elements(:tag_name, 'alert')
alerts = @driver.find_elements(:class_name, 'UIAAlert')
alerts.should be_empty
}
end

it "should get window size" do
size = @driver.manage.window.size
size.width.should eq(320)
size.height.should eq(480)
size.height.should eq(568)
end

end
58 changes: 28 additions & 30 deletions sample-code/examples/ruby/u_i_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

APP_PATH = '../../apps/UICatalog/build/Release-iphonesimulator/UICatalog.app'

def capabilities
def desired_caps
{
'browserName' => '',
'platform' => 'Mac',
'device' => 'iPhone Simulator',
'version' => '6.0',
'version' => '7.1',
'app' => absolute_app_path
}
end
Expand All @@ -39,7 +39,7 @@ def go_back

describe "UI Catalog" do
before(:all) do
@driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)
@driver = Selenium::WebDriver.for(:remote, :desired_capabilities => desired_caps, :url => server_url)

end

Expand All @@ -49,34 +49,33 @@ def go_back

describe "An Element" do

subject { @driver.find_elements(:tag_name, "tableView")[0]}
subject { @driver.find_elements(:class_name, "UIATableView")[0]}

it {should_not be nil}

context "when used as a selection context" do

it "Can be a selection context" do
rows = subject.find_elements(:tag_name, "tableCell")
rows = subject.find_elements(:class_name, "UIATableCell")
rows.size.should eq 12
end

it "does not return elements it does not contain" do
nav_bar = subject.find_elements(:tag_name, "navigationBar")
nav_bar = subject.find_elements(:class_name, "UIANavigationBar")
nav_bar.length.should be 0
end
end

# Not currently working, no text being returned
it "returns its text" do
rows = subject.find_elements(:tag_name, "tableCell")
rows = subject.find_elements(:class_name, "UIATableCell")
rows[0].attribute(:name).should eq "Buttons, Various uses of UIButton"
end

end

describe "position" do
it "is returned by the driver" do
third_row = @driver.find_elements(:tag_name, "tableCell")[2]
third_row = @driver.find_elements(:class_name, "UIATableCell")[2]
third_row.location.x.should be 0
third_row.location.y.should be 152
end
Expand All @@ -96,8 +95,8 @@ def go_back
describe "attributes" do

before :all do
@driver.find_elements(:tag_name, "tableCell")[9].click
@switch = @driver.find_element(:tag_name, "switch")
@driver.find_elements(:class_name, "UIATableCell")[9].click
@switch = @driver.find_element(:class_name, "UIASwitch")
end

# Go back to the menu when you're done
Expand Down Expand Up @@ -130,8 +129,8 @@ def go_back
describe "text fields" do

before :all do
@driver.find_elements(:tag_name, "tableCell")[2].click
@text_field = @driver.find_element(:tag_name, "textField")
@driver.find_elements(:class_name, "UIATableCell")[2].click
@text_field = @driver.find_element(:class_name, "UIATextField")
end

after :all do
Expand All @@ -146,7 +145,12 @@ def go_back
@text_field.attribute("value").should eq "discombobulate"
end

it "can accept key presses as an ActionChain"
it "can accept key presses as an ActionChain" do
@driver.action.send_keys(Selenium::WebDriver::Keys[:backspace])
.send_keys('te')
.perform
@text_field.attribute("value").should eq "discombobulatte"
end

it "can be cleared" do
@text_field.clear
Expand All @@ -156,8 +160,8 @@ def go_back

describe "alerts" do
before :all do
@driver.find_elements(:tag_name, "tableCell")[10].click
@elements = @driver.find_elements(:tag_name, "staticText")
@driver.find_elements(:class_name, "UIATableCell")[10].click
@elements = @driver.find_elements(:class_name, "UIATableCell")
end

after :all do
Expand All @@ -171,26 +175,20 @@ def go_back
it "can be dismissed"

it "can be modal & have buttons" do
modal = @driver.find_elements(:tag_name, "staticText")
modal = @driver.find_elements(:class_name, "UIAStaticText")
end
end

describe "scrolling" do

it "can be done with co-ordinates" do
row = @driver.find_elements(:tag_name, "tableCell")[2]
initial_location = row.location
action = @driver.touch.flick(0, 20)

action.perform
initial_location.should_not eq row.location
end
# Does not work on iOS 7 yet
it "can be done with co-ordinates"
end

describe "sliders" do
before :all do
@driver.find_elements(:tag_name, "tableCell")[1].click
@slider = @driver.find_element(:tag_name, "slider")
@driver.find_elements(:class_name, "UIATableCell")[1].click
@slider = @driver.find_element(:class_name, "UIASlider")
end

after :all do
Expand Down Expand Up @@ -222,8 +220,8 @@ def go_back

describe "sizes" do
it "can be obtained from elements" do
table_dimensions = @driver.find_element(:tag_name, "tableView").size
row_dimensions = @driver.find_elements(:tag_name, "tableCell")[0].size
table_dimensions = @driver.find_element(:class_name, "UIATableView").size
row_dimensions = @driver.find_elements(:class_name, "UIATableCell")[0].size

table_dimensions["width"].should eq row_dimensions["width"]
table_dimensions["height"].should_not eq row_dimensions["height"]
Expand All @@ -233,7 +231,7 @@ def go_back
describe "page source" do
before :all do
@main_source = @driver.page_source
@driver.find_elements(:tag_name, "tableCell")[2].click
@driver.find_elements(:class_name, "UIATableCell")[2].click
@text_source = @driver.page_source
end

Expand Down
4 changes: 2 additions & 2 deletions sample-code/examples/ruby/xunit_android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'test/unit'
require 'selenium-webdriver'

def capabilities
def desired_caps
{
'browserName' => 'android',
'platform' => 'linux',
Expand All @@ -18,7 +18,7 @@ def capabilities

def init(data={})
server_url = 'http://127.0.0.1:4723/wd/hub'
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities.merge(data), :url => server_url)
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => desired_caps.merge(data), :url => server_url)
driver.manage.timeouts.implicit_wait = 20 # seconds
driver
end
Expand Down

0 comments on commit 633c2ed

Please sign in to comment.