summaryrefslogtreecommitdiffstats
path: root/features/support/helpers/dogtail.rb
blob: 2d812053164d034388609dbb8c259146e6155b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
module Dogtail
  module Mouse
    LEFT_CLICK = 1
    MIDDLE_CLICK = 2
    RIGHT_CLICK = 3
  end

  TREE_API_NODE_SEARCHES = [
    :button,
    :child,
    :childLabelled,
    :childNamed,
    :menu,
    :menuItem,
    :tab,
    :textentry,
  ]

  TREE_API_NODE_SEARCH_FIELDS = [
    :parent,
  ]

  TREE_API_NODE_ACTIONS = [
    :click,
    :doubleClick,
    :grabFocus,
    :keyCombo,
    :point,
    :typeText,
  ]

  TREE_API_APP_SEARCHES = TREE_API_NODE_SEARCHES + [
    :dialog,
    :window,
  ]

  # We want to keep this class immutable so that handles always are
  # left intact when doing new (proxied) method calls.  This way we
  # can support stuff like:
  #
  #     app = Dogtail::Application.new('gedit')
  #     menu = app.menu('Menu')
  #     menu.click()
  #     menu.something_else()
  #     menu.click()
  #
  # i.e. the object referenced by `menu` is never modified by method
  # calls and can be used as expected. This explains why
  # `proxy_call()` below returns a new instance instead of adding
  # appending the new component the proxied method call would result
  # in.

  class Application

    def initialize(app_name, opts = {})
      @app_name = app_name
      @opts = opts
      @init_lines = @opts[:init_lines] || [
        "from dogtail import tree",
        "from dogtail.config import config",
        "config.logDebugToFile = False",
        "config.logDebugToStdOut = False",
        "config.blinkOnActions = True",
        "config.searchShowingOnly = True",
        "application = tree.root.application('#{@app_name}')",
      ]
      @components = @opts[:components] || ['application']
    end

    def build_script(lines)
      (
        ["#!/usr/bin/python"] +
        @init_lines +
        lines
      ).join("\n")
    end

    def build_line
      @components.join('.')
    end

    def run(lines = nil)
      @opts[:user] ||= LIVE_USER
      lines ||= [build_line]
      lines = [lines] if lines.class != Array
      script = build_script(lines)
      script_path = $vm.execute_successfully('mktemp', @opts).stdout.chomp
      $vm.file_overwrite(script_path, script)
      args = ["/usr/bin/python '#{script_path}'", @opts]
      if @opts[:allow_failure]
        return $vm.execute(*args)
      else
        begin
          return $vm.execute_successfully(*args)
        rescue Exception => e
          debug_log("Failing Dogtail script (#{script_path}):")
          script.split("\n").each { |line| debug_log(" "*4 + line) }
          raise e
        end
      end
    end

    def self.value_to_s(v)
      if v == true
        'True'
      elsif v == false
        'False'
      elsif v.class == String
        "'#{v}'"
      elsif [Fixnum, Float].include?(v.class)
        v.to_s
      else
        raise "#{self.class.name} does not know how to handle argument type '#{v.class}'"
      end
    end

    # Generates a Python-style parameter list from `args`. If the last
    # element of `args` is a Hash, it's used as Python's kwargs dict.
    # In the end, the resulting string should be possible to copy-paste
    # into the parentheses of a Python function call.
    # Example: [42, {:foo => 'bar'}] => "42, foo = 'bar'"
    def self.args_to_s(args)
      return "" if args.size == 0
      args_list = args
      args_hash = nil
      if args_list.class == Array && args_list.last.class == Hash
        *args_list, args_hash = args_list
      end
      (
        (args_list.nil? ? [] : args_list.map { |e| self.value_to_s(e) }) +
        (args_hash.nil? ? [] : args_hash.map { |k, v| "#{k}=#{self.value_to_s(v)}" })
      ).join(', ')
    end

    def wait(timeout = nil)
      if timeout
        try_for(timeout) { run }
      else
        run
      end
    end

    def exist?
      @opts[:allow_failure] = true
      # We do not want any retries since this method should return the
      # result for the immediate situation, not for the situation up
      # to 20 retries in the future.
      optimization = "config.searchCutoffCount = 1"
      @init_lines << optimization unless @init_lines.include?(optimization)
      run.success?
    end

    def wait_vanish(timeout)
      try_for(timeout) { not(exist?) }
    end

    # Equivalent to the Tree API's Node.findChildren(), with the
    # arguments constructing a GenericPredicate to use as parameter.
    def children(*args)
      non_predicates = [:recursive, :showingOnly]
      findChildren_opts = []
      findChildren_opts_hash = Hash.new
      if args.last.class == Hash
        args_hash = args.last
        non_predicates.each do |opt|
          if args_hash.has_key?(opt)
            findChildren_opts_hash[opt] = args_hash[opt]
            args_hash.delete(opt)
          end
        end
      end
      findChildren_opts = ""
      if findChildren_opts_hash.size > 0
        findChildren_opts = ", " + self.class.args_to_s([findChildren_opts_hash])
      end
      # A fundamental assumption of ScriptProxy is that we will only
      # act on *one* object at a time. If we were to allow more, we'd
      # have to port looping, conditionals and much more into our
      # script generation, which is insane.
      # However, since references are lost between script runs (=
      # Application.run()) we need to be a bit tricky here. We use the
      # internal a11y AT-SPI "path" to uniquely identify a Dogtail
      # node, so we can give handles to each of them that can be used
      # later to re-find them.
      predicate_opts = self.class.args_to_s(args)
      find_paths_script_lines = [
        "from dogtail import predicate",
        "for n in #{build_line}.findChildren(predicate.GenericPredicate(#{predicate_opts})#{findChildren_opts}):",
        "    print(n.path)",
      ]
      a11y_at_spi_paths = run(find_paths_script_lines).stdout.chomp.split("\n")
                          .grep(Regexp.new('^/org/a11y/atspi/accessible/'))
                          .map { |path| path.chomp }
      a11y_at_spi_paths.map do |path|
        more_init_lines = [
          "from dogtail import predicate",
          "node = None",
          "for n in #{build_line}.findChildren(predicate.GenericPredicate()):",
          "    if str(n.path) == '#{path}':",
          "        node = n",
          "        break",
          "assert(node)",
        ]
        Node.new(
          @app_name,
          @opts.merge(
            init_lines: @init_lines + more_init_lines,
            components: ['node']
          )
        )
      end
    end

    def get_field(key)
      run("print(#{build_line}.#{key})").stdout.chomp
    end

    def set_field(key, value)
      run("#{build_line}.#{key} = #{self.class.value_to_s(value)}")
    end

    def text
      get_field('text')
    end

    def text=(value)
      set_field('text', value)
    end

    def name
      get_field('name')
    end

    def proxy_call(method, args)
      args_str = self.class.args_to_s(args)
      method_call = "#{method.to_s}(#{args_str})"
      Node.new(
        @app_name,
        @opts.merge(
          init_lines: @init_lines,
          components: @components + [method_call]
        )
      )
    end

    TREE_API_APP_SEARCHES.each do |method|
      define_method(method) do |*args|
        proxy_call(method, args)
      end
    end

    TREE_API_NODE_SEARCH_FIELDS.each do |field|
      define_method(field) do
        Node.new(
          @app_name,
          @opts.merge(
            init_lines: @init_lines,
            components: @components + [field]
          )
        )
      end
    end

  end

  class Node < Application

    TREE_API_NODE_SEARCHES.each do |method|
      define_method(method) do |*args|
        proxy_call(method, args)
      end
    end

    TREE_API_NODE_ACTIONS.each do |method|
      define_method(method) do |*args|
        proxy_call(method, args).run
      end
    end

  end
end