watir-or 0.0.3 → 0.0.4

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.
@@ -1,36 +1,26 @@
1
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
2
+ module WatirObjectBuilder
3
+ def self.build_watir_obj(repository, rep_element)
4
+ eval("repository.browser.#{build_element(rep_element, repository)}")
5
+ end
7
6
 
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
7
+ def self.build_element(rep_element, repository)
8
+ parent = ""
9
+ unless rep_element.locator[:parent].nil?
10
+ parent = build_element(repository.find { |el| el.rep_id == rep_element.locator[:parent] }, repository) + "."
26
11
  end
12
+ locator = rep_element.locator.dup
13
+ locator.delete(:parent)
14
+ parent << "%s(%s)" % [rep_element.type, build_locator(locator)]
27
15
  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)
16
+
17
+ #Method need for separate frame(:name => "foo") and frame(:name, "foo").
18
+ #Because frame with multiply conditions doesn't work
19
+ def self.build_locator(locator)
20
+ if locator.length > 1
21
+ locator.inspect.gsub(/(^\{|\}$)/, "")
32
22
  else
33
- Watir.convert(browser, repository, self)
23
+ ":%s,%s" % [locator.keys.first, locator[locator.keys.first].inspect]
34
24
  end
35
25
  end
36
26
  end
@@ -1,40 +1,71 @@
1
1
  module ObjectRepository
2
2
  class RepositoryElement
3
- include ElementConverter
4
- attr_reader :id, :type, :locator, :description
3
+ attr_reader :rep_id, :type, :description
5
4
 
6
- def initialize(id, type, hows, whats, description)
7
- @id = id
8
- @description = description
9
- @type = type
10
- normalize_whats!(whats)
11
- @locator = Hash[*hows.zip(whats).flatten]
5
+ def initialize(rep_id, type, hows, whats, description, repository)
6
+ @rep_id = rep_id.dup
7
+ @description = description #.dup
8
+ @type = type.dup
9
+ @hows, @whats = hows.dup, whats.dup
10
+ @repository = repository
12
11
  end
13
12
 
14
13
  def inspect
15
- "Element #{@id.inspect}:\n\tlocator: #{@locator.inspect}\n\tdescription: #{@description.inspect}\n"
14
+ "Element #{@rep_id.inspect}:\n\thows: #{@hows.inspect}\n\twhats: #{@whats.inspect}\n\tdescription: #{@description.inspect}\n"
16
15
  end
17
16
 
18
17
  def ==(other)
19
- @id == other.id && @type == other.type && @description == other.description && @locator == other.locator
18
+ @rep_id == other.rep_id && @type == other.type && @description == other.description
19
+ end
20
+ end
21
+
22
+ class WatirElement < RepositoryElement
23
+ include WatirObjectBuilder
24
+ attr_reader :watir_obj, :locator
25
+
26
+ def initialize(id, type, hows, whats, description, repository)
27
+ super(id, type, hows, whats, description, repository)
28
+ transform_whats!
29
+ @locator = Hash[*@hows.zip(@whats).flatten]
30
+ end
31
+
32
+ def method_missing(meth_name, *args, &block)
33
+ @watir_obj ||= WatirObjectBuilder.build_watir_obj(@repository, self)
34
+ if @watir_obj.respond_to?(meth_name) || meth_name.to_s.include?("_no_wait")
35
+ @watir_obj.send(meth_name, *args, &block)
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def present?
42
+ method_missing(:present?)
20
43
  end
21
44
 
22
45
  private
23
46
 
24
- def normalize_whats!(whats)
25
- whats.map! do |what|
26
- what =~ /^\/(.*)\/$/ ? what = Regexp.new($1) : what
47
+ def transform_whats!
48
+ @hows.each_with_index do |how, index|
49
+ if how == :index
50
+ @whats[index] = @whats[index].to_i
51
+ elsif @whats[index] =~ /^\/(.*)\/$/ && how != :xpath
52
+ @whats[index] = Regexp.new($1)
53
+ end
27
54
  end
28
55
  end
29
56
  end
30
57
 
