high-level web testing

14
High-level web testing Peter Sergeant Contractor @ NET-A-PORTER

Upload: petersergeant

Post on 05-Dec-2014

5.515 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: High-level Web Testing

High-level web testing

Peter SergeantContractor @ NET-A-PORTER

Page 2: High-level Web Testing
Page 3: High-level Web Testing
Page 4: High-level Web Testing

# $mech is a WWW::Mechanize subclass object

$mech->get('/Fulfilment/Putaway?process_group=844284');

print Dumper $mech->as_data();

# Page contents as data

$VAR1 = {

'metadata' => {

'delivery_number' => '615019',

'process_group_id' => '844284',

'page_type' => 'Surplus',

'Designer' => 'Rows',

'Description' => 'New Description',

'Colour' => 'Black',

'Sales Channel' => 'MRPORTER.COM'

},

'item_list' => [

{

'Size' => 'None/Unknown',

'SKU' => '2106034-001'

},

{

'Size' => 'None/Unknown',

'SKU' => '2106034-005'

}

]

};

Page 5: High-level Web Testing

'Fulfilment/PackingException/ViewContainer' => {

auto_match => qr!^/Fulfilment/PackingException/ViewContainer\?container_id=M\w+$!,

specification => {

orphaned_items => {

location => 'id("orphaned_items")',

transform => 'parse_table',

optional => 1,

},

shipment_items => {

location => 'id("shipment_items")',

transform => 'parse_table',

optional => 1,

},

cancelled_items => {

location => 'id("cancelled_items")',

transform => 'parse_table',

optional => 1,

},

}

},

Page 6: High-level Web Testing

'Fulfilment/Packing/EmptyTote' => {

auto_match => qr!^/Fulfilment/Packing/EmptyTote!,

specification => {

totes => {

location => 'id("totes")',

transform => 'parse_simple_list',

},

},

},

Page 7: High-level Web Testing

=head2 parse_simple_list

Return a simplified text representation of the li elements under a node.

=cut

sub _xclient_parse_simple_list {

my ($self, $list_node) = @_;

# Find the child elements

my @li_elements = $list_node->find_xpath('li')->get_nodelist();

# Render them as text

my @simplified = map {

$self->_xclient_trim_cell($_->as_text)

} @li_elements;

# And give them back...

return \@simplified;

}

Page 8: High-level Web Testing

# Send the shipment ID

$mech->submit_form_ok({

with_fields => { shipment_nr = $shipment_id }

}, "Shipment ID submitted");

Page 9: High-level Web Testing

# Check we're in the right place, and then submit the form

unless ( $mech->uri->path eq '/Fulfilment/Selection' ) {

die "Expecting to be on /Fulfilment/Selection, actually: " .

$mech->uri->path

}

# Send the shipment ID

$mech->submit_form_ok({

with_fields => { shipment_nr = $shipment_id }

}, "Shipment ID submitted");

Page 10: High-level Web Testing

# Check we're in the right place, and then submit the form

unless ( $mech->uri->path eq '/Fulfilment/Selection' ) {

die "Expecting to be on /Fulfilment/Selection, actually: " .

$mech->uri->path

}

# Send the shipment ID

$mech->submit_form_ok({

with_fields => { shipment_nr = $shipment_id }

}, "Shipment ID submitted");

# Check we didn't get any logical errors

if ( $mech->as_data->error_message ) {

die "HTTP request passed, but the application complained: " .

$mech->as_data->error_message;

}

Page 11: High-level Web Testing

__PACKAGE__->create_fetch_method(

method_name => 'flow_mech__fulfilment__packingexception',

page_description => 'packing Exception Page',

page_url => '/Fulfilment/PackingException'

);

__PACKAGE__->create_form_method(

method_name => 'flow_mech__fulfilment__packingexception_comment',

form_name => 'add_comments',

form_description => 'add comment',

assert_location => qr!^/Fulfilment/Packing/CheckShipmentException!,

transform_fields => sub { return { note_text => $_[1] } },

);

#-------------------------------------------------------------------------------

$framework

->flow_mech__fulfilment__packingexception

->flow_mech__fulfilment__packingexception_comment(

"The quick brown fox ran over the lazy sheep dog"

);

#-------------------------------------------------------------------------------

# ->flow_mech__fulfilment__packingexception()

# Retrieving the packing Exception Page

# URL /Fulfilment/PackingException retrieved successfully via HTTP

# No status message shown

# Retrieved the packing Exception Page

# ->flow_mech__fulfilment__packingexception_comment( "Comment 1:70236" )

# URL [/Fulfilment/Packing/CheckShipmentException] matched assertion

# Searching for the add comment form

# Submitting add comment form

# URL /Fulfilment/Packing/CheckShipmentException?shipment_id=1630767 retrieved successfully via HTTP

# No status message shown

Page 12: High-level Web Testing

# Look for our purchase order

$flow

->flow_mech__goodsin__stockin

->flow_mech__goodsin__stockin_search({

purchase_order_number => $purchase_order_id

});

# Did we find it?

is(

$flow->mech->as_data->{'products'}->[0]->{'PID'},

$product_id,

"Found our product from the purchase order search"

);

# Check the packing slip

$flow

->flow_mech__goodsin__stockin_packingslip( $flow->stock_order->id )

# Is it sensible?

is(

$flow->mech->as_data->{'product_data'}->{'Purchase Order'}->{'value'},

$purchase_order->purchase_order_number,

"Purchase order matched from Stock Order page"

);

Page 13: High-level Web Testing

sub create_form_method {

my ( $class, %args ) = @_;

# This is a good place to check a user is using it correctly...

$class->_auto_methods_check_params(\%args,

{

required => [qw/

method_name form_name form_description assert_location

/],

optional => ['form_button', 'transform_fields'],

});

$class->meta->add_method(

$args{'method_name'} => sub {

my ( $self, @user_options ) = @_;

$self->show_method_name( $args{'method_name'}, @user_options );

$self->assert_location( $args{'assert_location'} )

if defined $args{'assert_location'};

my $transformed_fields = $args{'transform_fields'} ?

$args{'transform_fields'}->( $self, @user_options ) : {};

$self->indent_note("Searching for the $args{'form_description'} form");

my $name = ref($args{'form_name'}) eq 'CODE' ?

$args{'form_name'}->( $self, @user_options ) :

$args{'form_name'};

my $form = $self->mech->form_name( $name );

unless ( $form ) {

croak "Couldn't find a form with name $args{'form_name'}";

}

$self->mech->set_fields( %$transformed_fields );

my $button = ref($args{'form_button'}) eq 'CODE' ?

$args{'form_button'}->( $self, @user_options ) :

$args{'form_button'};

$self->indent_note("Submitting $args{'form_description'} form");

$self->mech->submit_form(

fields => $transformed_fields,

$button ? ( button => $button ) : ()

);

$self->note_status;

return $self;

}

);

}

Page 14: High-level Web Testing

Come and work at Net-A-Porter

• We’re hiring• It’s a lot of fun• Nice offices