selenium-dsl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :test do
4
+ gem "minitest"
5
+ end
6
+
7
+ gem "selenium-webdriver"
8
+ gem "pry"
9
+ gem "term-ansicolor"
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.3.2)
5
+ childprocess (0.3.6)
6
+ ffi (~> 1.0, >= 1.0.6)
7
+ coderay (1.0.8)
8
+ ffi (1.1.5)
9
+ libwebsocket (0.1.5)
10
+ addressable
11
+ method_source (0.8.1)
12
+ minitest (4.2.0)
13
+ multi_json (1.3.7)
14
+ pry (0.9.10)
15
+ coderay (~> 1.0.5)
16
+ method_source (~> 0.8)
17
+ slop (~> 3.3.1)
18
+ rubyzip (0.9.9)
19
+ selenium-webdriver (2.26.0)
20
+ childprocess (>= 0.2.5)
21
+ libwebsocket (~> 0.1.3)
22
+ multi_json (~> 1.0)
23
+ rubyzip
24
+ slop (3.3.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ minitest
31
+ pry
32
+ selenium-webdriver
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require "bundler/gem_tasks"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ t.libs.push 'spec'
8
+ end
data/bin/sd ADDED
@@ -0,0 +1,4 @@
1
+ require 'selenium_dsl'
2
+
3
+ codes = IO.read(ARGV[0])
4
+ SeleniumDsl.parser(codes,ARGV[1])
data/bin/selenium-dsl ADDED
@@ -0,0 +1,4 @@
1
+ require 'selenium_dsl'
2
+
3
+ codes = IO.read(ARGV[0])
4
+ SeleniumDsl.parser(codes,ARGV[1])
@@ -0,0 +1,131 @@
1
+ class SeleniumDsl
2
+ module Commands
3
+ def commands
4
+ SeleniumDsl::Commands.private_instance_methods.collect do |x|
5
+ "#{x[1,99]}"
6
+ end
7
+ end
8
+
9
+ def in_commands?(cmd)
10
+ if cmd
11
+ c = cmd[1,99]
12
+ commands.index(c) ? c : nil
13
+ else
14
+ nil
15
+ end
16
+ end
17
+
18
+ def find_element(typ,el)
19
+ wait = Selenium::WebDriver::Wait.new(:timeout => 10)
20
+ wait.until { @nodes = @nodes.find_element(typ, el) }
21
+ print '.'.green if !opt_v
22
+ rescue Exception => e
23
+ failed
24
+ end
25
+
26
+ def find_elements(typ,el,idx)
27
+ wait = Selenium::WebDriver::Wait.new(:timeout => 10)
28
+ wait.until { @nodes = @nodes.find_elements(typ, el)[idx-1] }
29
+ print '.'.green if !opt_v
30
+ rescue Exception => e
31
+ failed
32
+ end
33
+
34
+ def parse_cmd(line)
35
+ arr = match_line(@r_cmd,line.strip,'cmd')
36
+
37
+ query,cmd,prm = arr
38
+ if query!=[] && !(cmd==[] && prm=='') && !@mock
39
+ puts "#{@path}>cmd: #{arr.inspect}" if opt_v
40
+ @nodes = @driver
41
+
42
+ query.each do |el|
43
+ if el[0]==":"
44
+ find_element(:name, el[1,99])
45
+ elsif el[0]=="/"
46
+ find_element(:xpath,"/#{el}")
47
+ else
48
+ idx = el[/\[(\d+)\]/,1].to_i
49
+ el.sub!(/\[(\d+)\]/,'')
50
+ if el[0]==">"
51
+ if idx>0
52
+ # binding.pry
53
+ find_elements(:css, el[1,99],idx)
54
+ else
55
+ find_element(:css, el[1,99])
56
+ end
57
+ else
58
+ if idx>0
59
+ find_elements(:css, el,idx)
60
+ else
61
+ find_element(:css, el)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ if cmd==[] #no command supplied
68
+ cmd << "~val" if (value=prm[/[=]+/]) && value.length==1
69
+ end
70
+ c1,c2 = cmd[0].split(':',2)
71
+ if (exc = in_commands?(c1))
72
+ @return = nil
73
+ send("_#{exc}",prm,c2)
74
+ end
75
+ true
76
+ else
77
+ false
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def _attr(prm,c2='')
84
+ @return = @nodes.attribute(c2)
85
+ assert(prm)
86
+ end
87
+
88
+ def _val(prm,c2='')
89
+ if !(@nodes.attribute("type")=='file' ||
90
+ @nodes.tag_name=="select")
91
+ @nodes.clear
92
+ end
93
+ @return = @nodes.send_keys(prm[1,99])
94
+ end
95
+
96
+ def _click(prm,c2='')
97
+ @return = @nodes.click
98
+ if prm[0]=='|'
99
+ prm = prm[1,99]
100
+ splt = prm[/(<|>|=)=|(=|!)~/]
101
+ line = prm.strip.split(splt,2)
102
+ if line.length==2
103
+ if (parse_cmd(line[0]) && eval("\"#{@return}\" #{splt} /#{line[1]}/"))
104
+ print '.'.green if !opt_v
105
+ else
106
+ print 'F'.red if !opt_v
107
+ end
108
+ end
109
+ end
110
+ rescue Exception => e
111
+ failed
112
+ end
113
+
114
+ def _text(prm,c2='')
115
+ @return = @nodes.text
116
+ assert(prm)
117
+ end
118
+
119
+ protected
120
+
121
+ def assert(prm)
122
+ if prm[0,2]=='->'
123
+ if @return =~ /#{prm[2,99]}/
124
+ print '.'.green if !opt_v
125
+ else
126
+ failed
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,84 @@
1
+ class SeleniumDsl
2
+ module Engines
3
+ def engines
4
+ SeleniumDsl::Engines.private_instance_methods.collect do |x|
5
+ "#{x[1,99]}"
6
+ end
7
+ end
8
+
9
+ def in_engines?(cmd)
10
+ engines.index(cmd) ? cmd : nil
11
+ end
12
+
13
+ def parse_eng(line)
14
+ arr = match_line(@r_eng,line.strip,'eng')
15
+ cmd,prm = arr
16
+ if (exc = in_engines?(cmd[0]))
17
+ puts "#{@path}>eng: #{arr.inspect}" if opt_v
18
+ send("_#{exc}",prm) if !@mock
19
+ true
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+ private
26
+ def _chrome(prm)
27
+ @driver = Selenium::WebDriver.for :chrome
28
+ end
29
+
30
+ def _firefox(prm)
31
+ @driver = Selenium::WebDriver.for :firefox
32
+ end
33
+
34
+ def _phantomjs(prm)
35
+ prm = "http://localhost:8080" if prm==''
36
+ @driver = Selenium::WebDriver.for(:remote, :url => prm)
37
+ end
38
+
39
+ def _remote(prm)
40
+ prm = "http://localhost:4444/wd/hub/" if prm==''
41
+ @driver = Selenium::WebDriver.for(:remote, :url => prm)
42
+ end
43
+
44
+ def _mock(prm)
45
+ @mock = true
46
+ end
47
+
48
+ def _debug(prm)
49
+ binding.pry
50
+ end
51
+
52
+ def _visit(prm)
53
+ _firefox('') if !@driver
54
+ @driver.get(prm)
55
+ end
56
+
57
+ def _wait(prm)
58
+ wait = Selenium::WebDriver::Wait.new(:timeout => 10)
59
+ wait.until { @driver.find_element(:css,'title').text =~ /#{prm}/ }
60
+ end
61
+
62
+ def _quit(prm)
63
+ @driver.quit
64
+ end
65
+
66
+ def _if(prm)
67
+ splt = prm[/(<|>|=)=|(=|!)~/]
68
+ nest = 0
69
+ line = prm.split(splt,2)
70
+ if line.length==2
71
+ if !(parse_cmd(line[0]) && eval("\"#{@return}\" #{splt} /#{line[1]}/"))
72
+ while (line = _line_)
73
+ nest += 1 if line.strip =~ /^if/
74
+ if (line.strip =~ /^end$/)
75
+ break if nest==0
76
+ nest -=1
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,29 @@
1
+ class SeleniumDsl
2
+ module Commands
3
+ def parse_mcr(line)
4
+ arr = match_line(@r_mcr,line.strip,'mcr')
5
+
6
+ m_set,m_run,prm = arr
7
+ if (m_set!=[] || m_run!=[]) && !@mock
8
+ puts "#{@path}>mcr: #{arr.inspect}" if opt_v
9
+ @return = nil
10
+ c = _code_
11
+ if m_set!=[]
12
+ key = m_set[0].split('=')[0].strip
13
+ c[:macro][key] = prm
14
+ else
15
+ m = c[:macro][m_run[0]]
16
+ prm.split(',').each_with_index do |row,idx|
17
+ m.gsub!("$#{idx}",row)
18
+ end
19
+ l = c[:line]- 1
20
+ c[:line] = l
21
+ c[:code][l] = m
22
+ end
23
+ true
24
+ else
25
+ false
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,95 @@
1
+ class SeleniumDsl
2
+ module Modules
3
+
4
+ def modules
5
+ SeleniumDsl::Modules.private_instance_methods.collect do |x|
6
+ "#{x[1,99]}"
7
+ end
8
+ end
9
+
10
+ def in_modules?(cmd)
11
+ modules.index(cmd) ? cmd : nil
12
+ end
13
+
14
+ def parse_mod(line)
15
+ arr = match_line(@r_mod,line.strip,'mod')
16
+ cmd,prm = arr
17
+ if (exc = in_modules?(cmd[0]))
18
+ send("_#{exc}",prm) if !@mock
19
+ true
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+ # the regex @r_fnc will return with value in
26
+ # the first array, so it need to split for prm
27
+ def parse_fnc(line)
28
+ arr = match_line(@r_fnc,line.strip,'mod')
29
+ cmd,prm = arr[0][0].split(/ +/,2)
30
+ npath = "#{@path}/#{cmd}"
31
+ if @code.keys.index("~/#{cmd}")
32
+ puts "#{@path}>mod: #{arr.inspect}" if opt_v
33
+ prm = prm.to_s.split(',')
34
+ @path = npath
35
+
36
+ @code[@path] = @code["~/#{cmd}"] if !@code[@path]
37
+ @code[@path][:line] = 0
38
+
39
+ p = []
40
+ parm = @code[@path][:parm]
41
+ if parm && parm!=[]
42
+ parm.each_with_index do |v,i|
43
+ var = v.clone
44
+ var[1] = prm[i] if prm[i]
45
+ p << var
46
+ end
47
+ end
48
+ vars = Hash[p]
49
+ @code[@path][:vars] = vars
50
+ puts ">>vars: #{vars}" if opt_v
51
+ true
52
+ else
53
+ false
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def _def(prm)
60
+ # k,v = prm.split(/\(/,2)
61
+ # k.strip!
62
+ # v.sub!(/\)/,'') if v
63
+ k,v = prm.split(/ +/,2)
64
+ k.strip!
65
+ if v
66
+ v=v.split(',').collect do |x|
67
+ x.split(':')
68
+ end
69
+ puts ">>parm: #{v}" if opt_v
70
+ end
71
+ nest = 0
72
+ codes = []
73
+ while (line = _line_)
74
+ nest += 1 if line.strip =~ /^if/
75
+ if line.strip =~ /^end$/
76
+ break if nest==0
77
+ nest -=1
78
+ end
79
+ puts ">>#{line.inspect}" if opt_v
80
+ codes << line
81
+ end
82
+ @code["#{@path}/#{k}"] =
83
+ {
84
+ :macro => {},
85
+ :code => codes,
86
+ :line => 0,
87
+ :parm => v
88
+ }
89
+ end
90
+
91
+ def _end(prm)
92
+ #
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,126 @@
1
+ require 'selenium-webdriver'
2
+ require 'selenium_dsl/commands'
3
+ require 'selenium_dsl/engines'
4
+ require 'selenium_dsl/modules'
5
+ require 'selenium_dsl/macros'
6
+ require 'pry'
7
+
8
+ require 'term/ansicolor'
9
+ include Term::ANSIColor
10
+
11
+ class SeleniumDsl
12
+ class << self
13
+ include Commands
14
+ include Engines
15
+ include Modules
16
+
17
+ def init
18
+ nodes = /^(\>*([\-\w]*(\.[.\[\]\-\d\w]+|[#:][\-\d\w]+)|[\d\w]+)|\/\w+)(\[\d+\])*/
19
+ action = /^[~]\w+(\:\w+)*/
20
+ @driver = nil
21
+ @nodes = nil
22
+ @return = nil
23
+ @mock = false
24
+ @code = {}
25
+ @path = '~'
26
+ @opt = ''
27
+ @r_eng = [/^(mock|debug|chrome|firefox|remote|phantomjs|visit|wait|quit|if)/]
28
+ @r_mcr = [/^\$[\-\w]+ *\=/,/^\$[\-\w]+[^\=]/]
29
+ @r_cmd = [nodes, action]
30
+ @r_mod = [/^(def +|end)/]
31
+ @r_fnc = [/^ *\w+.*/]
32
+ end
33
+
34
+ def match_line(rgx,str,id='')
35
+ str2 = str
36
+ arr = rgx.collect do |r|
37
+ rtn = []
38
+ while (x=str[r])
39
+ str = str[x.length,255].strip
40
+ rtn << x.strip
41
+ end
42
+ rtn
43
+ end << str
44
+ arr
45
+ end
46
+
47
+ def parser(codes, opt='')
48
+ init if !@driver
49
+ @opt = opt
50
+ codes = codes.split(/\n/)
51
+ puts "OPT: #{@opt}" if opt
52
+ @code[@path] =
53
+ {
54
+ :macro => {},
55
+ :code => codes,
56
+ :vars => {},
57
+ :line => 0
58
+ }
59
+
60
+ while (line = _line_)
61
+ stx = parse_mod(line)
62
+ stx = parse_mcr(line) if !stx
63
+ stx = parse_eng(line) if !stx
64
+ stx = parse_cmd(line) if !stx
65
+ stx = parse_fnc(line) if !stx
66
+ end
67
+ print "\n"
68
+ end
69
+
70
+ private
71
+
72
+ def opt_v
73
+ @opt=~/\-[v]/
74
+ end
75
+
76
+ def _code_
77
+ @code[@path]
78
+ end
79
+
80
+ # try to the next line of code
81
+ def _line_
82
+ # change path up if eol of code
83
+ while (@path.scan('/')!=[])
84
+ c = @code[@path]
85
+ l = c[:line]
86
+ break if c[:code][l]
87
+ @path.sub!(/\/\w+$/,'')
88
+ end
89
+ c = @code[@path]
90
+ l = c[:line]
91
+ while c[:code][l] && c[:code][l].to_s.strip=='' #next line should not empty
92
+ l= (c[:line]+=1)
93
+ p "L: #{l}"
94
+ end
95
+ if c[:code][l] # not eol?
96
+ c[:line]+=1
97
+ c = Marshal.load( Marshal.dump(c) )
98
+ @line = c
99
+ v = c[:vars]
100
+ r = c[:code][l]
101
+ v.each do |k,v|
102
+ r.gsub!("&#{k}",v)
103
+ end
104
+ r
105
+ else
106
+ nil
107
+ end
108
+ end
109
+
110
+ def failed
111
+ print 'F'.red if !opt_v
112
+ c = _code_
113
+ l = c[:line]
114
+ r = c[:code]
115
+ y = 2
116
+ puts "\n=====>>>>ASSERT FAILED!!!<<<<=====".yellow
117
+ y.times do |idx|
118
+ no = (l-y)+idx
119
+ tx = r[no].send(idx<(y-1) ? :green : :red)
120
+ puts "#{no+1}. #{tx}"
121
+ end
122
+ puts "#{l+1}. #{r[l]}" if r[l]
123
+ Kernel.exit(1)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib",__FILE__)
2
+ require 'selenium_dsl/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "selenium-dsl"
6
+ s.version = SeleniumDsl::Version
7
+ s.author = ["WHarsojo"]
8
+ s.email = ["wharsojo@gmail.com"]
9
+ s.homepage = "http://github.com/wharsojo/selenium-dsl"
10
+ s.summary = %q{Simple DSL for Selenium}
11
+
12
+ s.rubyforge_project = "selenium-dsl"
13
+
14
+ s.files = %w[Gemfile Gemfile.lock Rakefile selenium-dsl.gemspec bin/sd bin/selenium-dsl lib/selenium_dsl.rb lib/selenium_dsl/commands.rb lib/selenium_dsl/engines.rb lib/selenium_dsl/modules.rb lib/selenium_dsl/macros.rb]
15
+
16
+ s.test_files = []
17
+
18
+ s.require_paths = ["lib"]
19
+ s.executables = ["sd","selenium-dsl"]
20
+ s.add_runtime_dependency "selenium-webdriver"
21
+ s.add_runtime_dependency "pry"
22
+ s.add_runtime_dependency "term-ansicolor"
23
+ s.post_install_message = ">>Enjoy your SDSL!!!<<"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selenium-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - WHarsojo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: selenium-webdriver
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: term-ansicolor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description:
63
+ email:
64
+ - wharsojo@gmail.com
65
+ executables:
66
+ - sd
67
+ - selenium-dsl
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - Rakefile
74
+ - selenium-dsl.gemspec
75
+ - bin/sd
76
+ - bin/selenium-dsl
77
+ - lib/selenium_dsl.rb
78
+ - lib/selenium_dsl/commands.rb
79
+ - lib/selenium_dsl/engines.rb
80
+ - lib/selenium_dsl/modules.rb
81
+ - lib/selenium_dsl/macros.rb
82
+ homepage: http://github.com/wharsojo/selenium-dsl
83
+ licenses: []
84
+ post_install_message: ! '>>Enjoy your SDSL!!!<<'
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project: selenium-dsl
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Simple DSL for Selenium
106
+ test_files: []