31
- class CustomElement
32
- def initialize(repository, browser, attributes)
33
- @repository, @browser, @attributes = repository, browser, attributes
58
+ class CustomElement < RepositoryElement
59
+ def initialize(*args)
60
+ super(*args)
61
+ @attributes = {}
62
+ @hows.each_with_index do |how, index|
63
+ @attributes[how] = @repository.get_by_rep_id(@whats[index])
64
+ end
34
65
  end
35
66
 
36
67
  def field(name)
37
- @repository.get(@attributes[name])
68
+ @attributes[name]
38
69
  end
39
70
  end
40
71
  end
@@ -33,11 +33,7 @@ module ObjectRepository
33
33
  description = row[4]
34
34
  end
35
35
  hows << row[2]
36
- whats << if row[2] == "index"
37
- row[3].to_i
38
- else
39
- row[3]
40
- end
36
+ whats << row[3]
41
37
  if (!worksheet.row(row.idx + 1)[0].nil? || row.idx + 1 >= rows_count )
42
38
  yield rep_id, type, hows.map { |el| el.to_sym }, whats, description
43
39
  end
@@ -7,7 +7,7 @@ module ObjectRepository
7
7
  doc = Nokogiri::XML(File.read(file), nil, 'UTF-8', Nokogiri::XML::ParseOptions::NOBLANKS)
8
8
  doc.root.xpath("//object").each { |node|
9
9
  hows = node.search("./how").children
10
- whats = hows.map { |h| h.name == "index" ? h.content.to_i : h.content }
10
+ whats = hows.map { |h| h.content }
11
11
  #Add parent
12
12
  hows = hows.map { |h| h.name.to_sym }
13
13
  if node.parent != doc.root
@@ -1,5 +1,6 @@
1
1
  module ObjectRepository
2
2
  class Repository
3
+ include Enumerable
3
4
  attr_reader :file, :browser, :elements
4
5
 
5
6
  def initialize(file, browser, options = nil)
@@ -10,26 +11,21 @@ module ObjectRepository
10
11
  fill_elements(options)
11
12
  end
12
13
 
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
14
+ def each(&block)
15
+ @elements.each { |el| yield el }
21
16
  end
22
17
 
23
18
  def method_missing(meth_name, what, &block)
24
- case meth_name.to_s
19
+ element = case meth_name.to_s
25
20
  when /^get_by_(.*)/
26
- element = find(what, $1.to_sym)
21
+ find { |el| el.instance_variable_get("@#{$1.to_sym}") == what }
27
22
  when "get"
28
- element = find(what)
23
+ find { |el| el.rep_id == what || el.description == what }
29
24
  else
30
25
  super
31
26
  end
32
- return element.to_form_object(@browser, self)
27
+ raise "Couldn't find repository element with #{what.inspect}" if element.nil?
28
+ return element
33
29
  end
34
30
 
35
31
  def inspect
@@ -39,14 +35,18 @@ module ObjectRepository
39
35
  private
40
36
 
41
37
  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)
38
+ eval("ObjectRepository::Reader::" + @type.upcase).read(@file, options) do |rep_id, type, hows, whats, description|
39
+ unless @elements.find { |el| el.rep_id == rep_id }
40
+ @elements << if type[0..0] =~ /[A-Z]/
41
+ eval("#{type}").new(rep_id, type, hows, whats, description, self)
42
+ else
43
+ WatirElement.new(rep_id, type, hows, whats, description, self)
44
+ end
45
45
  else
46
46
  raise "Duplicating of id: #{rep_id.inspect}"
47
47
  end
48
- }
49
- rescue NameError => e
48
+ end
49
+ rescue NameError
50
50
  raise "Unknown type #{@type.inspect} of repository"
51
51
  end
52
52
  end
data/spec/browser_stub.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  class BrowserStub
2
2
  def method_missing(meth_name, *args, &block)
3
- return "%s(%s)" % [meth_name, args.first.inspect.gsub(/(^\{|\}$)/, "")]
3
+ Control.new
4
+ # return "%s(%s)" % [meth_name, args.first.inspect.gsub(/(^\{|\}$)/, "")]
5
+ end
6
+ end
7
+
8
+ class Control
9
+ def method_missing(meth_name, *args, &block)
10
+ Control.new
4
11
  end
