DEV Community

Cover image for Automate Firefox browser with Perl
Gaurav Rai
Gaurav Rai

Posted on • Edited on

Automate Firefox browser with Perl

There are various module available on metacpan which have help you with the automating the Firefox browser.

  1. WWW::Mechanize::Firefox - automate through the Mozrepl plugin. However, it is retired from the Mozilla platform in November 2017. The last known compatible version is Firefox 54. (Take a look at WWW::Mechanize::Chrome if you are looking for Chrome (or Chromium) browser. The chrome version is relevent unlike firefox).
  2. Firefox::Marionette - Automate the Firefox browser with the Marionette protocol
  3. Selenium::Firefox - It is part of Selenium::Remote::Driver and support multiple browsers.

There are others also but some of them are long deprecated and not recommended.

We will be using Firefox::Marionette for automation. To know more about Marionette protocol have a look at its documentation

We have already created a login page during our last exercise. We would be using the same example (except reCAPTCHA) for our automation.

Lets take a quick look at it.
Alt Text
We will be filling the username and password and clicking on the 'Login' button through automated process.
Lets have a quick look at the HTML body of this page-

<body>
    <h1>Login</h1>
    <form action="/login" method="post">
        <label>username:</label> <input name="username" type="text">
        <br><br>
        <label>password:</label> <input name="password" type="password">
        <br><br>
        <input id="submit" type="submit" value="Log in">
    </form>
</body>
Enter fullscreen mode Exit fullscreen mode

Lets start with writing some code

#!/usr/bin/env perl

use strict;
use warnings;
use Firefox::Marionette;

sub main {
    my $url           = "http://localhost:3000/";
    my $firefox       = Firefox::Marionette->new(visible => 1);
    my $window_handle = $firefox->new_window(type => 'window', focus => 1, private => 1);
    $firefox->switch_to_window($window_handle);
    $firefox->go($url);
}

main();
Enter fullscreen mode Exit fullscreen mode

Lets go through each line in main function.

  • We have our server running locally on 3000 port hence that url. You can update it to your own url.
  • In next line we are creating the object. We have passed visible as 1 so that we can see the live browser otherwise it will start in headless mode. There are various parameter available. You can use them as per your requirements.
  • Next we are saying to open a Firefox windows with different parameters. Here, we are saying open a new window in private mode and put it in foreground. Have a look at new_window for more detail.
  • switch_to_window switch the focus to the said window
  • go($url) instruct the Firefox to go to the given URL

After hitting the url, we will get the login page. Now we have fill the fields with proper value ans submit the form.
If you look at the HTML we can see there are 3 input fields which can distinguished based on there name and id value. E.g.

  • For username, find a field with name eq username and fill it
  • For password, find a field with name eq password and fill it
  • For login button, find by id having value submit and click it.

Lets complete the code written previously.

sub main {
    my $url           = "http://localhost:3000/";
    my $firefox       = Firefox::Marionette->new(visible => 1);
    my $window_handle = $firefox->new_window(type => 'window', focus => 1, private => 1);
    $firefox->switch_to_window($window_handle);
    $firefox->go($url);

    # For username
    my $element = $firefox->find_name('username');
    # Remove if there is something already filled there
    $firefox->clear($element);
    # Fill it with username i.e. admin
    $firefox->type($element, "admin");

    # For password
    $element = $firefox->find_name('password');
    # Remove if there is something already filled there
    $firefox->clear($element);
    # Fill it with password i.e. admin
    $firefox->type($element, "admin");

    # This is just for your eyes so that you can see what is going on
    sleep 5;

    # Find login button by the given id and click it
    $firefox->find_id('submit')->click();

    sleep 10;
}
Enter fullscreen mode Exit fullscreen mode

I have added the comment for clarity. Lets look it in action.
Save the file and run it.
Alt Text
After auto button click it will redirect to home page

Alt Text

I have also created a gif that will give more insight on the live action.

Alt Text

Taking screenshot

You can also take screenshot of the web page. The selfie method will take the screenshot, return the PNG file and save it locally(by default inside /tmp/ dir). You can also change the location where you what to save it by copying the content to different file.
Let's add the below code in end

 sub main {
    ....
        # This will take screenshot of the whole document
        my $file_handle = $firefox->selfie();

        # This will only taken screenshot of the element specified
        # my $file_handle = $firefox->selfie($firefox->find_class(<class_name>));

        open(my $screenshot, '>', "firefox_output_screen.png") or die "Unable to open file: $!";
        binmode($screenshot);
        while (my $line = <$file_handle>) {
            print $screenshot $line;
        }
        close($screenshot);
    ...
}
Enter fullscreen mode Exit fullscreen mode

The file will be saved in current working directory.

Perl onion logo taken from here
Firefox logo taken from here

Top comments (0)