coba-testrunner 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/coba +123 -0
  3. data/lib/coba-testrunner.rb +136 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 887a6c9c0812137ba8baf5cb172af9db0bc3edbf
4
+ data.tar.gz: f76985062fc9b73d25933a9873a8d15be30e5db5
5
+ SHA512:
6
+ metadata.gz: fb292c33461d3a2b79e658fdb95747bf5e3701a3c70203dc6c0876a3f690e8b4da51de61be572ae7143653d3b48d0c29156b892225c8cd42201da8547518609d
7
+ data.tar.gz: b22430cca1ba24185e344cd1ba5f9b8d0194c15682fe948c22742c8728e15d8a7bf4104f266ff1db924c6b7446a1f2702b7dc5d230c564fd6deb1344309bb70b
data/bin/coba ADDED
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "thor"
4
+ require "json"
5
+ require "coba-testrunner"
6
+
7
+ class Coba < Thor
8
+ desc "test configFile [browser]", "Testing based on configFile and/or specific Browser"
9
+ long_desc <<-LONGDESC
10
+ coba test will test your test suite agains specific browser and input
11
+
12
+ You can optionally specify browser and this browser will override browser that you specify
13
+ on configFile
14
+
15
+ ex :\n
16
+ > $./coba test configFile.json\n
17
+ > $./coba test configFile.json firefox
18
+ LONGDESC
19
+ def test(configFile, browser=nil)
20
+ puts "Testing based on #{configFile}"
21
+
22
+ # 1. read configFile
23
+ # error when not found
24
+ # error when json not valid
25
+
26
+ if File.exist?(configFile)
27
+ file = File.read(configFile)
28
+ else
29
+ raise "File #{configFile} doesn't exist"
30
+ end
31
+
32
+ config = JSON.parse(file)
33
+
34
+ # 2. get test Cases
35
+ # error when not found
36
+ # error when json not valid
37
+
38
+ if File.exist?(config['data'])
39
+ testFile = File.read(config['data'])
40
+ else
41
+ raise "File #{config['data']} doesn't exist"
42
+ end
43
+
44
+ testCases = JSON.parse(testFile)
45
+
46
+ # 3. get browser
47
+ # isArray? isString?
48
+ # String push ke array
49
+ # Array loop
50
+ arrayOfBrowser = Array.new
51
+ if config['browsers'].instance_of? String
52
+ arrayOfBrowser << config['browsers'];
53
+ else
54
+ arrayOfBrowser = config['browsers'];
55
+ end
56
+
57
+ arrayOfBrowser.each_with_index do |browser,index|
58
+ # 4. Create config for single browser
59
+ singleBrowserConfig = config;
60
+ singleBrowserConfig["browsers"] = browser
61
+
62
+ # variable untuk menyimpan hasil
63
+ logs = Array.new
64
+
65
+ # 5. Loop Test Cases hasil generate
66
+ testCases.each_with_index do |testCase,testIndex|
67
+ # 5. Create object Test
68
+ singleBrowserConfig['keyIndex'] = testIndex
69
+ test = TestRunner.new(singleBrowserConfig)
70
+
71
+ # - init
72
+ test.init()
73
+ # driver init
74
+
75
+ # screenshot after init (if config set to true)
76
+ # - start
77
+ test.start(testCase)
78
+ # - before Hook (if available)
79
+ # Log Time
80
+ # screenshot before and after test (if config set to true)
81
+ # - Execution
82
+ # Log Time
83
+ # screenshot before and after test (if config set to true)
84
+ # - after Hook (if available)
85
+ # Log Time
86
+ # screenshot before and after test (if config set to true)
87
+ # - stop
88
+ result = test.stop()
89
+ # driver quit
90
+
91
+ # compose result
92
+ logs.push(result)
93
+ end
94
+
95
+ # 6. Ketika selesai atau throw error save log (JSON) ke dalam sebuah file dengan alamat cwd()+log+browserName
96
+ logPath = "#{Dir.pwd}/#{browser}"
97
+ if !Dir.exist? "#{logPath}/"
98
+ FileUtils::mkdir_p "#{logPath}/"
99
+ end
100
+
101
+ File.open("#{logPath}/#{browser}.json","w") do |f|
102
+ f.write(JSON.pretty_generate logs)
103
+ end
104
+ end
105
+
106
+
107
+ end
108
+
109
+
110
+ desc "generate type", "[IN PROGRESS] Generate data for testing purpose"
111
+ long_desc <<-LONGDESC
112
+ coba generate will generate data for your test suite base on validation rule/something else :p
113
+
114
+ ex :\n
115
+ > $./coba generate input\n
116
+ > $./coba generate output
117
+ LONGDESC
118
+ def generate(type)
119
+ puts "Generate on #{type} data"
120
+ end
121
+ end
122
+
123
+ Coba.start(ARGV)
@@ -0,0 +1,136 @@
1
+ require "selenium-webdriver"
2
+ require 'fileutils'
3
+
4
+ class TestRunner
5
+ def initialize(config)
6
+ @config = config
7
+ @driver = ""
8
+ @time = Hash.new
9
+ @index = config['keyIndex']
10
+ @testCase = ""
11
+ end
12
+ def init()
13
+ start = Time.new
14
+
15
+ case @config['browsers']
16
+ when "firefox"
17
+ @driver = Selenium::WebDriver.for :firefox, :profile => "Driver"
18
+ when "safari"
19
+ @driver = Selenium::WebDriver.for :safari
20
+ when "opera"
21
+ @driver = Selenium::WebDriver.for :opera
22
+ when "chrome"
23
+ @driver = Selenium::WebDriver.for :chrome
24
+ when "ie"
25
+ @driver = Selenium::WebDriver.for :ie
26
+ when "phantomjs"
27
+
28
+ else
29
+ puts "Browser you specified not supported yet"
30
+ end
31
+
32
+ finish = Time.new
33
+
34
+ logTime "initialize", start, finish
35
+ end
36
+ def start(testCase=nil)
37
+ @testCase = testCase
38
+
39
+ beforeExecution
40
+
41
+ loadUrl(@config['startingUrl'])
42
+
43
+ mainTest(testCase)
44
+
45
+ afterExecution
46
+ end
47
+ def stop
48
+ @driver.quit
49
+ @testCase['time'] = @time
50
+ return @testCase
51
+ end
52
+ def config
53
+ @config
54
+ end
55
+
56
+ private #Private method dibawah
57
+ def logTime(index,start,finish)
58
+ @time[index] = Hash.new
59
+ @time[index]['start'] = start
60
+ @time[index]['finish'] = finish
61
+ @time[index]['time'] = finish - start
62
+ end
63
+ def loadUrl(url)
64
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'loadUrl','start')
65
+
66
+ start = Time.new
67
+
68
+ if url
69
+ @driver.navigate.to url
70
+ end
71
+
72
+ finish = Time.new
73
+
74
+ logTime "load", start, finish
75
+
76
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'loadUrl','finish')
77
+ end
78
+ def mainTest(testCase)
79
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'MainExecution','start')
80
+
81
+ start = Time.new
82
+
83
+ if @config['scenario'] and File.exist?(@config['scenario'])
84
+ load @config['scenario'] #work LOL
85
+ test = Test.new(testCase,@driver).run
86
+ @testCase['assertion'] = test
87
+ end
88
+
89
+ finish = Time.new
90
+
91
+ logTime "main", start, finish
92
+
93
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'MainExecution','finish')
94
+ end
95
+ def beforeExecution
96
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'PreExecution','load')
97
+
98
+ start = Time.new
99
+
100
+ if @config['before'] and File.exist?(@config['before'])
101
+ load @config['scenario'] #work LOL
102
+ test = BeforeTest.new(@driver).run
103
+ end
104
+
105
+ finish = Time.new
106
+
107
+ logTime "before", start, finish
108
+
109
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'PreExecution','finish')
110
+ end
111
+ def afterExecution
112
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'AfterExecution','load')
113
+
114
+ start = Time.new
115
+
116
+ if @config['after'] and File.exist?(@config['after'])
117
+ load @config['scenario'] #work LOL
118
+ test = AfterTest.new(@driver).run
119
+ end
120
+
121
+ finish = Time.new
122
+
123
+ logTime "after", start, finish
124
+
125
+ takeScreenshot("#{Dir.pwd}/#{@config['browsers']}/#{@index+1}",'AfterExecution','finish')
126
+ end
127
+ def takeScreenshot(folderPath,prefix=nil,sufix=nil)
128
+ if @config['screenshot']
129
+ if !Dir.exist? "#{folderPath}/"
130
+ FileUtils::mkdir_p "#{folderPath}/"
131
+ end
132
+ puts "#{folderPath}/#{prefix}.png"
133
+ @driver.save_screenshot("#{folderPath}/#{prefix}-#{sufix}.png")
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coba-testrunner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sunu Pinasthika Fajar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: run test case agains some config
14
+ email: sunupf@gmail.com
15
+ executables:
16
+ - coba
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/coba
21
+ - lib/coba-testrunner.rb
22
+ homepage: ''
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: test runner for selenium-webdriver
46
+ test_files: []