tabbyx 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tabbyx.rb +29 -0
- data/lib/tabbyx/calabash_android.rb +11 -0
- data/lib/tabbyx/calabash_ios.rb +4 -0
- data/lib/tabbyx/core/base.rb +54 -0
- data/lib/tabbyx/core/config.rb +36 -0
- data/lib/tabbyx/core/initialize.rb +35 -0
- data/lib/tabbyx/fixtures/constants.rb +21 -0
- data/lib/tabbyx/fixtures/global.rb +39 -0
- data/lib/tabbyx/helpers/csv_helper.rb +65 -0
- data/lib/tabbyx/helpers/debug_helper.rb +37 -0
- data/lib/tabbyx/helpers/excel_helper.rb +79 -0
- data/lib/tabbyx/helpers/http_batch_handler.rb +50 -0
- data/lib/tabbyx/helpers/http_helper.rb +46 -0
- data/lib/tabbyx/helpers/minitest_helper.rb +48 -0
- data/lib/tabbyx/helpers/screenshot_helpers.rb +128 -0
- data/lib/tabbyx/helpers/txt_helper.rb +35 -0
- data/lib/tabbyx/steps_android/assert_steps.rb +31 -0
- data/lib/tabbyx/steps_android/check_box_steps.rb +3 -0
- data/lib/tabbyx/steps_android/context_menu_steps.rb +17 -0
- data/lib/tabbyx/steps_android/date_picker_steps.rb +8 -0
- data/lib/tabbyx/steps_android/enter_text_steps.rb +23 -0
- data/lib/tabbyx/steps_android/location_steps.rb +7 -0
- data/lib/tabbyx/steps_android/navigation_steps.rb +47 -0
- data/lib/tabbyx/steps_android/press_button_steps.rb +39 -0
- data/lib/tabbyx/steps_android/progress_steps.rb +51 -0
- data/lib/tabbyx/steps_android/search_steps.rb +7 -0
- data/lib/tabbyx/steps_android/spinner_steps.rb +11 -0
- data/lib/tabbyx/steps_ios/assertions.rb +79 -0
- data/lib/tabbyx/steps_ios/date_picker.rb +39 -0
- data/lib/tabbyx/steps_ios/operations.rb +187 -0
- data/lib/tabbyx/steps_ios/wait.rb +48 -0
- data/lib/tabbyx/utils/adb_devices.rb +248 -0
- data/lib/tabbyx/utils/adb_screenshot.rb +22 -0
- data/lib/tabbyx/utils/code_coverage.rb +6 -0
- data/lib/tabbyx/utils/shell.rb +32 -0
- data/lib/tabbyx/version.rb +3 -0
- metadata +286 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d5f561c10b7d01338e3ac087878c9ef7e3a07381
|
4
|
+
data.tar.gz: 940d701ff81724a79638e4341cef3e750958ad8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7dc32fb6e82ab1c7a6cf319a3465523b8ca330c75ab8e57f2547217493e4bf7618a0e69e059de921f4399bcbd3a7f2595cbe5b0f14b3a432836b75f0af29fc5b
|
7
|
+
data.tar.gz: a4f70a93ff08205644694606429f60c187e73e702901b43f9cc862c62046d81408b6a1d657a752a8fde211156072173e6ad9cf9d8d1d142cf8ba78819a7ee9a4
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tabbyx"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/tabbyx.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "tabbyx/version"
|
2
|
+
|
3
|
+
|
4
|
+
require 'tabbyx/core/base'
|
5
|
+
require 'tabbyx/core/initialize'
|
6
|
+
require 'tabbyx/core/config'
|
7
|
+
|
8
|
+
require 'tabbyx/fixtures/constants'
|
9
|
+
require 'tabbyx/fixtures/global'
|
10
|
+
|
11
|
+
# helpers
|
12
|
+
require 'tabbyx/helpers/http_helper'
|
13
|
+
require 'tabbyx/helpers/csv_helper'
|
14
|
+
require 'tabbyx/helpers/excel_helper'
|
15
|
+
require 'tabbyx/helpers/txt_helper'
|
16
|
+
require 'tabbyx/helpers/screenshot_helpers'
|
17
|
+
|
18
|
+
# support
|
19
|
+
require 'tabbyx/utils/code_coverage'
|
20
|
+
|
21
|
+
# utilities
|
22
|
+
require 'tabbyx/utils/shell'
|
23
|
+
require 'tabbyx/utils/adb_devices'
|
24
|
+
require 'tabbyx/utils/adb_screenshot'
|
25
|
+
|
26
|
+
|
27
|
+
module Tabbyx
|
28
|
+
# Your code goes here...
|
29
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'tabbyx/steps_android/assert_steps'
|
2
|
+
require 'tabbyx/steps_android/check_box_steps'
|
3
|
+
require 'tabbyx/steps_android/context_menu_steps'
|
4
|
+
require 'tabbyx/steps_android/date_picker_steps'
|
5
|
+
require 'tabbyx/steps_android/enter_text_steps'
|
6
|
+
require 'tabbyx/steps_android/location_steps'
|
7
|
+
require 'tabbyx/steps_android/navigation_steps'
|
8
|
+
require 'tabbyx/steps_android/press_button_steps'
|
9
|
+
require 'tabbyx/steps_android/progress_steps'
|
10
|
+
require 'tabbyx/steps_android/search_steps'
|
11
|
+
require 'tabbyx/steps_android/spinner_steps'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'tabbyx/fixtures/constants'
|
3
|
+
|
4
|
+
|
5
|
+
module Base
|
6
|
+
|
7
|
+
# load all yaml files in config folder
|
8
|
+
def Base::load_yamls
|
9
|
+
config = File.absolute_path(CONFIG_PATH)
|
10
|
+
Dir[File.join(config, '*.yml')].map {|file| [File.basename(file, '.yml').to_s, YAML.load_file(file)]}
|
11
|
+
end
|
12
|
+
|
13
|
+
def Base::read_yaml(filename)
|
14
|
+
path = File.absolute_path(CONFIG_PATH)
|
15
|
+
file = File.join(path,"config","#{filename}.yml")
|
16
|
+
# puts file
|
17
|
+
if File.exist?(file)
|
18
|
+
YAML.load_file(file)
|
19
|
+
else
|
20
|
+
warn('cannot find the yaml file '+ filename)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def Base::create_directory(directory_name, path= INPUT_PATH)
|
25
|
+
path = File.absolute_path(path)
|
26
|
+
directory = File.join(path,"#{directory_name}")
|
27
|
+
unless File.directory?(directory)
|
28
|
+
Dir.mkdir(directory)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.create_file(file_name, file_path= INPUT_PATH)
|
33
|
+
path = File.absolute_path(file_path)
|
34
|
+
file = File.join(path,file_name)
|
35
|
+
unless File.exist?(file)
|
36
|
+
puts "create file..."
|
37
|
+
File.new(file, "a+")
|
38
|
+
end
|
39
|
+
file
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.file_exists?(filename, file_path= INPUT_PATH)
|
43
|
+
path = File.absolute_path(file_path)
|
44
|
+
file = File.join(path, filename)
|
45
|
+
File.exist?(file) ? file : nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.file(filename,file_path= INPUT_PATH)
|
49
|
+
path = File.absolute_path(file_path)
|
50
|
+
File.join(path, filename)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'tabbyx/core/base'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def stringify
|
5
|
+
inject({}) do |options, (key, value)|
|
6
|
+
options[key.to_s] = value.to_s
|
7
|
+
options
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Config
|
13
|
+
def Config::read_config(product, server, target)
|
14
|
+
|
15
|
+
users_yml = Base.read_yaml('users')
|
16
|
+
hosts_yml = Base.read_yaml('hosts')
|
17
|
+
server_yml = Base.read_yaml('server')
|
18
|
+
password_yml = Base.read_yaml('passwords')
|
19
|
+
app_yml = Base.read_yaml('app')
|
20
|
+
device_yml = Base.read_yaml('devices')
|
21
|
+
config = {
|
22
|
+
:server => (Base.read_yaml('server')[server] if server_yml),
|
23
|
+
:user => (Base.read_yaml('users')[product][server] if users_yml),
|
24
|
+
:password => (Base.read_yaml('passwords')[server] if password_yml),
|
25
|
+
:app => (Base.read_yaml('app')[product] if app_yml),
|
26
|
+
:device => (Base.read_yaml('devices')[target] if device_yml),
|
27
|
+
:host => (Base.read_yaml('hosts')[product][server] if hosts_yml),
|
28
|
+
}
|
29
|
+
|
30
|
+
# Make sure everything is strings
|
31
|
+
config.each do |key, value|
|
32
|
+
config[key] = value.stringify if value.respond_to?(:stringify)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'tabbyx/core/base'
|
2
|
+
|
3
|
+
module Tabby
|
4
|
+
|
5
|
+
def self.page(class_name)
|
6
|
+
class_name.new()
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.initialize_project
|
10
|
+
Base.create_directory("apps",ROOT_PATH)
|
11
|
+
Base.create_directory("screenshots",ROOT_PATH)
|
12
|
+
Base.create_directory("reports",ROOT_PATH)
|
13
|
+
# Base.create_directory("inputs",ROOT_PATH)
|
14
|
+
# Base.create_directory("outputs",ROOT_PATH)
|
15
|
+
Base.create_directory("testcases",ROOT_PATH)
|
16
|
+
Base.create_directory("testsuites",ROOT_PATH+"features/")
|
17
|
+
|
18
|
+
if ENV['PLATFORM'] == 'android'
|
19
|
+
Base.create_directory("pages",ROOT_PATH+"features/android/")
|
20
|
+
Base.create_file("pages.rb",ROOT_PATH+"features/android/support")
|
21
|
+
end
|
22
|
+
|
23
|
+
if ENV['PLATFORM'] == 'ios'
|
24
|
+
Base.create_directory("pages",ROOT_PATH+"features/ios/")
|
25
|
+
Base.create_file("pages.rb",ROOT_PATH+"features/ios/support")
|
26
|
+
end
|
27
|
+
|
28
|
+
if ENV['PLATFORM'] == 'web'
|
29
|
+
Base.create_directory("pages",ROOT_PATH+"features/web/")
|
30
|
+
Base.create_file("pages.rb",ROOT_PATH+"features/web/support")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
# Wait durations
|
3
|
+
NORMAL_SLEEP_SECS = 2
|
4
|
+
MID_SLEEP_SECS = 5
|
5
|
+
MAX_SLEEP_SECS = 10
|
6
|
+
MAX_WAIT_SECS = 30
|
7
|
+
MAX_TIME_OUT = 180
|
8
|
+
MAX_RETRY_TIMES = 3
|
9
|
+
|
10
|
+
# for web
|
11
|
+
MIN_WAIT = 3
|
12
|
+
MID_WAIT = 5
|
13
|
+
MAX_WAIT = 10
|
14
|
+
WAIT_TIME_OUT = 60
|
15
|
+
NETWORK_WAIT = 5
|
16
|
+
|
17
|
+
|
18
|
+
ENV['ROOT_PATH'] ? ROOT_PATH = ENV['ROOT_PATH'] : ROOT_PATH = ""
|
19
|
+
ENV['CONFIG_PATH']? CONFIG_PATH = ENV['CONFIG_PATH'] : CONFIG_PATH = ""
|
20
|
+
ENV['INPUT_PATH'] ? INPUT_PATH = ENV['INPUT_PATH'] : INPUT_PATH = ""
|
21
|
+
ENV['SCREENSHOT_TEMP'] ? SCREENSHOT_TEMP = ENV['SCREENSHOT_TEMP'] : SCREENSHOT_TEMP = ""
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'tabbyx/core/config'
|
2
|
+
|
3
|
+
# Default settings in case environment variables hasn't been set
|
4
|
+
ENV['SERVER'] = 'uat' if ENV['SERVER'].nil?
|
5
|
+
ENV['PRODUCT'] = 'sample' if ENV['PRODUCT'].nil?
|
6
|
+
ENV['TARGET'] = 'iossimulator' if ENV['TARGET'].nil?
|
7
|
+
ENV['BROWSER'] = 'chrome' if ENV['BROWSER'].nil?
|
8
|
+
|
9
|
+
ENV['CONNECT_TIMEOUT'] = '90' if ENV['CONNECT_TIMEOUT'].nil?
|
10
|
+
|
11
|
+
# Always use downcase
|
12
|
+
ENV['SERVER'] = ENV['SERVER'].downcase
|
13
|
+
ENV['PRODUCT'] = ENV['PRODUCT'].downcase
|
14
|
+
ENV['TARGET'] = ENV['TARGET'].downcase
|
15
|
+
ENV['BROWSER'] = ENV['BROWSER'].downcase
|
16
|
+
|
17
|
+
# Read the configuration files
|
18
|
+
puts "Configuring for product #{ENV['PRODUCT']}, environment #{ENV['SERVER']}"
|
19
|
+
warn("please check support/env.rb to make sure product and environment should be correct!!!")
|
20
|
+
|
21
|
+
config = Config.read_config(ENV['PRODUCT'], ENV['SERVER'], ENV['TARGET'])
|
22
|
+
|
23
|
+
# Setup global constants
|
24
|
+
SERVER = config[:server]
|
25
|
+
USER = config[:user]
|
26
|
+
PASSWORD = config[:password]
|
27
|
+
APP = config[:app]
|
28
|
+
DEVICE = config[:device]
|
29
|
+
HOST = config[:host]
|
30
|
+
|
31
|
+
puts USER, APP, HOST
|
32
|
+
|
33
|
+
warn("请配置测试账户信息!") unless USER
|
34
|
+
warn("请配置app信息!") unless APP
|
35
|
+
warn("请配置当前项目的host!") unless HOST
|
36
|
+
|
37
|
+
# Relevant configurations for API Testing
|
38
|
+
USER_AGENT = "QDReaderAppStore/3.7.3 (iPhone; iOS 9.3; Scale/2.00)"
|
39
|
+
COOKIE = "QDHeader=NzRiMjdkYzIxMmY2NjNlYmRlNWI2NzdkY2RkYzRmNWJ8My43LjN8NzUwfDEzMzR8UWlZZXw5LjMwfDV8aVBob25lIE9TL2lQaG9uZS94ODZfNjR8MTY0fFFpWWV8Mw==; _ga=GA1.2.207823887.1473851687; cmfuToken=N((cfBPzO963RhWhcOx2uGENWANvs2qlZ4zKVpEK8Tlo1RKq3oztfJ4onZu5bFWY3-a4XjIMPYkQOmqWfUrob5VIX1i7EQP6OP6TaPc9tWnPtMxigj0_wm4hU3rxDO24U-ewbhHaG_Ibeca9uUrJXCZe9JkwBizrFwDXUJoCGAMbQPTJwGekSHeQeJWMJF0OlJzPmLXmcIHeoxQ_hrACayIuQTxkLaSAXwBUjkXdzXukBQXW7P5nVMQkkHvTfhs8WajMOuPuv5X1hh9I_M0xNG2CbtyChJBVxvVSKf8_yDOxHaLdEvglmHAEg2; stat_gid=8208333844"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'tabbyx/core/base'
|
3
|
+
|
4
|
+
module CSVhelper
|
5
|
+
|
6
|
+
def self.read_from_csv(filename)
|
7
|
+
file = Base.file_exists?(filename)
|
8
|
+
sheet = CSV.read(file)
|
9
|
+
sheet
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# example: CSVhelper.read_specified_cell("api_testcases.csv",2,2)
|
14
|
+
def self.read_specified_cell(filename,row,column)
|
15
|
+
sheet = read_from_csv(filename)
|
16
|
+
sheet[row][column]
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
# example:
|
21
|
+
# h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
|
22
|
+
# CSVhelper.write_hash_to_csv(h,"test.csv")
|
23
|
+
def self.write_hash_to_csv(hash,filename)
|
24
|
+
Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename)
|
25
|
+
CSV.open(file, "wb") {|csv| hash.to_a.each {|elem| csv << elem} }
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# example:
|
30
|
+
# arr = ["test", "test2", "test3"]
|
31
|
+
# CSVhelper.write_array_to_csv(arr,"test_ar.csv")
|
32
|
+
def self.write_array_to_csv(array,filename)
|
33
|
+
Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename)
|
34
|
+
CSV.open(file, "wb") do |csv|
|
35
|
+
csv << array
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# example:
|
41
|
+
# dic = [["test", "test2", "test3"],["test", "test2", "test3"],["test", "test2", "test3"]]
|
42
|
+
# CSVhelper.write_dictionary_to_csv(dic, "test_dic.csv")
|
43
|
+
def self.write_dictionary_to_csv(dic,filename)
|
44
|
+
Base.file_exists?(filename) ? file = Base.file_exists?(filename) : file = Base.create_file(filename)
|
45
|
+
CSV.open(file, "wb") do |csv|
|
46
|
+
dic.each do |data|
|
47
|
+
csv << data
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# example: CSVhelper.append_to_row_end("api_testcases.csv",2,"pass")
|
54
|
+
def self.append_to_row_end(filename,row,value)
|
55
|
+
file = Base.file_exists?(filename)
|
56
|
+
original_data = CSV.parse(File.read(file)) if Base.file_exists?(filename)
|
57
|
+
original_data[row] << value
|
58
|
+
CSV.open(file, "wb") do |csv|
|
59
|
+
original_data.each do |data|
|
60
|
+
csv << data
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'tabbyx/utils/adb_devices'
|
2
|
+
|
3
|
+
# information of the connected devices
|
4
|
+
puts ADBDevices.adb_devices
|
5
|
+
puts ADBDevices.get_device_id
|
6
|
+
puts ADBDevices.get_android_version
|
7
|
+
puts ADBDevices.get_sdk_version
|
8
|
+
puts ADBDevices.get_screen_resolution
|
9
|
+
puts ADBDevices.get_battery_level
|
10
|
+
puts ADBDevices.get_battery_temperature
|
11
|
+
puts ADBDevices.get_battery_status
|
12
|
+
puts ADBDevices.get_focused_package_activity
|
13
|
+
puts ADBDevices.get_current_activity
|
14
|
+
|
15
|
+
puts ADBDevices.get_installed_apps_list
|
16
|
+
puts ADBDevices.get_system_apps_list
|
17
|
+
|
18
|
+
# debug
|
19
|
+
# puts ADBDevices.get_pid(ADBDevices.get_current_package_name)
|
20
|
+
# puts ADBDevices.kill_process("5723")
|
21
|
+
# ADBDevices.stop_current_app
|
22
|
+
# ADBDevices.reset_current_app
|
23
|
+
# ADBDevices.clear_app_data("com.test.myapplication")
|
24
|
+
# ADBDevices.remove_all_apks
|
25
|
+
|
26
|
+
# debug UIAutomatorTests
|
27
|
+
# ADBDevices.run_android_test_by_class("com.test.myapplication.demoTest","com.test.myapplication")
|
28
|
+
# ADBDevices.run_android_test("com.test.myapplication" )
|
29
|
+
|
30
|
+
# app helpers
|
31
|
+
puts ADBDevices.is_install?("com.qq.reader")
|
32
|
+
# ADBDevices.install_app(app_path)
|
33
|
+
# ADBDevices.uninstall_app("com.test.myapplication")
|
34
|
+
# ADBDevices.push_file(local_path,"/data/local/tmp/")
|
35
|
+
# ADBDevices.pull_file(remote_path,local_path)
|
36
|
+
# ADBDevices.get_app_startup_time("com.test.myapplication/com.app.activity.login.LoginActivity")
|
37
|
+
# ADBDevices.start_activity("com.qq.reader/.activity.GuideActivity")
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'rubyXL'
|
2
|
+
require 'tabbyx/core/base'
|
3
|
+
|
4
|
+
module ExcelHelper
|
5
|
+
|
6
|
+
# read content of specified sheet by row and store in an array
|
7
|
+
# example:
|
8
|
+
# ExcelHelper.read_from_excel("api_testcases.xlsx",0) # by sheet number
|
9
|
+
# ExcelHelper.read_from_excel("api_testcases.xlsx",'Sheet 1') # by sheet name
|
10
|
+
def self.read_from_excel(filename,worksheet)
|
11
|
+
content = []
|
12
|
+
file = Base.file_exists?(filename)
|
13
|
+
excel = RubyXL::Parser.parse(file)
|
14
|
+
sheet = excel.worksheets[worksheet]
|
15
|
+
|
16
|
+
sheet.each do |row|
|
17
|
+
row_array = []
|
18
|
+
row && row.cells.each { |cell|
|
19
|
+
val = cell && cell.value
|
20
|
+
row_array.push val
|
21
|
+
}
|
22
|
+
content.push row_array
|
23
|
+
end
|
24
|
+
content
|
25
|
+
end
|
26
|
+
|
27
|
+
# example:
|
28
|
+
# ExcelHelper.get_value_by_location("api_testcases.xlsx",0,1,1)
|
29
|
+
def self.get_value_by_location(filename,worksheet,row,column)
|
30
|
+
file = Base.file_exists?(filename)
|
31
|
+
excel = RubyXL::Parser.parse(file)
|
32
|
+
sheet = excel.worksheets[worksheet]
|
33
|
+
value = ''
|
34
|
+
value = sheet[row][column].value unless sheet[row][column].nil? unless sheet[row].nil?
|
35
|
+
value
|
36
|
+
end
|
37
|
+
|
38
|
+
# if file exists, it writes value to specified cell. if file does not exist,create an excel and put in outputs folder.
|
39
|
+
# example:
|
40
|
+
# ExcelHelper.write_into_worksheet("test.xlsx",0,5,5,"Hi Excel")
|
41
|
+
def self.write_into_worksheet(filename,worksheet,row,column,value)
|
42
|
+
if Base.file_exists?(filename)
|
43
|
+
file = Base.file(filename)
|
44
|
+
raise("指定的文件不是excel,请检查") unless File.extname(file) == ".xlsx"
|
45
|
+
excel = RubyXL::Parser.parse(file)
|
46
|
+
elsif Base.file_exists?(filename,"../outputs/")
|
47
|
+
file = Base.file(filename,"../outputs/")
|
48
|
+
excel = RubyXL::Parser.parse(file)
|
49
|
+
else
|
50
|
+
file = Base.file(filename,"../outputs/")
|
51
|
+
puts "创建excel文件"+filename
|
52
|
+
excel = RubyXL::Workbook.new
|
53
|
+
end
|
54
|
+
|
55
|
+
sheet = excel.worksheets[worksheet]
|
56
|
+
if sheet[row].nil?
|
57
|
+
sheet.add_cell(row,column,value)
|
58
|
+
elsif sheet[row][column].nil?
|
59
|
+
sheet.add_cell(row,column,value)
|
60
|
+
else
|
61
|
+
sheet[row][column].change_contents(value)
|
62
|
+
end
|
63
|
+
excel.save(file)
|
64
|
+
end
|
65
|
+
|
66
|
+
# write an array, of which contains array items into excel file.
|
67
|
+
# example:
|
68
|
+
# dic = [["test", "test2", "test3"],["test", "test2", "test3"],["test", "test2", "test3"]]
|
69
|
+
# ExcelHelper.write_dictionary_to_excel(dic,"test1.xlsx",0)
|
70
|
+
def self.write_dictionary_to_excel(dic,filename,worksheet)
|
71
|
+
dic.each_with_index do |array, row|
|
72
|
+
array.each_with_index do |item,column|
|
73
|
+
write_into_worksheet(filename,worksheet,row,column,item)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|