Class: Rubium::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rubium/driver.rb

Defined Under Namespace

Classes: TimeoutError

Constant Summary collapse

DEFAULT_SESSION_TIMEOUT =

The default session timeout, in seconds

30

Instance Attribute Summary collapse

Session Management collapse

Managing timeouts collapse

Javascript Proxy Methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(capabilities, host = Rubium.default_host, port = Rubium.default_port) ⇒ Driver

Returns a new instance of Driver.


10
11
12
13
14
15
# File 'lib/rubium/driver.rb', line 10

def initialize(capabilities, host = Rubium.default_host, port = Rubium.default_port)
  @capabilities = capabilities
  @host = host
  @port = port
  @implicit_timeout = 1
end

Instance Attribute Details

#implicit_timeoutvoid

Returns the value of attribute implicit_timeout


5
6
7
# File 'lib/rubium/driver.rb', line 5

def implicit_timeout
  @implicit_timeout
end

Instance Method Details

#capture_screenshot(output_file, format = :png) ⇒ void


164
165
166
# File 'lib/rubium/driver.rb', line 164

def capture_screenshot(output_file, format = :png)
  File.open(output_file, 'wb') { |io| io.write driver.screenshot_as(format) }
end

#execute_script(script) ⇒ void Also known as: execute

Note:

This method will always return immediately in the case of an error, regardless of# any implicit or native timeout set. If you need to execute some Javascript until it is successful, you should consider using an explicit wait.

Executes a string of Javascript within the Instruments process.

Parameters:

  • script (String)

    the Javascript to be executed.

Raises:

  • (Selenium::WebDriver::Error::JavascriptError)

    if the evaluated Javascript errors.


194
195
196
# File 'lib/rubium/driver.rb', line 194

def execute_script(script)
  driver.execute_script(script)
end

#find(xpath) ⇒ void


154
155
156
157
158
# File 'lib/rubium/driver.rb', line 154

def find(xpath)
  element_proxy_for driver.find_element(:xpath, xpath)
rescue Selenium::WebDriver::Error::NoSuchElementError => e
  UIAutomation::NoSuchElement.new
end

#find_all(xpath) ⇒ void


160
161
162
# File 'lib/rubium/driver.rb', line 160

def find_all(xpath)
  driver.find_elements(:xpath, xpath)
end

#launch(session_timeout = DEFAULT_SESSION_TIMEOUT) ⇒ void

Launches a new Appium session.

Launching a new Appium session will cause Appium to launch Instruments, which in turn will launch your application in the simulator or on your device.

Parameters:

  • session_timeout (Numeric) (defaults to: DEFAULT_SESSION_TIMEOUT)

    the underlying HTTP session timeout, in seconds

Raises:


27
28
29
30
# File 'lib/rubium/driver.rb', line 27

def launch(session_timeout = DEFAULT_SESSION_TIMEOUT)
  @session ||= Rubium::Session.new(@host, @port, @capabilities, session_timeout)
  update_implicit_timeout
end

#loggerUIAutomation::Logger

Returns a proxy to the native UIAutomation logger.


184
185
186
# File 'lib/rubium/driver.rb', line 184

def logger
  @logger ||= UIAutomation::Logger.logger(self)
end

#native_timeoutvoid

Returns the native Javascript API implicit timeout

See Also:

  • UIATarget.timeout()

106
107
108
# File 'lib/rubium/driver.rb', line 106

def native_timeout
  target.timeout
end

#native_timeout=(new_timeout) ⇒ void

Sets the native Javascript API implicit timeout

Parameters:

  • new_timeout (Numeric)

    The new timeout, in seconds

See Also:

  • UIATarget.setTimeout()

114
115
116
# File 'lib/rubium/driver.rb', line 114

def native_timeout=(new_timeout)
  target.set_timeout(new_timeout)
end

#quitvoid

Quits the current session, if there is one.

When you quit a session, Appium will terminate the Instruments process which will in turn kill the iOS simulator or remove the app from your device.


37
38
39
40
# File 'lib/rubium/driver.rb', line 37

def quit
  @session.terminate if @session
  @session = nil
end

#relaunchvoid

Quits any existing session before launching a new one.


68
69
70
71
# File 'lib/rubium/driver.rb', line 68

def relaunch
  quit
  launch
end

#targetUIAutomation::Target

Returns a proxy to the local target (UIATarget).

This method is the main entry point into the UIAutomation Javascript proxy API. The local target is the root object in the UIAutomation object graph.

Returns:


177
178
179
# File 'lib/rubium/driver.rb', line 177

def target
  @target ||= UIAutomation::Target.local_target(self)
end

#wait_until(timeout: 10, interval: 0.2, &block) ⇒ void

Performs an explicit wait until the given block returns true.

You can use this method to wait for an explicit condition to occur before continuing.

Examples:

driver.wait_until { something_happened }

Parameters:

  • timeout (Numeric) (defaults to: 10)

    The explicit wait timeout, in seconds

  • interval (Numeric) (defaults to: 0.2)

    The interval to wait between retries.

Yield Returns:

  • (Boolean)

    The block will be repeatedly called up to the timeout until it returns true.

Raises:


146
147
148
149
150
# File 'lib/rubium/driver.rb', line 146

def wait_until(timeout: 10, interval: 0.2, &block)
  Selenium::WebDriver::Wait.new(timeout: timeout, interval: interval).until(&block)
rescue Selenium::WebDriver::Error::TimeOutError => e
  raise TimeoutError.new(e.message)
end

#with_implicit_timeout(timeout, &block) ⇒ void

Temporarily sets the implicit timeout to the given value and invokes the block.

After the block has been invoked, the original timeout will be restored.

Parameters:

  • timeout (Numeric)

    The temporary timeout, in seconds


97
98
99
100
101
# File 'lib/rubium/driver.rb', line 97

def with_implicit_timeout(timeout, &block)
  update_implicit_timeout(timeout)
  yield
  update_implicit_timeout
end

#with_native_timeout(value, &block) ⇒ void

Temporarily sets the native Javascript API implicit timeout by pushing a new timeout on to the timeout stack, calling the given block and then popping the timeout off the stack.

Parameters:

  • value (Numeric)

    The temporary timeout, in seconds

See Also:

  • UIATarget.popTimeout()

125
126
127
128
129
# File 'lib/rubium/driver.rb', line 125

def with_native_timeout(value, &block)
  target.push_timeout(value)
  yield if block_given?
  target.pop_timeout
end

#with_session(session_timeout = DEFAULT_SESSION_TIMEOUT, &block) ⇒ void

Launches a new session, calls the given block, then quits.

This method lets you treat a session as a transaction, with the given block being executed after launching then quitting the session when the block returns.

Using this method ensures you do not have to explicitly quit the session when you are finished.

This method will quit the session after the block has finished executing, even if the block raises an exception.

Parameters:

  • session_timeout (Numeric) (defaults to: DEFAULT_SESSION_TIMEOUT)

    the underlying HTTP session timeout, in seconds

Raises:

  • (RuntimeError)

    if a session is already running


56
57
58
59
60
61
62
63
64
# File 'lib/rubium/driver.rb', line 56

def with_session(session_timeout = DEFAULT_SESSION_TIMEOUT, &block)
  raise "Session already running!" if @session
  launch(session_timeout)
  begin
    yield @session if block_given?
  ensure
    quit
  end
end