Wednesday, 10 August 2022

perl selenium automation complete tutorial with code examples

First, we need to install the Selenium WebDriver for Perl, which can be done using the CPAN command:

cpan Selenium::WebDriver

Once the installation is done, you can start writing the code. Here's an example of how to use Perl Selenium to navigate to a website and perform a search:


use strict;

use warnings;

use Selenium::WebDriver;


my $driver = Selenium::WebDriver->new(

    'remote_server_addr' => 'http://localhost:4444/wd/hub',

    'browser_name' => 'chrome',

);


$driver->get('https://www.google.com/');

my $search_box = $driver->find_element('name', 'q');

$search_box->send_keys('Perl Selenium Tutorial');

$search_box->submit();

Let's break down the code:

  1. We import the Selenium::WebDriver module and enable strict and warnings mode for better error handling.
  2. We create a new instance of the Selenium::WebDriver class and specify the Selenium server's address and the browser we want to use (in this case, Google Chrome).
  3. We use the get() method to navigate to the Google homepage.
  4. We locate the search box element using the find_element() method and provide the element's name attribute and value as arguments.
  5. We use the send_keys() method to type our search query into the search box.
  6. Finally, we use the submit() method to submit the search query.

Here's another example that shows how to interact with dropdown menus using Perl Selenium:

use strict;

use warnings;

use Selenium::WebDriver;


my $driver = Selenium::WebDriver->new(

    'remote_server_addr' => 'http://localhost:4444/wd/hub',

    'browser_name' => 'chrome',

);


$driver->get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select');

$driver->switch_to->frame('iframeResult');


my $dropdown = $driver->find_element('xpath', '//select[@name="cars"]');

my $option = $dropdown->find_element('xpath', '//option[@value="saab"]');

$option->click();

Above code navigates to a website that contains a dropdown menu and selects a specific option from it. Let's break it down:

  1. We import the Selenium::WebDriver module and enable strict and warnings mode for better error handling.
  2. We create a new instance of the Selenium::WebDriver class and specify the Selenium server's address and the browser we want to use (in this case, Google Chrome).
  3. We use the get() method to navigate to the website that contains the dropdown menu.
  4. We switch to the iframe that contains the dropdown menu using the switch_to() method and providing the iframe's ID or name.
  5. We locate the dropdown menu element using the find_element() method and provide the element's XPath as an argument.
  6. We locate the specific option we want to select using the find_element() method on the dropdown menu element and providing the option's XPath as an argument.
  7. We use the click() method to select the option.

These are just two simple examples of what you can do with Perl Selenium automation. With the Selenium WebDriver for Perl, you can automate a wide range of tasks on websites and web applications.

Method 1: Filling out a form

use strict;

use warnings;

use Selenium::WebDriver;


my $driver = Selenium::WebDriver->new(

    'remote_server_addr' => 'http://localhost:4444/wd/hub',

    'browser_name' => 'chrome',

);


$driver->get('https://www.w3schools.com/html/html_forms.asp');

my $name_field = $driver->find_element('xpath', '//input[@name="fname"]');

my $last_name_field = $driver->find_element('xpath', '//input[@name="lname"]');

my $submit_button = $driver->find_element('xpath', '//input[@type="submit"]');


$name_field->send_keys('John');

$last_name_field->send_keys('Doe');

$submit_button->click();

Above code navigates to a web page containing a form, fills out the form with some sample data and submits it. The XPath is used to locate the form fields and submit button.

Method 2: Taking a screenshot of a web page

use strict;

use warnings;

use Selenium::WebDriver;


my $driver = Selenium::WebDriver->new(

    'remote_server_addr' => 'http://localhost:4444/wd/hub',

    'browser_name' => 'chrome',

);


$driver->get('https://www.google.com/');

my $file_name = 'google_homepage.png';

$driver->capture_screenshot($file_name);


Above code navigates to the Google homepage and captures a screenshot of the page. The capture_screenshot() method is used to capture the screenshot and save it to a file.

Method 3: Login and logout:

use Selenium::Remote::Driver;


my $driver = Selenium::Remote::Driver->new(

    browser_name => 'firefox'

);


