Module: SeleniumDsl::Commands

Included in:
SeleniumDsl
Defined in:
lib/selenium_dsl/macros.rb,
lib/selenium_dsl/commands.rb

Instance Method Summary collapse

Instance Method Details

#commandsObject



3
4
5
6
7
# File 'lib/selenium_dsl/commands.rb', line 3

def commands
  SeleniumDsl::Commands.private_instance_methods.collect do |x|
    "#{x[1,99]}"
  end
end

#find_element(typ, el) ⇒ Object



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
# File 'lib/selenium_dsl/commands.rb', line 18

def find_element(typ,el)
  wait = Selenium::WebDriver::Wait.new(:timeout => 5) 
  wait.until do
    try=5
    begin
      nodes = @nodes.find_element(typ, el) 
    rescue Exception => e
      if e.class == Selenium::WebDriver::Error::NoSuchWindowError ||
         e.class == Selenium::WebDriver::Error::NoSuchElementError
        puts "switch: #{@driver.window_handle} to #{@driver.window_handles.inspect}" if opt_v
        @driver.switch_to.window @driver.window_handles[0]
        sleep 1
        try-= 1
        try>0 ? retry : raise(e.class,e.to_s)
      else
        puts "~>err: #{e.to_s}"
        failed
      end
    end
    @nodes = nodes if nodes
    nodes
  end
  print (opt_m ? '.' : '.'.green) if !opt_v
rescue Exception => e
  failed
end

#find_elements(typ, el, idx) ⇒ Object



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
# File 'lib/selenium_dsl/commands.rb', line 45

def find_elements(typ,el,idx)
  wait = Selenium::WebDriver::Wait.new(:timeout => 5) 
  wait.until do
    try=5
    begin
      # binding.pry
      nodes = @nodes.find_elements(typ, el)[idx-1]
    rescue Exception => e
      if e.class == Selenium::WebDriver::Error::NoSuchWindowError ||
         e.class == Selenium::WebDriver::Error::NoSuchElementError
        puts "switch: #{@driver.window_handle} to #{@driver.window_handles.inspect}" if opt_v
        @driver.switch_to.window @driver.window_handles[0]
        sleep 1
        try-= 1
        try>0 ? retry : raise(e.class,e.to_s)
      else
        puts "~>err: #{e.to_s}"
        failed
      end
    end
    @nodes = nodes if nodes
    # puts "func : #{el} #{idx}" if opt_v
    # puts "TAG : #{@nodes.tag_name}" if opt_v
    # puts "TEXT: #{@nodes.text}" if opt_v
    nodes
  end
  print (opt_m ? '.' : '.'.green) if !opt_v
rescue Exception => e
    # puts "TAG : #{@nodes.tag_name}" if opt_v
    # puts @nodes.text if opt_v
  failed
end

#in_commands?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
# File 'lib/selenium_dsl/commands.rb', line 9

def in_commands?(cmd)
  if cmd
    c = cmd[1,99]
    commands.index(c) ? c : nil
  else
    nil
  end
end

#parse_cmd(line) ⇒ Object



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
# File 'lib/selenium_dsl/commands.rb', line 78

def parse_cmd(line)
  arr = match_line(@r_cmd,line.strip,'cmd')
  query,cmd,prm = arr
  @nodes = @driver 
  if query!=[] && !(cmd==[] && prm=='') && !@mock
    puts "#{@path}>cmd: #{arr.inspect}" if opt_v

    query.each do |el|
      idx = el[/\[([\w\d=]+)\]/,1].to_i
      el.sub!(/\[(\d+)\]/,'') if idx>0
      if el[0]==":"
        if idx>0
          find_elements(:name, el[1,99],idx)
        else
          find_element(:name, el[1,99])
        end
      elsif el[0]=="/"
        if idx>0
          find_elements(:xpath, "/#{el}",idx)
        else
          find_element(:xpath, "/#{el}")
        end
      else
        if el[0]==">"
          if idx>0
            find_elements(:css, el[1,99],idx)
          else
            find_element(:css,  el[1,99])
          end
        else
          if idx>0
            find_elements(:css, el,idx)
          else
            find_element(:css,  el)
          end
        end
      end
    end

    if cmd==[] #no command supplied
      cmd << "~val" if (str=prm[/[=~]+/]) && str.length==1
    end
    if cmd[0][0]=="@"
      @return = @nodes.attribute(cmd[0][1,99])
      assert(prm)
    else
      c1,c2 = cmd[0].split(':',2)
      if (exc = in_commands?(c1))
        @return = nil
        send("_#{exc}",prm,c2)
      end
    end
    true
  else
    false
  end
end

#parse_mcr(line) ⇒ Object



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
# File 'lib/selenium_dsl/macros.rb', line 3

def parse_mcr(line)
  arr = match_line(@r_mcr,line.strip,'mcr')

  m_set,m_run,prm = arr
  if (m_set!=[] || m_run!=[]) && !@mock
    puts "#{@path}>mcr: #{arr.inspect}" if opt_v
    @return  = nil
    c = _code_
    if m_set!=[]
      key = m_set[0].split('=')[0].strip
      c[:macro][key] = prm
    else
      m = c[:macro][m_run[0]]
      prm.split(',').each_with_index do |row,idx|
        m.gsub!("$#{idx}",row)
      end
      l = c[:line]- 1
      c[:line]    = l 
      c[:code][l] = m
    end
    true
  else
    false
  end
end