cbthelper 0.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c0d344eb5bed1594c52cb3a3f793c643af8866e3a1cba768c5c6a52445326f64
4
+ data.tar.gz: 8c8d0263e924b9b4602c79138289b0d54ab2d7e597e7282ec0ac03b83478172e
5
+ SHA512:
6
+ metadata.gz: b98849177569e7c854e0aef42859dedd875fe836b8b561d03891c34ae18df47601012faec9e2f52d39e05f751815494c9c50b785bdbb3cc10fd0d3406be248ea
7
+ data.tar.gz: b3fa1206472fb7c7be48533e5cd8c8fd9e2f189d5628d36680b1f689c37981b7668a106d8bb6723cfff498fda2865d76b7e61ad45b1efff4c5ba9b01d3767bee
@@ -0,0 +1,48 @@
1
+ require_relative "cbthelper/Globals"
2
+ require_relative "cbthelper/AutomatedTest"
3
+ require_relative "cbthelper/CapsBuilder"
4
+ require_relative "cbthelper/Snapshot"
5
+ require_relative "cbthelper/TestHistoryBuilder"
6
+ require_relative "cbthelper/Video"
7
+ require "rest-client"
8
+ require "json"
9
+ def getCapsBuilder
10
+ =begin
11
+ Used to get the selenium capability builder
12
+ Generating the CapsBuilder pulls in a large amount of data, so user should not call the constrcutor manually
13
+ =end
14
+ $CAPSBUILDER = CapsBuilder.new
15
+ return $CAPSBUILDER
16
+ end
17
+
18
+
19
+ def login(username, authkey)
20
+ #Sets the username and authkey used to make the HTTP requests
21
+ $USERNAME = username
22
+ $AUTHKEY = authkey
23
+
24
+ end
25
+
26
+ def getTestHistoryBuilder
27
+ =begin
28
+ Used to get the TestHistoryBuilder
29
+ Can also just call the constructor. Method created to match getCapsBuilder()
30
+ =end
31
+ return TestHistoryBuilder.new
32
+ end
33
+
34
+ def getTestHistory(options)
35
+ =begin
36
+ Returns a ruby hash with the test history, filtering based on the options given.
37
+ :param options: a ruby hash created by the TestHistoryBuilder
38
+ =end
39
+ return JSON.parse(RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/"))
40
+ end
41
+
42
+ def getTestFromId(sessid)
43
+ =begin
44
+ Creates an automated test from the selenium session id
45
+ :param sessid: string for the seleneium session/test id. Should come from WebDriver
46
+ =end
47
+ return AutomatedTest.new(sessid)
48
+ end
@@ -0,0 +1,133 @@
1
+ require_relative "Globals"
2
+ require_relative "Snapshot"
3
+ require_relative "Video"
4
+ require "rest-client"
5
+ require "json"
6
+
7
+ class AutomatedTest
8
+ =begin
9
+ Helpful representation of a selenium test
10
+ :param testId: the selenium session ID, usually from webdriver
11
+
12
+ =end
13
+ def initialize(testId)
14
+ @testId = testId
15
+ end
16
+
17
+ def setScore(score)
18
+ =begin
19
+ Sets the score for our test in the CBT app
20
+ :param score: should be 'pass', 'fail', or 'unset'. The main module exposes SCORE_PASS, SCORE_FAIL, SCORE_UNSET
21
+ =end
22
+ RestClient.put("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3"+ "/selenium/#{@testId}",
23
+ "action=set_score&score=#{score}")
24
+ end
25
+
26
+ def setDescription(description)
27
+ #Sets the description for the test in the web app
28
+ RestClient.put("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3"+ "/selenium/#{@testId}",
29
+ "action=set_description&description=#{description}")
30
+ end
31
+
32
+ def stop(score = "")
33
+ =begin
34
+ score is optional, will combine setScore and stopTest
35
+ Sends the command to our api to stop the selenium test. Similar to driver.quit()
36
+ =end
37
+ if score != ""
38
+ setScore(score)
39
+ RestClient.delete("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/#{@testId}")
40
+ end
41
+
42
+ end
43
+
44
+ def takeSnapshot(description= "")
45
+ =begin
46
+ Sends the command to take a snapshot and returns a Snapshot instance
47
+ :param description: (optional) shortcut for Snapshot.setDescription
48
+ :returns: the Snapshot instance for this snapshot
49
+ =end
50
+ response = RestClient.post("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3" + "/selenium/#{@testId}/snapshots",
51
+ "selenium_test_id=#{@testId}")
52
+ hash = /(?<="hash": ")((\w|\d)*)/.match(response)[0]
53
+ snap = Snapshot.new(hash, @testId)
54
+ if description != ""
55
+ snap.setDescription(description)
56
+ end
57
+ return snap
58
+
59
+ end
60
+
61
+ def getSnapshots()
62
+ # Returns all snapshots for this test
63
+ snaps = JSON.parse(RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId +"/snapshots/"))
64
+ ret = []
65
+ for snap in snaps
66
+ ret.push(Snapshot.new(snap["hash"], @testId))
67
+
68
+ end
69
+ return ret
70
+ end
71
+
72
+ def saveAllSnapshots(directory, useDescription=false)
73
+ #Downloads all snapshots for this test into the provided directory
74
+ prefix = "image"
75
+ snaps = getSnapshots
76
+ makeDirectory(directory)
77
+ for i in 0...snaps.size
78
+ if useDescription and snaps[i].info["description"] != ""
79
+ img = snaps[i].info["description"] + ".png"
80
+ else
81
+ img = prefix + i.to_s + ".png"
82
+ end
83
+ snaps[i].saveLocally(File.join(directory, img))
84
+ end
85
+
86
+ end
87
+
88
+ def RecordingVideo(description='')
89
+ #Return the video recording for this test
90
+ response = RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3" + "/selenium/#{@testId}/videos")
91
+ hash = /(?<="hash": ")((\w|\d)*)/.match(response)[0]
92
+ video = Video.new(hash, @testId)
93
+ if description != ""
94
+ video.setDescription(description)
95
+ end
96
+ return video
97
+
98
+ end
99
+
100
+ def getVideos
101
+ # Returns all videos for this test
102
+ videos = JSON.parse(RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId +"/videos/"))
103
+ ret = []
104
+ for video in videos
105
+ ret.push(Video.new(video["hash"], @testId))
106
+
107
+ end
108
+ return ret
109
+ end
110
+
111
+ def saveAllVideos(directory, useDescription=false)
112
+ # Downloads all videos for this test into a directory
113
+ prefix = "video"
114
+ videos = getVideos
115
+ makeDirectory(directory)
116
+ for i in 0...videos.size
117
+ if useDescription and videos[i].info["description"] != ""
118
+ vid = videos[i].info["description"] + ".mp4"
119
+ else
120
+ vid = prefix + i.to_s + ".mp4"
121
+ end
122
+ videos[i].saveLocally(File.join(directory, vid))
123
+ end
124
+
125
+ end
126
+
127
+
128
+ def makeDirectory(dir)
129
+ Dir.mkdir(dir) unless Dir.exist?(dir)
130
+
131
+ end
132
+
133
+ end
@@ -0,0 +1,153 @@
1
+ require "rest-client"
2
+ require "json"
3
+ require_relative "Globals"
4
+
5
+ class CapsBuilder
6
+ =begin
7
+ Builder for generating selenium capabilities
8
+ All of the with... methods return self for method chaining
9
+ =end
10
+ def initialize
11
+ @capsData = JSON.parse(RestClient.get("https://crossbrowsertesting.com/api/v3/selenium/browsers"))
12
+ @platform = nil
13
+ @browser = nil
14
+ @browserVersion = nil
15
+ @width = nil
16
+ @height = nil
17
+ @name = nil
18
+ @version = nil
19
+ @recordVideo = nil
20
+ @recordNetwork = nil
21
+ end
22
+
23
+ def withPlatform(platform)
24
+ =begin
25
+
26
+ Sets the platform (OS) the user wants to use. The string will be compared against the 'name' and 'api_name' properties returned from the selenium api.
27
+ :param platform: a string specifying the platform (eg. Windows 7, Mac 10.13)
28
+ =end
29
+ @platform = platform
30
+ self
31
+ end
32
+
33
+ def withBrowser(browser)
34
+ =begin
35
+ Sets the browser the user wants to use. The string will be compared against the 'name' and 'api_name' properties returned from the selenium api.
36
+ :param browser: as string specifying the browser (eg. Edge 17, Chrome 55x64)
37
+ =end
38
+ @browser = browser
39
+ self
40
+ end
41
+
42
+ def withBrowserVersion(version)
43
+ # Sets the browser verson
44
+ @browserVersion = version
45
+ self
46
+ end
47
+
48
+ def withResolution(width, height)
49
+ # Sets the screen size for the test
50
+ @width = width
51
+ @height = height
52
+ self
53
+ end
54
+
55
+ def withName(name)
56
+ # Sets the name of the test in the web app
57
+ @name = name
58
+ self
59
+ end
60
+
61
+ def withBuild(build)
62
+ #Sets the build number in the web app
63
+ @version = build
64
+ self
65
+ end
66
+
67
+ def withRecordVideo(bool)
68
+ #Records a video for the length of the test
69
+ @recordVideo = bool
70
+ self
71
+ end
72
+
73
+ def withRecordNetwork(bool)
74
+ #Records network traffic for the length of the test
75
+ @recordNetwork = bool
76
+ self
77
+ end
78
+
79
+ def build
80
+ #Used to generate the capabilites using any options the user specifies
81
+ return choose
82
+ end
83
+
84
+ def bestOption(options, target)
85
+ #Determines the best platform based on user input :param target
86
+ if target != nil
87
+ target =target.downcase
88
+ end
89
+ for option in options
90
+ name = option['name'].downcase
91
+ apiName = option['name'].downcase
92
+ if target == name or target == apiName
93
+ return option
94
+ end
95
+ end
96
+ return nil
97
+ end
98
+
99
+ def bestBrowserNoPlatform(target)
100
+ #Determines the best platform based on user input :param target when no platform provided
101
+ target = target.downcase
102
+ for platform in @capsData
103
+ for browser in platform['browsers']
104
+ name = browser['name'].downcase
105
+ if target == name or target == apiName
106
+ return browser
107
+ end
108
+ end
109
+ end
110
+ return nil
111
+ end
112
+
113
+ def choose
114
+ #Sets the capabilities passed to the WebDriver
115
+ data = @capsData
116
+ caps = Selenium::WebDriver::Remote::Capabilities.new
117
+ if @platform
118
+ caps['platform'] = @platform
119
+ end
120
+ if @browser
121
+ caps['browser'] = @browser
122
+ end
123
+
124
+ if @browserVersion
125
+ caps['browser_version'] = @browserVersion
126
+ end
127
+
128
+ if @width and @height
129
+ caps['screenResolution'] = @width.to_s + 'x' + @height.to_s
130
+ end
131
+
132
+ if @name
133
+ caps['name'] = @name
134
+ end
135
+
136
+ if @version
137
+ caps['build'] = @version
138
+ end
139
+
140
+ if @recordVideo
141
+ caps['record_video'] = @recordVideo
142
+ end
143
+
144
+ if @recordNetwork
145
+ caps['record_network'] = @recordNetwork
146
+ end
147
+ return caps
148
+ end
149
+
150
+
151
+
152
+
153
+ end
@@ -0,0 +1,5 @@
1
+ $USERNAME = ""
2
+ $AUTHKEY = ""
3
+ $CAPSBUILDER = nil
4
+ $API = "https://crossbrowsertesting.com/api/v3/selenium/"
5
+ $HUB = "http://hub.crossbrowsertesting.com:80/wd/hub"
@@ -0,0 +1,58 @@
1
+ require_relative "Globals"
2
+ require "json"
3
+ require "rest-client"
4
+
5
+ class Snapshot
6
+ =begin
7
+ Represents a snapshot for selenium tests
8
+ :param hash: the hash for this image, returned by rest api when taking a screenshot
9
+ :param test: an AutomatedTest object that represents a test currently running
10
+ =end
11
+ attr_accessor :info
12
+ def initialize(hash, test)
13
+ @hash = hash
14
+ @testId = test
15
+ getInfo
16
+ end
17
+
18
+ def getInfo
19
+ =begin
20
+ Calls out to api to get updated info for this snapshot
21
+ :returns: a hash object with all of the info for this Snapshot
22
+
23
+ =end
24
+ @info = JSON.parse(RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId +"/snapshots/"+ @hash))
25
+ return @info
26
+ end
27
+ def setDescription(description)
28
+ #Sets the description for this snapshot
29
+
30
+ url = "https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId + "/snapshots/" + @hash
31
+ @info = RestClient.put(url, "description=#{description}")
32
+
33
+ end
34
+
35
+ def saveLocally(location)
36
+ #Downloads the snapshot to the given location
37
+ #:param location: a string with the location and filename for the image. Should have a .png extension
38
+ saveSnapshot(location)
39
+ end
40
+
41
+ def saveSnapshot(location)
42
+ url = getInfo["image"]
43
+ path = File.split(location)[0]
44
+ Dir.mkdir(path) unless Dir.exist?(path)
45
+
46
+ # downloads the image to the given location in chunks
47
+ File.open(location, "wb") {|f|
48
+ block = proc { |response|
49
+ response.read_body do |chunk|
50
+ f.write chunk
51
+ end
52
+ }
53
+ RestClient::Request.execute(method: :get,
54
+ url: url,
55
+ block_response: block)
56
+ }
57
+ end
58
+ end
@@ -0,0 +1,114 @@
1
+ class TestHistoryBuilder
2
+ =begin
3
+ Builder to generate options for getting test history
4
+ All of the with... methods return self for method chaining
5
+ =end
6
+ def initialize
7
+ @options = {}
8
+ end
9
+
10
+ def withLimit(limit)
11
+ # Sets the max number of tests to return
12
+ @options["num"] = limit
13
+ self
14
+ end
15
+
16
+ def withActive(active)
17
+ =begin
18
+ If set, will only return active or inactive tests
19
+ :param active: boolean value
20
+ =end
21
+ @options["active"] = active
22
+ self
23
+ end
24
+
25
+ def withName(name)
26
+ # Will only return tests that match the name given
27
+ @options["name"] = name
28
+ self
29
+ end
30
+
31
+ def withBuild(build)
32
+ # Will only return tests that match the build given
33
+ @options["build"] = build
34
+ self
35
+ end
36
+
37
+ def withUrl(url)
38
+ # Will only return tests that navigate to the same url
39
+ @options["url"] = url
40
+ self
41
+ end
42
+
43
+ def withScore(score)
44
+ #Will only return tests with the score specified ('pass', 'fail', 'unset')
45
+ @options["score"] = score
46
+ self
47
+ end
48
+
49
+ def withPlatform(platform)
50
+ =begin
51
+ Will only return tests with the same platform (OS)
52
+ :param platform: string with the platform (eg. 'Windows 10', 'Mac OS 10.13')
53
+ =end
54
+ @options["platform"] = platform
55
+ self
56
+ end
57
+
58
+ def withPlatformType(platformType)
59
+ =begin
60
+ Will only return tests with the same platformType (OS Family)
61
+ :param platformType: string with the platform type (eg. 'Windows', 'Mac', 'Android')
62
+ =end
63
+ @options["platformType"] = platformType
64
+ self
65
+ end
66
+
67
+ def withBrowser(browser)
68
+ =begin
69
+ Will only return tests that used the same browser
70
+ :param browser: a string with the browser name and version: (eg. Chrome 65)
71
+ =end
72
+ @options["browser"] = browser
73
+ self
74
+ end
75
+
76
+ def withBrowserType(browserType)
77
+ =begin
78
+ Will only return tests that used the same browser type
79
+ :param browserType: a string representing the browser family (eg. 'Chrome', 'Edge', 'Safari')
80
+ =end
81
+ @options["browserType"] = browserType
82
+ self
83
+ end
84
+
85
+ def withResolution(resolution)
86
+ =begin
87
+ Will only return tests that used the same resolution
88
+ :param resolution: a string with the form 'WIDTHxHEIGHT' (eg. '1024x768')
89
+
90
+ =end
91
+ @options["resolution"]= resolution
92
+ self
93
+ end
94
+
95
+ def withStartDate(startDate)
96
+ @options["startDate"] = startDate
97
+ self
98
+ end
99
+
100
+ def withEndDate(endDate)
101
+ @options["endDate"] = endDate
102
+ self
103
+ end
104
+
105
+ def build
106
+ =begin
107
+ Generates the test history options
108
+ :returns: a ruby hash to pass to getTestHistory()
109
+ =end
110
+ return @options
111
+ end
112
+
113
+ end
114
+
@@ -0,0 +1,70 @@
1
+ require_relative "Globals"
2
+ require "json"
3
+ require "rest-client"
4
+
5
+ class Video
6
+ =begin
7
+ Represents a video recording for a selenium test
8
+ :param hash: the hash for this video, returned by rest api when starting a recording
9
+ :param test: an AutomatedTest object that represents a test currently running
10
+ =end
11
+
12
+ attr_accessor :info
13
+ def initialize(hash, test)
14
+ @hash = hash
15
+ @testId = test
16
+ getInfo
17
+
18
+ end
19
+
20
+ def getInfo
21
+ =begin
22
+ Calls out to api to get updated info for this video
23
+ :returns: a hash object with all of the info for this video
24
+ =end
25
+ @info = JSON.parse(RestClient.get("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId +"/videos/"+ @hash))
26
+ return @info
27
+
28
+ end
29
+
30
+ def stopRecording
31
+ # Sends the command to stop a video recording
32
+ RestClient.delete("https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId +"/videos/"+ @hash)
33
+
34
+ end
35
+
36
+ def setDescription(description)
37
+ # Sets the description for this video
38
+ url = "https://#{$USERNAME}:#{$AUTHKEY}@crossbrowsertesting.com/api/v3/selenium/" + @testId + "/videos/" + @hash
39
+ @info = RestClient.put(url, "description=#{description}")
40
+
41
+ end
42
+
43
+ def saveLocally(location)
44
+ #:param location: a string with the location and filename for the video. Should have a .mp4 extension
45
+ saveVideo(location)
46
+ end
47
+ def saveVideo(location)
48
+ url = getInfo["video"]
49
+ if info["is_finished"] == false
50
+ stopRecording
51
+ end
52
+
53
+ #
54
+ path = File.split(location)[0]
55
+ Dir.mkdir(path) unless Dir.exist?(path)
56
+
57
+ # downloads the video to the given location in chunks
58
+ File.open(location, "wb") {|f|
59
+ block = proc { |response|
60
+ response.read_body do |chunk|
61
+ f.write chunk
62
+ end
63
+ }
64
+ RestClient::Request.execute(method: :get,
65
+ url: url,
66
+ block_response: block)
67
+ }
68
+
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbthelper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daphne Magsby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: cbthelper wraps CrossBrowserTesting's (https://crossbrowsertesting.com/apidocs/v3/selenium.html)
14
+ into an easy to use library.
15
+ email: daphnem@crossbrowsertesting.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cbthelper.rb
21
+ - lib/cbthelper/AutomatedTest.rb
22
+ - lib/cbthelper/CapsBuilder.rb
23
+ - lib/cbthelper/Globals.rb
24
+ - lib/cbthelper/Snapshot.rb
25
+ - lib/cbthelper/TestHistoryBuilder.rb
26
+ - lib/cbthelper/Video.rb
27
+ homepage: http://rubygems.org/gems/cbthelper
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.7.7
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: cbthelper
51
+ test_files: []