Seleniu...nection(3)

πŸ“¦ version 1.49, in opensuse-leap 16.0

πŸ’… Selenium::Remote::Mock::RemoteConnection β€” the NAME bit

okay so bestie, this is a page from the User Contributed Perl Documentation, and the whole thing is about Selenium::Remote::Mock::RemoteConnection β€” literally just a utility class to mock the responses from a Selenium server. that's it. that's the vibe. πŸŽ€

✨ VERSION ✨

version 1.49 β€” periodt.

🌸 SYNOPSIS 🌸

πŸ¦‹ Record interactions

ok so picture this: you wanna record everything your driver does. here's the whole thing, copy it exactly, no cap:

use strict;
use warnings;
use Selenium::Remote::Driver;
use Selenium::Remote::Mock::RemoteConnection;

# create a new Mock object to record the interactions with Selenium
# Server
my $mock_connection = Selenium::Remote::Mock::RemoteConnection->new( record => 1 );

# the Mock object is passed to the driver in place of what would be
# a regular Selenium::Remote::RemoteConnection object
my $driver = Selenium::Remote::Driver->new( remote_conn => $mock_connection );

# always store the session id, as it will become undef once
# $driver->quit is called
my $session_id = $driver->session_id;

# do all the selenium things and quit
$driver->get('http://www.google.com');
$driver->get('http://www.wikipedia.com');
$driver->quit;

# dump the session to a file
$mock_connection->dump_session_store( 'my_record.json' );

so this code up here does some basic Selenium interactions, and then it literally ends up generating a JSON file with all the requests and their responses for your Selenium session. the JSON file is giving:

'{
    "HTTP_REQUEST_URL {request_parameters}":[response1,response2,...],
    ...
}'

and why do we store an array of responses, you ask? because the exact same request can be made more than once during a session, so we have to store every response to the same requests. obviously. πŸ’β€β™€οΈ

🧚 Replay interactions

#!perl
use strict;
use warnings;
use Test::More;
use Test::Selenium::Remote::Driver;
use Selenium::Remote::Mock::RemoteConnection;
my $mock_connection_2 =
  Selenium::Remote::Mock::RemoteConnection->new( replay => 1,
    replay_file => 'my_record.json' );
# javascript + version parameters added or else it will not work
my $driver =
  Test::Selenium::Remote::Driver->new( remote_conn => $mock_connection_2, javascript => 1, version => '' );
$driver->get_ok('http://www.google.com');
$driver->get_ok('http://www.wikipedia.com');
$driver->quit;
done_testing;

so we take the file we generated with the recording snippet from the section before, and boom β€” we're able to mock the responses. iconic.

⚠️ but heads up: there's one small limitation (that the author hopes to remove in future versions!) β€” a record generated with Selenium::Remote::Driver is not directly useable with Test::Selenium::Remote::Driver. this is mainly because the way the two instances are created are a bit different, which leads to different requests made, for creating a session for instance. so for now, what works for sure is recording and replaying from the same class. iykyk 😭

πŸ’œ Mock responses

#!perl
use Test::More;
use Test::Selenium::Remote::Driver;
use Selenium::Remote::WebElement;
use Selenium::Remote::Mock::Commands;
use Selenium::Remote::Mock::RemoteConnection;

my $spec = {
    findElement => sub {
        my (undef,$searched_item) = @_;
        return { status => 'OK', return => { ELEMENT => '123456' } }
          if ( $searched_item->{value} eq 'q' );
        return { status => 'NOK', return => 0, error => 'element not found' };
    },
    getPageSource => sub { return 'this output matches regex'},
};
my $mock_commands = Selenium::Remote::Mock::Commands->new;

my $successful_driver =
  Test::Selenium::Remote::Driver->new(
    remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
    commands => $mock_commands,
);
$successful_driver->find_element_ok('q','find_element_ok works');
dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found';
$successful_driver->find_no_element_ok('notq','find_no_element_ok works');
$successful_driver->content_like( qr/matches/, 'content_like works');
$successful_driver->content_unlike( qr/nomatch/, 'content_unlike works');

done_testing();

ok so mocking responses by hand? that requires more advanced knowledge of the underlying implementation of Selenium::Remote::Driver. the thing we're mocking here is the processed response that will be returned by Selenium::Remote::RemoteConnection to the '_execute_command' call. to accomplish this we need, like, two things:

  • a spec β€” a HASHREF which keys are the name of the methods we want to mock. note that those keys should also be valid keys from the _cmds attribute in Selenium::Remote::Commands. the value of each key is a sub which will be given two parameters:
    • $url_params β€” the values that should have been replaced in the URL. for instance, on the example above, it would have been:
{ session_id => 'some_session_id'}
  • $params β€” the original parameters of the request. on the example above it would have been:
{ value => 'q', using => 'xpath'}

the sub used as a value in the spec is not expected to return anything, so you have to craft very carefully what you return so that it will produce the expected result. like, be so intentional about it. 🫢

  • a mock_cmd β€” a Selenium::Remote::Mock::Commands object. this is used mainly to hijack the normal commands so that placeholders do not get replaced in the URLs.

🌈 DESCRIPTION 🌈

Selenium::Remote::Mock::RemoteConnection is a class to act as a short-circuit or a pass through to the connection to a Selenium Server. using this class in place of Selenium::Remote::RemoteConnection allows you to:

  • record interactions with the Selenium Server into a JSON file πŸ“
  • replay recorded interactions from a JSON file to mock answers from the Selenium Server πŸ”
  • mock responses to specific functions 🎭

πŸ’€ BUGS πŸ’€

this code is really early alpha, so its API might change. use with caution ! (yes the space before the exclamation mark is canon, leave her alone)

πŸ‘‘ AUTHORS πŸ‘‘

Current Maintainers:

  • George S. Baugh <george@troglodyne.net>

Previous maintainers:

  • Daniel Gempesaw <gempesaw@gmail.com>
  • Emmanuel PeroumalnaΓ―k <peroumalnaik.emmanuel@gmail.com>
  • Luke Closs <cpan@5thplane.com>
  • Mark Stosberg <mark@stosberg.com>

Original authors:

  • Aditya Ivaturi <ivaturi@gmail.com>

Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child

Copyright (c) 2014-2017 Daniel Gempesaw

Copyright (c) 2018-2021 George S. Baugh

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


and that's the whole page, bestie πŸ’– this lil' beauty is Seleniu...nection(3), rendered by perl v5.42.0 and dated 2023-04-07 β€” go forth and mock responsibly, mwah πŸŽ€βœ¨