watir-or 0.0.2

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.
data/README ADDED
@@ -0,0 +1,11 @@
1
+ == Object Repository for WATiR
2
+
3
+ This is a library for creating object repository for WATiR testing framework.
4
+
5
+ == Installation
6
+
7
+ gem install watir-or
8
+
9
+ == Usage
10
+
11
+ For more information see the http://github.com/lfidnl/watir-or/wiki
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+ require 'spec/rake/spectask'
13
+
14
+ Rake::RDocTask.new do |rdoc|
15
+ files =['README', 'LICENSE', 'lib/**/*.rb']
16
+ rdoc.rdoc_files.add(files)
17
+ rdoc.main = "README" # page to start on
18
+ rdoc.title = "watir-or Docs"
19
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
20
+ rdoc.options << '--line-numbers'
21
+ end
22
+
23
+ Rake::TestTask.new do |t|
24
+ t.test_files = FileList['test/**/*.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new do |t|
28
+ t.spec_files = FileList['spec/**/*.rb']
29
+ t.libs << Dir["lib"]
30
+ end
@@ -0,0 +1,37 @@
1
+ module ObjectRepository
2
+ module ElementConverter
3
+ module Watir
4
+ def self.convert(browser, repository, rep_element)
5
+ eval("browser.#{build_element(rep_element, repository)}")
6
+ end
7
+
8
+ def self.build_element(rep_element, repository)
9
+ parent = ""
10
+ unless rep_element.locator[:parent].nil?
11
+ parent = build_element(repository.find(rep_element.locator[:parent]), repository) + "."
12
+ end
13
+ locator = rep_element.locator.dup
14
+ locator.delete(:parent)
15
+ parent << "%s(%s)" % [rep_element.type, build_locator(locator)]
16
+ end
17
+
18
+ #Method need for separate frame(:name => "foo") and frame(:name, "foo").
19
+ #Because frame with multiply conditions doesn't work
20
+ def self.build_locator(locator)
21
+ if locator.length > 1
22
+ locator.inspect.gsub(/(^\{|\}$)/, "")
23
+ else
24
+ ":%s,%s" % [locator.keys.first, locator[locator.keys.first].inspect]
25
+ end
26
+ end
27
+ end
28
+
29
+ def to_form_object(browser, repository)
30
+ if self.type[0, 1] =~ /[A-Z]/
31
+ eval(self.type).new(repository, browser, self.locator)
32
+ else
33
+ Watir.convert(browser, repository, self)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module ObjectRepository
2
+ class RepositoryElement
3
+ include ElementConverter
4
+ attr_reader :id, :type, :locator, :description
5
+
6
+ def initialize(id, type, hows, whats, description)
7
+ @id = id
8
+ @description = description
9
+ @type = type
10
+ @locator = Hash[*hows.zip(whats).flatten]
11
+ end
12
+
13
+ def inspect
14
+ "Element #{@id.inspect}:\n\tlocator: #{@locator.inspect}\n\tdescription: #{@description.inspect}\n"
15
+ end
16
+
17
+ def ==(other)
18
+ @id == other.id && @type == other.type && @description == other.description && @locator == other.locator
19
+ end
20
+ end
21
+
22
+ class CustomElement
23
+ def initialize(repository, browser, attributes)
24
+ @repository, @browser, @attributes = repository, browser, attributes
25
+ end
26
+
27
+ def field(name)
28
+ @repository.get(@attributes[name])
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,49 @@
1
+ module ObjectRepository
2
+ module Reader
3
+ module XLS
4
+ require 'spreadsheet'
5
+
6
+ def self.read(file, options = nil, &block)
7
+ if options.nil?
8
+ read_worksheet(Spreadsheet.open(file).worksheet(0), &block)
9
+ elsif options[:worksheet].is_a?(String)
10
+ read_worksheet(Spreadsheet.open(file).worksheet(options[:tab]), &block)
11
+ elsif options[:worksheet] == :all
12
+ Spreadsheet.open(file).worksheets.each { |worksheet|
13
+ read_worksheet(worksheet, &block)
14
+ }
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def self.read_worksheet(worksheet, &block)
21
+ rows_count = worksheet.row_count
22
+ hows = Array.new
23
+ whats = Array.new
24
+ rep_id = nil
25
+ description = nil
26
+ type = nil
27
+ worksheet.each(1) { |row| # each(1) exclude header
28
+ unless row[0].nil?
29
+ hows.clear
30
+ whats.clear
31
+ rep_id = row[0]
32
+ type = row[1]
33
+ description = row[4]
34
+ end
35
+ hows << row[2]
36
+ whats << if row[2] == "index"
37
+ row[3].to_i
38
+ else
39
+ row[3]
40
+ end
41
+ if (!worksheet.row(row.idx + 1)[0].nil? || row.idx + 1 >= rows_count )
42
+ yield rep_id, type, hows.map { |el| el.to_sym }, whats, description
43
+ end
44
+ }
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,23 @@
1
+ module ObjectRepository
2
+ module Reader
3
+ module XML
4
+ require 'nokogiri'
5
+
6
+ def self.read(file, options = nil, &block)
7
+ doc = Nokogiri::XML(File.read(file), nil, 'UTF-8', Nokogiri::XML::ParseOptions::NOBLANKS)
8
+ doc.root.xpath("//object").each { |node|
9
+ hows = node.search("./how").children
10
+ whats = hows.map { |h| h.name == "index" ? h.content.to_i : h.content }
11
+ #Add parent
12
+ hows = hows.map { |h| h.name.to_sym }
13
+ if node.parent != doc.root
14
+ hows << :parent
15
+ whats << node.parent['id']
16
+ end
17
+
18
+ yield node['id'], node['type'], hows, whats, node.search('./description').first.content
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ module ObjectRepository
2
+ class Repository
3
+ attr_reader :file, :browser, :elements
4
+
5
+ def initialize(file, browser, options = nil)
6
+ @file = file
7
+ @elements = Array.new
8
+ @browser = browser
9
+ @type = File.extname(@file).delete('.')
10
+ fill_elements(options)
11
+ end
12
+
13
+ def find(what, how = nil)
14
+ if how.nil?
15
+ element = @elements.find { |el| el.id == what || el.description == what }
16
+ else
17
+ element = @elements.find { |el| el.instance_variable_get("@#{how}") == what }
18
+ end
19
+ raise "Couldn't find repository element with #{what.inspect} by #{how.inspect}" if element.nil?
20
+ return element
21
+ end
22
+
23
+ def method_missing(meth_name, what, &block)
24
+ case meth_name.to_s
25
+ when /^get_by_(.*)/
26
+ element = find(what, $1.to_sym)
27
+ when "get"
28
+ element = find(what)
29
+ else
30
+ super
31
+ end
32
+ return element.to_form_object(@browser, self)
33
+ end
34
+
35
+ def inspect
36
+ @elements.inspect
37
+ end
38
+
39
+ private
40
+
41
+ def fill_elements(options)
42
+ eval("ObjectRepository::Reader::" + @type.upcase).read(@file, options) { |rep_id, type, hows, whats, description|
43
+ unless @elements.find { |el| el.id == rep_id }
44
+ @elements << RepositoryElement.new(rep_id, type, hows, whats, description)
45
+ else
46
+ raise "Duplicating of id: #{rep_id.inspect}"
47
+ end
48
+ }
49
+ rescue NameError => e
50
+ raise "Unknown type #{@type.inspect} of repository"
51
+ end
52
+ end
53
+ end
data/lib/watir-or.rb ADDED
@@ -0,0 +1,9 @@
1
+ Dir[File.join(File.dirname(__FILE__), "watir-or/converters") + "/*.rb"].each { |converter|
2
+ require converter
3
+ }
4
+
5
+ Dir[File.join(File.dirname(__FILE__), "watir-or/readers") + "/*.rb"].each { |reader|
6
+ require reader
7
+ }
8
+ require 'watir-or/element'
9
+ require 'watir-or/repository'
@@ -0,0 +1,5 @@
1
+ class BrowserStub
2
+ def method_missing(meth_name, *args, &block)
3
+ return "%s(%s)" % [meth_name, args.first.inspect.gsub(/(^\{|\}$)/, "")]
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'lib/watir-or'
2
+ require 'spec/browser_stub'
3
+ TEST_FILE = "spec/test.xls"
4
+
5
+ class MyCustomElement < ObjectRepository::CustomElement
6
+ end
7
+
8
+ describe "CustomElement" do
9
+ before(:all) do
10
+ @browser_stub = BrowserStub.new
11
+ @repository = ObjectRepository::Repository.new(TEST_FILE, @browser_stub)
12
+ end
13
+
14
+ it "should return MyCustomElement object" do
15
+ @repository.get("custom element").should be_a(MyCustomElement)
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ require 'lib/watir-or.rb'
2
+ require 'spec/browser_stub'
3
+
4
+ describe "Search of elements" do
5
+ before(:all) do
6
+ @browser_stub = BrowserStub.new
7
+ @repository = ObjectRepository::Repository.new("spec/test.xls", @browser_stub)
8
+ end
9
+
10
+ context "find element by id or description if argument is" do
11
+ it "id" do
12
+ @repository.get("many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
13
+ end
14
+
15
+ it "description" do
16
+ @repository.get("Element with many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
17
+ end
18
+ end
19
+
20
+ context "find by" do
21
+ it "id" do
22
+ @repository.get_by_id("many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
23
+ end
24
+
25
+ it "description" do
26
+ @repository.get_by_description("Element with many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
27
+ end
28
+ end
29
+
30
+ it "should raise exception when element wasn't found" do
31
+ lambda{@repository.get("fake element")}.should raise_error
32
+ end
33
+ end
@@ -0,0 +1,54 @@
1
+ require 'lib/watir-or.rb'
2
+
3
+ describe "Read xls-repository" do
4
+ context "Read first worksheet" do
5
+ before(:all) do
6
+ @repository = ObjectRepository::Repository.new("spec/test.xls", nil)
7
+ end
8
+
9
+ it "element with one attribute was readed" do
10
+ one = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
11
+ ["test"], "Element with one attribute")
12
+ @repository.find("one attribute").should == one
13
+ end
14
+
15
+ it "element with many attribute was readed" do
16
+ many = ObjectRepository::RepositoryElement.new("many attributes", "select_list", [:text, :index],
17
+ ["test text", 1], "Element with many attributes")
18
+ @repository.find("many attributes").should == many
19
+ end
20
+
21
+ it "last element with many attribute was readed" do
22
+ last_many = ObjectRepository::RepositoryElement.new("last element with many attributes", "image",
23
+ [:name, :src], ["xxx", "path"], "Last element with many attributes")
24
+ @repository.find("last element with many attributes").should == last_many
25
+ end
26
+ end
27
+
28
+ context "Read special worksheet" do
29
+ before(:all) do
30
+ @repository = ObjectRepository::Repository.new("spec/test.xls", nil, :tab => "special worksheet")
31
+ end
32
+
33
+ it "should read element on special workheet" do
34
+ first = ObjectRepository::RepositoryElement.new("first element", "text_field",
35
+ [:name], ["awesome"], "First element on special worksheet")
36
+ @repository.find("first element").should == first
37
+ end
38
+ end
39
+
40
+ context "Read all worksheets" do
41
+ before(:all) do
42
+ @repository = ObjectRepository::Repository.new("spec/test.xls", nil, :tab => :all)
43
+ end
44
+
45
+ it "should read all elements on all workheets" do
46
+ on_first_worksheet = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
47
+ ["test"], "Element with one attribute")
48
+ on_special_worksheet = ObjectRepository::RepositoryElement.new("first element", "text_field",
49
+ [:name], ["awesome"], "First element on special worksheet")
50
+ @repository.find("one attribute").should == on_first_worksheet
51
+ @repository.find("first element").should == on_special_worksheet
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,25 @@
1
+ require 'lib/watir-or.rb'
2
+
3
+ describe "Read xml-repository" do
4
+ before(:all) do
5
+ @repository = ObjectRepository::Repository.new("spec/test.xml", nil)
6
+ end
7
+
8
+ it "element with one attribute was readed" do
9
+ one = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
10
+ ["test"], "Element with one attribute")
11
+ @repository.find("one attribute").should == one
12
+ end
13
+
14
+ it "element with many attribute was readed" do
15
+ many = ObjectRepository::RepositoryElement.new("many attributes", "select_list", [:text, :index],
16
+ ["test text", 1], "Element with many attributes")
17
+ @repository.find("many attributes").should == many
18
+ end
19
+
20
+ it "child element was readed" do
21
+ child = ObjectRepository::RepositoryElement.new("with parent", "div", [:index, :parent],
22
+ [3, "parent element"], "Element with parent")
23
+ @repository.find("with parent").should == child
24
+ end
25
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --backtrace
data/spec/test.xls ADDED
Binary file
data/spec/test.xml ADDED
@@ -0,0 +1,35 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <repository>
3
+ <object type="text_field" id="one attribute">
4
+ <how>
5
+ <name>test</name>
6
+ </how>
7
+ <description>Element with one attribute</description>
8
+ </object>
9
+ <object type="select_list" id="many attributes">
10
+ <how>
11
+ <text>test text</text>
12
+ <index>1</index>
13
+ </how>
14
+ <description>Element with many attributes</description>
15
+ </object>
16
+ <object type="div" id="parent element">
17
+ <how>
18
+ <index>1</index>
19
+ </how>
20
+ <description>Parent element</description>
21
+ <object type="div" id="with parent">
22
+ <how>
23
+ <index>3</index>
24
+ </how>
25
+ <description>Element with parent</description>
26
+ </object>
27
+ </object>
28
+ <object type="MyCustomElement" id="custom element">
29
+ <how>
30
+ <attr1>value of attr1</attr1>
31
+ <attr2>value of attr2</attr2>
32
+ </how>
33
+ <description>Custom Element</description>
34
+ </object>
35
+ </repository>
@@ -0,0 +1,27 @@
1
+ require 'lib/watir-or.rb'
2
+ require 'spec/browser_stub'
3
+
4
+ describe "WatirConverter" do
5
+ before(:all) do
6
+ @browser_stub = BrowserStub.new
7
+ @repository = ObjectRepository::Repository.new("spec/test.xls", @browser_stub)
8
+ end
9
+
10
+ it "build watir object properly" do
11
+ element = @repository.elements.find {|el| el.id == "many attributes"}
12
+ built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
13
+ built.should == 'select_list(:index=>1, :text=>"test text")'
14
+ end
15
+
16
+ it "build watir object which has one parent" do
17
+ element = @repository.elements.find {|el| el.id == "with parent"}
18
+ built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
19
+ built.should == 'div(:index,1).div(:index,3)'
20
+ end
21
+
22
+ it "2 level hierarchy" do
23
+ element = @repository.elements.find {|el| el.id == "2 level hierarchy"}
24
+ built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
25
+ built.should == 'div(:index,1).div(:index,3).div(:index,5)'
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watir-or
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Ivan Kabluchkov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-30 00:00:00 +04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: spreadsheet
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 6
30
+ - 3
31
+ - 1
32
+ version: 0.6.3.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: nokogiri
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 4
45
+ - 0
46
+ version: 1.4.0
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Library for working with object repository for Watir
50
+ email:
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - README
57
+ files:
58
+ - README
59
+ - Rakefile
60
+ - lib/watir-or.rb
61
+ - lib/watir-or/repository.rb
62
+ - lib/watir-or/readers/xml.rb
63
+ - lib/watir-or/readers/xls.rb
64
+ - lib/watir-or/element.rb
65
+ - lib/watir-or/converters/element_converter.rb
66
+ - spec/watir_builder_spec.rb
67
+ - spec/browser_stub.rb
68
+ - spec/read_xls_spec.rb
69
+ - spec/test.xml
70
+ - spec/test.xls
71
+ - spec/custom_elements_spec.rb
72
+ - spec/spec.opts
73
+ - spec/find_elements_spec.rb
74
+ - spec/read_xml_spec.rb
75
+ has_rdoc: true
76
+ homepage:
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Object Repository for Watir
105
+ test_files:
106
+ - spec/watir_builder_spec.rb
107
+ - spec/read_xls_spec.rb
108
+ - spec/custom_elements_spec.rb
109
+ - spec/find_elements_spec.rb
110
+ - spec/read_xml_spec.rb