A little help with some Perl and XML please?

Discussion in 'Mac Programming' started by Mac Rules, Aug 12, 2009.

  1. Mac Rules macrumors 6502

    Mac Rules

    Joined:
    Jul 15, 2006
    Location:
    Switzerland
    #1
    Hi guys and girls,

    I'm currently trying to write some Perl scripts where a series of webpages are accessed and certain details such as name and coursename are to be entered into the appropriate fields using an xml file that I have created. However, when I run the script the details from the XML file are not being inserted and instead it's placing a random jumble of numbers such as Array (00xcf0040). I think this is because the script is placing the name of the array rather than the data within it.

    If you guys could offer any tips or pointers I'd be most appreciative.

    FYI, I've included the XML and the script below. In case anyone is wondering I'm currently testing with Selenium.

    Thanks!

    XML
    <?xml version="1.0" encoding="utf-8"?>
    <courses>
    <name>Chemistry</name>
    <shortname>CHEM1</shortname>

    <name>Physics</name>
    <shortname>PHYS1</shortname>

    <name>Biology</name>
    <shortname>BIOL1</shortname>
    </courses>

    Script

    use strict;
    use warnings;
    use Time::HiRes qw(sleep);
    use Test::WWW::Selenium;
    use Test::More "no_plan";
    use Test::Exception;
    use XML::Simple;
    use Data::Dumper;

    my $xml = new XML::Simple;
    my $data = $xml->XMLin("Courselist.xml", KeyAttr=>'courses');
    my $sel = Test::WWW::Selenium->new( host => "localhost",
    port => 4444,
    browser => "*chrome",
    browser_url => "http://localhost/moodle/" );

    $sel->open_ok("/moodle/");

    my $count = 4;
    until ($count = 0) {
    $sel->click_ok("link=Courses");
    $sel->click_ok("link=Add/edit courses");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("//input[\@value='Add a new course']");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->type_ok("id_fullname", ($data->{name}));
    $sel->type_ok("id_shortname",($data->{shortname}));
    $sel->click_ok("id_submitbutton");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("//input[\@value='Click here to enter your course']");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->open_ok("/moodle/");
    $count--;
    }
     
  2. Mac Rules thread starter macrumors 6502

    Mac Rules

    Joined:
    Jul 15, 2006
    Location:
    Switzerland
    #2
    Sorry, I might add that my goal here is to get each entry entered then the next, hence the loop. I tried using a foreach loop but Selenium just closed down once it reached the first step of the loop, so if you have any thoughts on that too..
     
  3. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #3
    As you can see $data actually looks like this:

    Code:
    $VAR1 = {
              'shortname' => [
                             'CHEM1',
                             'PHYS1',
                             'BIOL1'
                           ],
              'name' => [
                        'Chemistry',
                        'Physics',
                        'Biology'
                      ]
            };
    
    
    So rework your loop to look like something like this:

    Code:
    #!/local/openpkg/bin/perl -w
    
    use strict;
    use Data::Dumper;
    
    use XML::Simple ;
    
    my $xml = new XML::Simple;
    my $data = $xml->XMLin("courses.xml", KeyAttr=>'courses');
    
    for my $index (0..@{$data->{name}}-1) {
    #your stuff goes here:
     print "Shortname: $data->{shortname}[$index] :: Name: $data->{name}[$index]\n";
    #
    };
    
    :qw
     
  4. Mac Rules thread starter macrumors 6502

    Mac Rules

    Joined:
    Jul 15, 2006
    Location:
    Switzerland
    #4
    @ChOas

    I can't thank you enough for that!!! It works perfectly!

    I've only just been assigned this project so thrown in the deep-end somewhat and still trying to fathom Perl and Selenium out, but this has saved me from s much stress.
     
  5. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #5
    Anytime :)

    Don't know much about Selenium, but Perl in general I can help quite a bit with :)
     

Share This Page