Class: Appium::Core::Base::Bridge

Inherits:
Selenium::WebDriver::Remote::Bridge
  • Object
show all
Defined in:
lib/appium_lib_core/common/base/bridge.rb,
lib/appium_lib_core/common/base/bridge/w3c.rb,
lib/appium_lib_core/common/base/bridge/mjsonwp.rb

Defined Under Namespace

Classes: MJSONWP, W3C

Constant Summary collapse

APPIUM_PREFIX =

Prefix for extra capability defined by W3C

'appium:'
FORCE_MJSONWP =

TODO: Remove the forceMjsonwp after Appium server won't need it

:forceMjsonwp

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handshake(**opts) ⇒ Bridge::MJSONWP, Bridge::W3C

Almost same as self.handshake in ::Selenium::WebDriver::Remote::Bridge

Implements protocol handshake which:

1. Creates session with driver.
2. Sniffs response.
3. Based on the response, understands which dialect we should use.

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/appium_lib_core/common/base/bridge.rb', line 35

def self.handshake(**opts)
  desired_capabilities = opts.delete(:desired_capabilities) { ::Selenium::WebDriver::Remote::Capabilities.new }

  if desired_capabilities.is_a?(Symbol)
    unless ::Selenium::WebDriver::Remote::Capabilities.respond_to?(desired_capabilities)
      raise ::Selenium::WebDriver::Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
    end

    desired_capabilities = ::Selenium::WebDriver::Remote::Capabilities.__send__(desired_capabilities)
  end

  bridge = new(opts)
  capabilities = bridge.create_session(desired_capabilities)

  case bridge.dialect
  when :oss # for MJSONWP
    Bridge::MJSONWP.new(capabilities, bridge.session_id, **opts)
  when :w3c
    Bridge::W3C.new(capabilities, bridge.session_id, **opts)
  else
    raise CoreError, 'cannot understand dialect'
  end
end

Instance Method Details

#add_appium_prefix(capabilities) ⇒ ::Selenium::WebDriver::Remote::W3C::Capabilities

Append appium: prefix for Appium following W3C spec www.w3.org/TR/webdriver/#dfn-validate-capabilities

Parameters:

  • capabilities (::Selenium::WebDriver::Remote::W3C::Capabilities, Hash)

    A capability

Returns:

  • (::Selenium::WebDriver::Remote::W3C::Capabilities)

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/appium_lib_core/common/base/bridge.rb', line 111

def add_appium_prefix(capabilities)
  w3c_capabilities = ::Selenium::WebDriver::Remote::W3C::Capabilities.new

  capabilities = capabilities.__send__(:capabilities) unless capabilities.is_a?(Hash)

  capabilities.each do |name, value|
    next if value.nil?
    next if value.is_a?(String) && value.empty?

    capability_name = name.to_s
    w3c_name = extension_prefix?(capability_name) ? name : "#{APPIUM_PREFIX}#{capability_name}"

    w3c_capabilities[w3c_name] = value
  end

  w3c_capabilities
end

#create_session(desired_capabilities) ⇒ ::Selenium::WebDriver::Remote::Capabilities, ::Selenium::WebDriver::Remote::W3C::Capabilities

Override Creates session handling both OSS and W3C dialects. Copy from Selenium::WebDriver::Remote::Bridge to keep using merged_capabilities for Appium

Examples:


opts = {
  caps: {
    platformName: :ios,
    automationName: 'XCUITest',
    app: 'test/functional/app/UICatalog.app.zip',
    platformVersion: '11.4',
    deviceName: 'iPhone Simulator',
    useNewWDA: true,
  },
  appium_lib: {
    wait: 30
  }
}
core = ::Appium::Core.for(caps)
driver = core.start_driver #=> driver.dialect == :w3c if the Appium server support W3C.

Parameters:

  • desired_capabilities (::Selenium::WebDriver::Remote::W3C::Capabilities, Hash)

    A capability

Returns:

  • (::Selenium::WebDriver::Remote::Capabilities, ::Selenium::WebDriver::Remote::W3C::Capabilities)

Raises:

  • (::Selenium::WebDriver::Error::WebDriverError)

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/appium_lib_core/common/base/bridge.rb', line 84

def create_session(desired_capabilities)
  response = execute(:new_session, {}, merged_capabilities(desired_capabilities))

  @session_id = response['sessionId']
  oss_status = response['status'] # for compatibility with Appium 1.7.1-
  value = response['value']

  if value.is_a?(Hash) # include for W3C format
    @session_id = value['sessionId'] if value.key?('sessionId')

    if value.key?('capabilities')
      value = value['capabilities']
    elsif value.key?('value')
      value = value['value']
    end
  end

  raise ::Selenium::WebDriver::Error::WebDriverError, 'no sessionId in returned payload' unless @session_id

  json_create(oss_status, value)
end