5
12
  end
@@ -1,10 +1,8 @@
1
1
  require 'lib/watir-or'
2
2
  require 'spec/browser_stub'
3
+ require 'spec/spec_helper'
3
4
  TEST_FILE = "spec/test.xls"
4
5
 
5
- class MyCustomElement < ObjectRepository::CustomElement
6
- end
7
-
8
6
  describe "CustomElement" do
9
7
  before(:all) do
10
8
  @browser_stub = BrowserStub.new
@@ -14,4 +12,10 @@ describe "CustomElement" do
14
12
  it "should return MyCustomElement object" do
15
13
  @repository.get("custom element").should be_a(MyCustomElement)
16
14
  end
15
+
16
+ it "should return field of MyCustomElement object" do
17
+ attr1 = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
18
+ ["test"], "Element with one attribute", @repository)
19
+ @repository.get("custom element").field(:attr1).should == attr1
20
+ end
17
21
  end
@@ -1,29 +1,32 @@
1
1
  require 'lib/watir-or.rb'
2
2
  require 'spec/browser_stub'
3
+ require 'spec/spec_helper'
3
4
 
4
5
  describe "Search of elements" do
5
6
  before(:all) do
6
7
  @browser_stub = BrowserStub.new
7
8
  @repository = ObjectRepository::Repository.new("spec/test.xls", @browser_stub)
9
+ @element = ObjectRepository::RepositoryElement.new("many attributes", "select_list", [:text, :index],
10
+ ["test text", 1], "Element with many attributes", @repository)
8
11
  end
9
12
 
10
13
  context "find element by id or description if argument is" do
11
14
  it "id" do
12
- @repository.get("many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
15
+ @repository.get("many attributes").should == @element
13
16
  end
14
17
 
15
18
  it "description" do
16
- @repository.get("Element with many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
19
+ @repository.get("Element with many attributes").should == @element
17
20
  end
18
21
  end
19
22
 
20
23
  context "find by" do
21
24
  it "id" do
22
- @repository.get_by_id("many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
25
+ @repository.get_by_rep_id("many attributes").should == @element
23
26
  end
24
27
 
25
28
  it "description" do
26
- @repository.get_by_description("Element with many attributes").should == "select_list(:index=>1, :text=>\"test text\")"
29
+ @repository.get_by_description("Element with many attributes").should == @element
27
30
  end
28
31
  end
29
32
 
@@ -1,54 +1,59 @@
1
1
  require 'lib/watir-or.rb'
2
+ require 'spec/spec_helper'
2
3
 
3
4
  describe "Read xls-repository" do
4
5
  context "Read first worksheet" do
5
6
  before(:all) do
6
- @repository = ObjectRepository::Repository.new("spec/test.xls", nil)
7
+ @repository = ObjectRepository::Repository.new("spec/test.xls", BrowserStub.new)
7
8
  end
8
9
 
9
10
  it "element with one attribute was readed" do
10
11
  one = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
11
- ["test"], "Element with one attribute")
12
- @repository.find("one attribute").should == one
12
+ ["test"], "Element with one attribute", @repository)
13
+ @repository.get("one attribute").should == one
13
14
  end
14
15
 
15
16
  it "element with many attribute was readed" do
16
17
  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
18
+ ["test text", 1], "Element with many attributes", @repository)
19
+ @repository.get("many attributes").should == many
19
20
  end
20
21
 
21
22
  it "last element with many attribute was readed" do
22
23
  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
24
+ [:name, :src], ["xxx", "path"], "Last element with many attributes", @repository)
25
+ @repository.get("last element with many attributes").should == last_many
25
26
  end
26
27
  end
27
28
 
28
29
  context "Read special worksheet" do
29
30
  before(:all) do
30
- @repository = ObjectRepository::Repository.new("spec/test.xls", nil, :worksheet => "special worksheet")
31
+ @repository = ObjectRepository::Repository.new("spec/test.xls", BrowserStub.new, :worksheet => "special worksheet")
31
32
  end
