Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^4: Using WWW::Selenium To Test Or Automate An Ajax Website

by Limbic~Region (Chancellor)
on Nov 25, 2009 at 14:24 UTC ( #809338=note: print w/replies, xml ) Need Help??

in reply to Re^3: Using WWW::Selenium To Test Or Automate An Ajax Website
in thread Using WWW::Selenium To Test Or Automate An Ajax Website

Any advice on how I actually install Selenium core on Ubuntu?

There really isn't anything to install. You just download the jar files and run them. Looking at the error message you got, I would guess you don't have Java installed. You need to install Java to run Selenium. WWW::Selenium is a perl interface to Selenium Remote Control only.

Did I get this code position correct?

Yes. You should always check the SYNOPSIS in TFM which shows you the typical usage.

Cheers - L~R

  • Comment on Re^4: Using WWW::Selenium To Test Or Automate An Ajax Website

Replies are listed 'Best First'.
Re^5: Using WWW::Selenium To Test Or Automate An Ajax Website
by icleave (Initiate) on Nov 27, 2009 at 16:58 UTC

    Thanks for the tips! I finally figured it all out except for the class path stuff (I have no idea how to create a java classpath to the .jar file) but I eventually experimented until I figured out that I can run one terminal running selenium rc and open another terminal and run my perl code from that.

    Low and behold my perl code comes to life and firefox loads up... only to die time and time again with a popup box that loads when you click 'login' on the website I'm trying to automate access to. After spending a whole day learning about how you need to select the popup box and close it, then select 'null' to get back to the original browser window I just can't seem to get my perl/selenium code to do what I want. The main issue is that upon clicking the 'logon' link at the website I'm trying to automate access, it is random event where by:

    1. No popup box pops up, or
    2. A popup box named 'stealth' pops up, or
    3. A popup box named 'entry' pops up.

    I found that selenium is very fragile when it comes to coding the next step for it to preform... if I run my code without any mention to a popbox then everything is fine, but when I run the code and a popup box happens then it dies with a "255" exit message. Looking around the options in the IDE I couldn't see any program flow commands for selenium IDE so after going to the selenium website it seemed to me that program flow must come from the coding language you are using.

    So I thought, ok, Perl has program flow commands, why not use the "if" statement and "elseif" and "else" for all three possibilities:

    1. no popup
    2. popup "stealth"
    3. popup "entry".

    I coded it up as best as I could in Perl but then I got syntax errors where my blocks where. I was under the impression that something like $sel->title_is("Entry") would give perl a 'true' or 'false' and operate the program flow accordingly. Please help, I'm quite confused, a sample of the code is below:

    my $sel = Test::WWW::Selenium->new( host => "localhost", port => 4444, browser => "*chrome", browser_url => "http://www.example" ); $sel->start(); $sel->set_timeout("60000"); $sel->open("/home/index.jsp"); $sel->click("link=Log in"); $sel->wait_for_page_to_load("60000"); if ($sel->title_is("Entry")) { $sel->select_window("entry"); $sel->close(); $sel->select_window("null"); } else ($sel->title_is("Stealth")) { $sel->select_window("stealth"); $sel->close(); $sel->select_window("null"); } $sel->click("emailAddress"); $sel->type("emailAddress", ""); $sel->click("password"); $sel->type("password", "password"); $sel->click("remember"); $sel->click("subButton"); $sel->wait_for_page_to_load("60000");
      Are you using the Selenium IDE FireFox plug-in to record your session and then modify it? Also, you are using Test::WWW::Selenium not WWW::Selenium. I think you want the latter not the former.

      I am not sure how to help you. Your code sample is fairly useless without the real website so that I can try it here to see what you are doing wrong. I am sorry, but unless you want to share the website and possibly login credentials, I can't guess what you are doing wrong.

      Update: As to your syntax errors - you should always use strict and use warnings. You have used else when you meant elsif. Also, you should either \ or you should use single quotes because it thinks your email address is really an array.

      Cheers - L~R


        Q: Are you using Selenium IDE FireFox plug-in to record?

        A: Yes. I have it record what I want to do, then I use the export function to put the Selenium commands into perl, then copy and paste into a text editor and remove the "_ok"s and place "sel->start();" in the code.

        RE: Test::WWW::Selenium - Thanks I changed that, but my code doesn't seem to run how I want it to.

        RE: Real website and login - The real website and my login is real in the code below.

        RE: Else, elsif, \@ - Thanks, I changed that too but my code doesn't seem to run how I want it to.

        Desired outcome:

        1. Go to
        2. Login (and handle any pops if they happen
        3. Check the number of points in the account and put into a scalar variable $points
        4. Open a file handle <CODES> and read the codes into an array @codes
        5. Enter the codes into the website and check against the $points scalar variable to see if they were entered correctly - printing if the code was successful or not
        6. Carry on for each of codes in the array @codes
        7. Logout when done

        Progress: I've only gotten as far as no. 2 in the Selenium IDE, and I can only get to no. 1 using the Selenium RC and Perl.

        I'm pretty confident I can write a file handle and read it into an array and for each of the codes get Selenium to input them into the CokeZone website, however it's no use if I can't even automate a login that handles the popups!

        Code I've written to try to login and handle the popups:

        #! /usr/bin/perl use strict; use warnings; use WWW::Selenium; my $sel = WWW::Selenium->new( host => "localhost", port => 4444, browser => "*chrome", browser_url => "http://www.cokezon" ); $sel->start(); $sel->open("/home/index.jsp"); $sel->click("link=Log in"); $sel->wait_for_page_to_load("30000"); if ( $sel->is_text_present("Thanks for visiting!\n\nNow here's your ch +ance to tell us what you think. This quick and easy survey will only +take you a few minutes to fill out, and it'll help us to get more of +the stuff you want to see into 'Coke Zone'.\n\nThanks for your help,\ +nThe 'Coke Zone' Team") == 1 ) { $sel->select_window("entry"); $sel->close(); $sel->select_window("null"); } if ( $sel->is_text_present("At the end of your visit on this website p +lease take the time to complete an on-line survey, which will provide + us with feedback on the website. Please do not close this window.\n\ +nEnjoy your visit") == 1 ) { $sel->select_window("stealth"); $sel->close(); $sel->select_window("null"); } $sel->click("emailAddress"); $sel->type("emailAddress", "bogus\"); $sel->type("password", "password"); $sel->click("remember"); $sel->click("subButton"); $sel->wait_for_page_to_load("30000"); my $points = $sel->get_text("//div[\@id='header_links']/div[1]/div[1]/ +h3"); print $points $sel->click("link=Log out"); exit 0;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://809338]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2018-07-23 06:13 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.