# Navigate to the login page

$driver->get('https://example.com/login');


# Enter username and password

my $username_field = $driver->find_element('username_field_selector');

my $password_field = $driver->find_element('password_field_selector');


$username_field->send_keys('my_username');

$password_field->send_keys('my_password');


# Click the login button

my $login_button = $driver->find_element('login_button_selector');

$login_button->click();


# Wait for the page to load

$driver->wait_for_page_to_load();


# Logout

my $logout_button = $driver->find_element('logout_button_selector');

$logout_button->click();

Method 4:Form submission:

use Selenium::Remote::Driver;


my $driver = Selenium::Remote::Driver->new(

    browser_name => 'firefox'

);


# Navigate to the form page

$driver->get('https://example.com/form');


# Enter form data

my $name_field = $driver->find_element('name_field_selector');

my $email_field = $driver->find_element('email_field_selector');

my $message_field = $driver->find_element('message_field_selector');


$name_field->send_keys('John Doe');

$email_field->send_keys('john.doe@example.com');

$message_field->send_keys('This is a test message');


# Submit the form

my $submit_button = $driver->find_element('submit_button_selector');

$submit_button->click();


# Wait for the form to be submitted

$driver->wait_for_page_to_load();


# Check that the form submission was successful

my $success_message = $driver->find_element('success_message_selector');

if ($success_message->get_text() eq 'Form submitted successfully') {

    print "Form submitted successfully\n";

} else {

    print "Form submission failed\n";

}



Method 5:Scrolling:

use Selenium::Remote::Driver;


my $driver = Selenium::Remote::Driver->new(

    browser_name => 'firefox'

);


# Navigate to a page with a long scrollable list

$driver->get('https://example.com/list');


# Scroll down to the bottom of the page

$driver->execute_script('window.scrollTo(0, document.body.scrollHeight);');


# Scroll up to the top of the page

$driver->execute_script('window.scrollTo(0, 0);');



Method 6: Capturing screenshots:

use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new(
    browser_name => 'firefox'
);

# Navigate to a page
$driver->get('https://example.com/page');

# Take a screenshot of the page
my $screenshot_data = $driver->screenshot();

# Save the screenshot to a file
open(my $fh, '>', 'screenshot.png') or die $!;
binmode($fh);
print $fh $screenshot_data;
close($fh);

Method 7:Clicking on links:

use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new(
    browser_name => 'firefox'
);

# Navigate to a page with links
$driver->get('https://example.com/links');

# Click on a link
my $link = $driver->find_element('link_selector');
$link->click();

# Wait for the page to load
$driver->wait_for_page_to_load();


Method 8:Filling out forms:

use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new(
    browser_name => 'firefox'
);

# Navigate to a page with a form
$driver->get('https://example.com/form');

# Fill out the form
my $name_field = $driver->find_element('name_field_selector');
my $email_field = $driver->find_element('email_field_selector');
my $message_field = $driver->find_element('message_field_selector');

$name_field->send_keys('John Doe');
$email_field->send_keys('john.doe@example.com');
$message_field->send_keys('This is a test message');

# Submit the form
my $submit_button = $driver->find_element('submit_button_selector');
$submit_button->click();

# Wait for the form to be submitted
$driver->wait_for_page_to_load();



Method 9:Verifying page content:

use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new(
    browser_name => 'firefox'
);

# Navigate to a page with some content
$driver->get('https://example.com/page');

# Check that the page contains some text
my $page_content = $driver->get_page_source();
if ($page_content =~ /some text/) {
    print "Page contains 'some text'\n";
} else {
    print "Page does not contain 'some text'\n";
}



Method 10:Handling alerts:

use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new(
    browser_name => 'firefox'
);

# Navigate to a page with an alert
$driver->get('https://example.com/alert');

# Click on a button that triggers an alert
my $button = $driver->find_element('alert_button_selector');
$button->click();

# Switch to the alert and accept it
my $alert = $driver->switch_to_alert();
$alert->accept();

# Wait for the alert to disappear
$driver->wait_for_alert_to_disappear();






Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home