32
33
 
33
34
  it "should read element on special workheet" do
34
35
  first = ObjectRepository::RepositoryElement.new("first element", "text_field",
35
- [:name], ["awesome"], "First element on special worksheet")
36
- @repository.find("first element").should == first
36
+ [:name], ["awesome"], "First element on special worksheet", @repository)
37
+ @repository.get("first element").should == first
37
38
  end
38
39
  end
39
40
 
40
41
  context "Read all worksheets" do
41
42
  before(:all) do
42
- @repository = ObjectRepository::Repository.new("spec/test.xls", nil, :worksheet => :all)
43
+ @repository = ObjectRepository::Repository.new("spec/test.xls", BrowserStub.new, :worksheet => :all)
43
44
  end
44
45
 
45
46
  it "should read all elements on all workheets" do
46
47
  on_first_worksheet = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
47
- ["test"], "Element with one attribute")
48
+ ["test"], "Element with one attribute", @repository)
48
49
  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
50
+ [:name], ["awesome"], "First element on special worksheet", @repository)
51
+ @repository.get("one attribute").should == on_first_worksheet
52
+ @repository.get("first element").should == on_special_worksheet
52
53
  end
53
54
  end
55
+
56
+ it "duplicating of id" do
57
+ lambda { ObjectRepository::Repository.new("spec/test_duplicating_id.xls", BrowserStub.new) }.should raise_error
58
+ end
54
59
  end
@@ -1,25 +1,26 @@
1
1
  require 'lib/watir-or.rb'
2
+ require 'spec_helper'
2
3
 
3
4
  describe "Read xml-repository" do
4
5
  before(:all) do
5
- @repository = ObjectRepository::Repository.new("spec/test.xml", nil)
6
+ @repository = ObjectRepository::Repository.new("spec/test.xml", BrowserStub.new)
6
7
  end
7
8
 
8
9
  it "element with one attribute was readed" do
9
10
  one = ObjectRepository::RepositoryElement.new("one attribute", "text_field", [:name],
10
- ["test"], "Element with one attribute")
11
- @repository.find("one attribute").should == one
11
+ ["test"], "Element with one attribute", @repository)
12
+ @repository.get("one attribute").should == one
12
13
  end
13
14
 
14
15
  it "element with many attribute was readed" do
15
16
  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
17
+ ["test text", 1], "Element with many attributes", @repository)
18
+ @repository.get("many attributes").should == many
18
19
  end
19
20
 
20
21
  it "child element was readed" do
21
22
  child = ObjectRepository::RepositoryElement.new("with parent", "div", [:index, :parent],
22
- [3, "parent element"], "Element with parent")
23
- @repository.find("with parent").should == child
23
+ [3, "parent element"], "Element with parent", @repository)
24
+ @repository.get("with parent").should == child
24
25
  end
25
26
  end
@@ -0,0 +1,4 @@
1
+ require 'browser_stub'
2
+
3
+ class MyCustomElement < ObjectRepository::CustomElement
4
+ end
data/spec/test.xls CHANGED
Binary file
data/spec/test.xml CHANGED
@@ -27,8 +27,8 @@
27
27
  </object>
28
28
  <object type="MyCustomElement" id="custom element">
29
29
  <how>
30
- <attr1>value of attr1</attr1>
31
- <attr2>value of attr2</attr2>
30
+ <attr1>one attribute</attr1>
31
+ <attr2>many attributes</attr2>
32
32
  </how>
33
33
  <description>Custom Element</description>
34
34
  </object>
Binary file
@@ -1,34 +1,35 @@
1
1
  require 'lib/watir-or.rb'
2
2
  require 'spec/browser_stub'
3
+ require 'spec/spec_helper'
3
4
 
4
- describe "WatirConverter" do
5
+ describe "WatirObjectBuilder" do
5
6
  before(:all) do
6
7
  @browser_stub = BrowserStub.new
7
8
  @repository = ObjectRepository::Repository.new("spec/test.xls", @browser_stub)
8
9
  end
9
10
 
10
11
  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)
