On Thu, Nov 03, 2005 at 04:17:48PM -0200, Alexandre Bairos wrote:
> > If anyone is interested, I could forward it on to the list. Maybe it
> > would be useful to bundle such helper scripts with selenium?
> 
> Cool stuff. I was thinking of implementing such a tool. I?d really
> appreciate your forward/bundled scripts. :)

OK. Right now, my scripts have paths specific to our application:

  Eg, selenium-regen looks for selenium in:
  $Config{prefix}/var/www/selenium

I need to abstract this functionality out to a library, and then we
can create simple scripts that call the library function with the
appropriate path.

Perhaps I should create a WWW::Selenium::Utils package on CPAN that
includes this functionality?

What would you guys find most useful?

I've attached my current script so that you can get a feel for it.  I
have some further enhancements planned too, so maybe CPAN is a good
place for it (and an excuse to publish my first CPAN package :).

Luke

-- 
Luke Closs
PureMessage Developer 
There is always time to juggle in the Sophos Zone.
#!/usr/bin/perl
use strict;
use warnings;
use Config;
use File::Find;
use Getopt::Long;

my $seldir = "$Config{prefix}/lib/manager/unauthenticated/selenium";
GetOptions( 'seldir=s' => \$seldir,
          ) or usage();

sub usage {
    die <<EOT;
USAGE: $0 [specific files to regenerate]

By default, $0 will regenerate all files found in selenium's tests directory.
EOT
}

die "Sorry, $seldir is not a directory!\n" unless -d $seldir;
my $suite = "$seldir/tests/TestSuite.html";
my $date = localtime;

#print the header
print "Creating new test suite at $suite...\n";
open(my $fh, ">$suite.tmp") or die "Can't open $suite.tmp: $!";
print $fh <<EOT;
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" 
          http-equiv="content-type">
    <title>Test Suite</title>
  </head>
  <body>
    Generated at $date<br />
    <table cellpadding="1" cellspacing="1" border="1">
      <tbody>
        <tr><td><b>Test Suite</b></td></tr>
        
EOT

# print the tests
my @tests;
# check for individual test wiki files that need to be regen
# file names are specified as script arguments
if (scalar @ARGV) {
    @tests = @ARGV;
}
# assume all test wiki files need to be regen
else {
    find(sub {
            my $n = $File::Find::name;
            return if -d $n;
            return unless m#(?:wiki|html)$#;
            $n =~ s#^.+/tests/##;
            push @tests, $n;
        }, "$seldir/tests");
}
# foo.wiki
# something/foo.wiki
foreach (sort {$a cmp $b} @tests) {
    next if /(?:\.tmp|TestSuite\.html)$/;
    next if /(.+)\.html$/ and -e "$seldir/tests/$1.wiki";

    my $f = $_;
    print "Adding row for $f\n";
    if (/\.wiki$/) {
        $f = wiki2html("$seldir/tests/$f");
    }
    (my $base = $f) =~ s#^.+/tests/(.+)\.html$#$1#;
    print $fh qq(\t<tr><td><a href="./$f">$base</a></td></tr>\n);
}

#print the footer
print $fh <<EOT;
      </tbody>
    </table>
  </body>
</html>
EOT

# save and rename into place
close $fh or die "Can't close $suite.tmp: $!";
rename "$suite.tmp", $suite or die "can't rename $suite.tmp $suite: $!";


sub wiki2html {
    my $wiki = shift;
    (my $html = $wiki) =~ s#\.wiki$#.html#;

    open(my $in, $wiki) or die "Can't open $wiki: $!";
    my $title = <$in>;
    chomp $title;
    $title =~ s#^\|(.+)\|$#$1#;
    print "Generating html for ($title): $html\n";

    open(my $out, ">$html") or die "Can't open $html: $!";
    print $out <<EOT;
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
          http-equiv="content-type">
    <title>$title</title>
  </head>
  <body>
    <table cellpadding="1" cellspacing="1" border="1">
      <tbody>
        <tr>
          <td rowspan="1" colspan="3">$title</td>
        </tr>
EOT
    while(<$in>) {
        next if /^#/ or /^\s*$/;
        chomp;
        # nice regex, eh?  It just splits on |
        if (/^\s*\|(.+)\|\s*$/) {
            my ($cmd, $opt1, $opt2) = split /\|/, $1, 3;
            die "No command found! ($_)" unless $cmd;
            $opt1 ||= '&nbsp;';
            $opt2 ||= '&nbsp;';
            print $out "\n<tr><td>$cmd</td><td>$opt1</td><td>$opt2</td></tr>\n";
        }
        else {
            warn "Invalid line: $_\n";
        }
    }
    close $in or die "Can't close $wiki: $!";

    print $out <<EOT;
      </tbody>
    </table>
  </body>
</html>
EOT
    close $out or die "Can't write $html: $!";
    return $1 if $html =~ m#.+/tests/(.+)$#;
}
_______________________________________________
Selenium-devel mailing list
Selenium-devel@lists.public.thoughtworks.org
http://lists.public.thoughtworks.org/mailman/listinfo/selenium-devel

Reply via email to