Subclass.pm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package Selenium::Subclass;
  2. #ABSTRACT: Generic template for Selenium sugar subclasses like Selenium::Session
  3. use strict;
  4. use warnings;
  5. use v5.28;
  6. no warnings 'experimental';
  7. use feature qw/signatures/;
  8. =head1 CONSTRUCTOR
  9. =head2 $class->new($parent Selenium::Client, $data HASHREF)
  10. You should probably not use this directly; objects should be created as part of normal operation.
  11. =cut
  12. sub new ($class,$parent,$data) {
  13. my %lowkey;
  14. @lowkey{map { lc $_ } keys(%$data)} = values(%$data);
  15. $lowkey{parent} = $parent;
  16. my $self = bless(\%lowkey,$class);
  17. $self->_build_subs($class);
  18. # Make sure this is set so we can expose it for use it in various other calls by end-users
  19. if ( $self->{sortfield} eq 'element-6066-11e4-a52e-4f735466cecf') {
  20. $self->{sortfield} = 'elementid';
  21. $self->{elementid} = delete $self->{'element-6066-11e4-a52e-4f735466cecf'};
  22. }
  23. return $self;
  24. }
  25. sub _request ($self, $method, %params) {
  26. #XXX BAD SPEC AUTHOR, BAD!
  27. if ( $self->{sortfield} eq 'elementid') {
  28. # Ensure element childs don't think they are their parent
  29. $self->{to_inject}{elementid} = $self->{elementid};
  30. }
  31. # Inject our sortField param, and anything else we need to
  32. $params{$self->{sortfield}} = $self->{$self->{sortfield}};
  33. my $inject = $self->{to_inject};
  34. @params{keys(%$inject)} = values(%$inject) if ref $inject eq 'HASH';
  35. # and ensure it is injected into child object requests
  36. # This is primarily to ensure that the session ID trickles down correctly.
  37. # Some also need the element ID to trickle down.
  38. # However, in the case of getting child elements, we wish to specifically prevent that, and do so above.
  39. $params{inject} = $self->{sortfield};
  40. $self->{callback}->($self,$method,%params) if $self->{callback};
  41. return $self->{parent}->_request($method, %params);
  42. }
  43. sub DESTROY($self) {
  44. return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
  45. $self->{destroy_callback}->($self) if $self->{destroy_callback};
  46. }
  47. #TODO filter spec so we don't need parent anymore, and can have a catalog() method
  48. sub _build_subs($self,$class) {
  49. #Filter everything out which doesn't have {sortField} in URI
  50. my $k = lc($self->{sortfield});
  51. #XXX deranged field name
  52. $k = 'elementid' if $self->{sortfield} eq 'element-6066-11e4-a52e-4f735466cecf';
  53. foreach my $sub (keys(%{$self->{parent}{spec}})) {
  54. next unless $self->{parent}{spec}{$sub}{uri} =~ m/{\Q$k\E}/;
  55. Sub::Install::install_sub(
  56. {
  57. code => sub {
  58. my $self = shift;
  59. return $self->_request($sub,@_);
  60. },
  61. as => $sub,
  62. into => $class,
  63. }
  64. ) unless $class->can($sub);
  65. }
  66. }
  67. 1;