12
+ element = @repository.get('many attributes')
13
+ built = ObjectRepository::WatirObjectBuilder.build_element(element, @repository)
13
14
  built.should == 'select_list(:index=>1, :text=>"test text")'
14
15
  end
15
16
 
16
17
  it "build element with regexp" do
17
- element = @repository.elements.find {|el| el.id == "with regexp what"}
18
- built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
18
+ element = @repository.get('with regexp what')
19
+ built = ObjectRepository::WatirObjectBuilder.build_element(element, @repository)
19
20
  built.should == 'text_field(:text,/regexp/)'
20
21
  end
21
22
 
22
23
  context "hierarchy" do
23
24
  it "watir object which has one parent" do
24
- element = @repository.elements.find {|el| el.id == "with parent"}
25
- built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
25
+ element = @repository.get('with parent')
26
+ built = ObjectRepository::WatirObjectBuilder.build_element(element, @repository)
26
27
  built.should == 'div(:index,1).div(:index,3)'
27
28
  end
28
29
 
29
30
  it "2 level" do
30
- element = @repository.elements.find {|el| el.id == "2 level hierarchy"}
31
- built = ObjectRepository::ElementConverter::Watir.build_element(element, @repository)
31
+ element = @repository.get('2 level hierarchy')
32
+ built = ObjectRepository::WatirObjectBuilder.build_element(element, @repository)
32
33
  built.should == 'div(:index,1).div(:index,3).div(:index,5)'
33
34
  end
34
35
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ivan Kabluchkov
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-17 00:00:00 +04:00
17
+ date: 2011-06-16 00:00:00 +04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: spreadsheet
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -36,6 +37,7 @@ dependencies:
36
37
  name: nokogiri
37
38
  prerelease: false
38
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
39
41
  requirements:
40
42
  - - ">="
41
43
  - !ruby/object:Gem::Version
@@ -57,21 +59,23 @@ extra_rdoc_files:
57
59
  files:
58
60
  - README
59
61
  - 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
62
  - lib/watir-or/converters/element_converter.rb
66
- - spec/watir_builder_spec.rb
63
+ - lib/watir-or/element.rb
64
+ - lib/watir-or/readers/xls.rb
65
+ - lib/watir-or/readers/xml.rb
66
+ - lib/watir-or/repository.rb
67
+ - lib/watir-or.rb
67
68
  - spec/browser_stub.rb
68
- - spec/read_xls_spec.rb
69
- - spec/test.xml
70
- - spec/test.xls
71
69
  - spec/custom_elements_spec.rb
72
- - spec/spec.opts
73
70
  - spec/find_elements_spec.rb
71
+ - spec/read_xls_spec.rb
74
72
  - spec/read_xml_spec.rb
73
+ - spec/spec.opts
74
+ - spec/spec_helper.rb
75
+ - spec/test.xls
76
+ - spec/test.xml
77
+ - spec/test_duplicating_id.xls
78
+ - spec/watir_builder_spec.rb
75
79
  has_rdoc: true
76
80
  homepage:
77
81
  licenses: []
@@ -82,6 +86,7 @@ rdoc_options: []
82
86
  require_paths:
83
87
  - lib
84
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
85
90
  requirements:
86
91
  - - ">="
87
92
  - !ruby/object:Gem::Version
@@ -89,6 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
94
  - 0
90
95
  version: "0"
91
96
  required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
92
98
  requirements:
93
99
  - - ">="
94
100
  - !ruby/object:Gem::Version
@@ -98,13 +104,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
104
  requirements: []
99
105
 
100
106
  rubyforge_project:
101
- rubygems_version: 1.3.6
107
+ rubygems_version: 1.3.7
102
108
  signing_key:
103
109
  specification_version: 3
104
110
  summary: Object Repository for Watir
105
111
  test_files:
106
- - spec/watir_builder_spec.rb
107
- - spec/read_xls_spec.rb
108
112
  - spec/custom_elements_spec.rb
109
113
  - spec/find_elements_spec.rb
114
+ - spec/read_xls_spec.rb
110
115
  - spec/read_xml_spec.rb
116
+ - spec/watir_builder_spec.rb