imperiusgem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a53a602fbae833352a7407e23500ec942b5e89a
4
+ data.tar.gz: 644179a412ac57038805331ff9ef009bd80d31af
5
+ SHA512:
6
+ metadata.gz: c1caef414b943ce3ddba07a8a895045a3c3ef5027a12eac9be81c70f0a91154d312c3c89082ee8c131eab60fd7495c3801ea9cc709c29c9ad8463103d2844cc1
7
+ data.tar.gz: 4868aea33948b88487eb9e6fc877d7daa5b2efb95dbd5737d4be1bf5ebeedcfd4373cc3d7ab3dc84735021e19a3ed64472b48fa5abdb8cc4727897b7baddd160
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ ![funny george image](https://github.com/lookout/ImperiusGeorge/raw/master/libs/george.jpg)
2
+ ### ImperiusGem
3
+ #####Remote-control/execute Java on Android phones via Ruby
4
+
5
+ 1. Add this gem to your project (add this to your Gemfile): `gem 'imperiusgem', :git => 'git@github.com:lookout/ImperiusGem.git', :branch => 'master'`
6
+ 2. Use `require 'imperiusgem'` in your ruby code.
7
+ 3. Connect your android device using adb
8
+ 4. Run `adb forward tcp:#1337 tcp:#1337`
9
+ 5. Compile [ImperiusGeorge](https://github.com/lookout/ImperiusGeorge) and push the .jar file to `/data/local/tmp/test.jar`
10
+ 6. Run `adb shell uiautomator runtest test.jar -c imperiusgeorge.TestServer`
11
+ 7. That's it! Try some sample code!
12
+
13
+ ###Simple example
14
+ ```
15
+ d = ImperiusGem::Device.new("http://localhost:7120")
16
+ uihelp = ImperiusGem::Thing.new(d,"imperiusgeorge.UIHelp")
17
+ uihelp.click("hello world")
18
+ ```
19
+
20
+ ###Fancy unit test example
21
+ ```
22
+ class TestTests < Test::Unit::TestCase
23
+ def test_something
24
+ UiDevice.getInstance.pressHome
25
+ `adb shell am start -n com.android.settings/.Settings`
26
+ UIHelp.find("Sound").click if UIHelp.find("Sound").exists
27
+ UIHelp.clickIfExactExists("Volumes")
28
+ UIHelp.clickIfExactExists("OK")
29
+ UiDevice.getInstance.pressHome
30
+ @d.clean
31
+ end
32
+
33
+ def self.const_missing(name)
34
+ @d = ImperiusGem::Device.new("http://localhost:7120") if !@d
35
+ @d.setPackages(["imperiusgeorge", "com.android.uiautomator.core"])
36
+ ImperiusGem::Thing.new(@d,name.to_s)
37
+ end
38
+ end
39
+ ```
data/lib/device.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'typhoeus'
3
+ require 'json'
4
+ require "#{File.dirname(__FILE__)}/thing.rb"
5
+
6
+ module ImperiusGem
7
+ class Device
8
+
9
+ def initialize(url)
10
+ @url = url
11
+ end
12
+
13
+ def run(on,method,args=[])
14
+ res = Device.http(:get,"#{@url}/execute",{:on => on, :method => method, :args => "#{[*args].to_json}"})
15
+ raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
16
+ res = JSON.parse("[#{res.response_body}]").first
17
+ return (res.to_s.start_with?("hash:")) ? ImperiusGem::Thing.new(self,res) : res
18
+ end
19
+
20
+ def find(name)
21
+ ImperiusGem::Thing.new(self,name)
22
+ end
23
+
24
+ def version
25
+ res = Device.http(:get,"#{@url}/version",{})
26
+ raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
27
+ res.response_body
28
+ end
29
+
30
+ def clean
31
+ res = Device.http(:get,"#{@url}/clean",{})
32
+ raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
33
+ end
34
+
35
+ def terminate
36
+ res = Device.http(:get,"#{@url}/terminate",{})
37
+ raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
38
+ end
39
+
40
+ def setPackages(packages={})
41
+ res = Device.http(:post,"#{@url}/packages",{:packages => packages.to_json})
42
+ raise "Error #{res.response_code} (url was #{res.effective_url}).\n#{res.response_body}" if (res.response_code != 200)
43
+ end
44
+
45
+ def self.http(method,url,params,body="",headers={})
46
+ headers.merge!({ :Accept => "application/json" })
47
+ request = Typhoeus::Request.new( url,
48
+ :method => method,
49
+ :body => body,
50
+ :headers => headers,
51
+ :params => params #:verbose => Context.verbose
52
+ )
53
+ res = request.run
54
+ end
55
+
56
+ end #class
57
+
58
+ def self.connect(device_id="")
59
+ devices=`adb devices`.split( /\r?\n/ )[1..-1].map!{|d| d.split("\t")[0]}
60
+ device_id = devices[0] if (device_id.to_s == '')
61
+ port = ((rand()*10000)+1000).to_i
62
+ puts "using port #{port} with device #{device_id}".green.on_white
63
+
64
+ `adb -s #{device_id} forward tcp:#{port} tcp:#{port}`
65
+ Thread.new { `adb -s #{device_id} shell uiautomator runtest UiAutoServer.jar -c TestServer -e port #{port}` }
66
+
67
+ d = Device.new("http://localhost:#{port}")
68
+ d.setPackages(["com.android.uiautomator.core"])
69
+ at_exit { d.terminate }
70
+ end
71
+ end #module
@@ -0,0 +1 @@
1
+ require 'device'
data/lib/thing.rb ADDED
@@ -0,0 +1,22 @@
1
+
2
+ module ImperiusGem
3
+ class Thing
4
+
5
+ def initialize(d,on)
6
+ @d = d
7
+ @on = on
8
+ end
9
+
10
+ def on; @on; end;
11
+
12
+ def method_missing(meth, *args, &block)
13
+ method = meth.to_s
14
+ super if method == "initialize"
15
+ @d.run(@on,method,fix_args(args))
16
+ end
17
+
18
+ def fix_args(args)
19
+ args.map { |arg| (arg.is_a? Thing) ? arg.on : arg }
20
+ end
21
+ end
22
+ end #module
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imperiusgem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim O'Brien
8
+ - Rustin Manafian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Control all of java and UI-Automator from ruby. See github homepage for
15
+ more info!
16
+ email:
17
+ - timo@t413.com
18
+ - rmanafian@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files:
22
+ - README.md
23
+ files:
24
+ - lib/imperiusgem.rb
25
+ - lib/device.rb
26
+ - lib/thing.rb
27
+ - README.md
28
+ homepage: https://github.com/lookout/ImperiusGem
29
+ licenses:
30
+ - AGPL
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.0.rc.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Control all of java and UI-Automator from ruby
52
+ test_files: []