-
Notifications
You must be signed in to change notification settings - Fork 6
3 Defining Elements
Defined elements always evaluate to an instance of a Watir Element subclass, which means all valid HTML tags are supported just as in Watir.
class Foo < WatirDrops::PageObject
element(:foo) { browser.text_element(id: 'foo') }
end
$ Foo.new.foo.class
=> Watir::TextField
As a result, there are no default actions for any element defined in Watir Drops. You have to explicitly call foo.text
or foo.set 'bar'
if you want to interact with the element.
It also means that you can use any of the various ways that Watir allows you to locate methods without restriction.
Watir Drops requires you to write out the full definition of the element and stores it inside a closure (the block or curly braces). This allows for setting context with variables that aren't defined until the method is used. Most often this scope will be browser
, but you can also scope them with other defined elements:
class Foo < WatirDrops::PageObject
element(:my_table) { browser.table(id: 'my_table') }
element(:foo) { my_table.td(id: 'foo') }
end
Watir Drops also allows you to pass in information dynamically when desired:
class Foo < WatirDrops::PageObject
element(:foo) { |index| browser.div(class: 'foo', index: index) }
end
class Bar < WatirDrops::PageObject
element(:bar) { |scope| scope.div(class: 'bar') }
end
This allows you to access different elements by passing in parameters along with the name of the element. So Foo.new.foo(0)
will return the first div element that has a class 'foo', etc, and Bar.new.bar(browser.div)
will locate the element inside of the first div element.