I'm not an Angular person, so I stole a clickable mat-chip example from https://stackoverflow.com/a/47962183/1200545. Depending on your implementation, you may need to tweak the code below.
First off, Page-Object has a limitation when defining widgets that use a custom tag name - ie "mat-chip". To get around this, we will define an associated method/locator in Watir:
module Watir
module Container
def mat_chip(*args)
HTMLElement.new(self, extract_selector(args).merge(tag_name: "mat-chip"))
end
def mat_chips(*args)
HTMLElementCollection.new(self, extract_selector(args).merge(tag_name: "mat-chip"))
end
end
end
We can then create a widget and accessors in Page-Object:
class MatChip < PageObject::Elements::Element
def self.accessor_methods(widget, name)
widget.send('define_method', "check_#{name}") do
self.send("#{name}_element").check
end
widget.send('define_method', "uncheck_#{name}") do
self.send("#{name}_element").uncheck
end
widget.send('define_method', "#{name}_checked?") do
self.send("#{name}_element").checked?
end
end
def set(bool = true)
click unless set? == bool
end
alias check set
def clear
set(false)
end
alias uncheck clear
def set?
attribute_value('aria-selected') == 'true'
end
alias checked? set?
PageObject.register_widget :mat_chip, self, :mat_chip
end
You can then use the accessor, named "mat_chip", the same was as a checkbox:
class MyPage
include PageObject
mat_chip('mychip', index: 1)
end
page = MyPage.new(browser)
page.check_mychip
p page.mychip_checked?
#=> true
page.uncheck_mychip
p page.mychip_checked?
#=> false
Alternatively, you can use the mat-chip directly as a nested element:
page.mat_chip_element.check
p page.mat_chip_element.checked?
#=> true
page.mat_chip_element.uncheck
p page.mat_chip_element.checked?
#=> false
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…