[LON-CAPA-cvs] cvs: modules /goltermann/externalTest Issues.txt Known runTestSuite.pl /goltermann/externalTest/t InterfaceTest.pm ProblemTest.pm Template.pm Utility.pm

goltermann goltermann at source.lon-capa.org
Tue May 7 07:36:12 EDT 2013


goltermann		Tue May  7 11:36:12 2013 EDT

  Added files:                 
    /modules/goltermann/externalTest	Issues.txt runTestSuite.pl 
    /modules/goltermann/externalTest/t	InterfaceTest.pm ProblemTest.pm 
                                      	Template.pm Utility.pm 

  Modified files:              
    /modules/goltermann/externalTest	Known 
  Log:
  initial commit
  stable versioninitial commit
  stable version
  
  
-------------- next part --------------

Index: modules/goltermann/externalTest/Issues.txt
+++ modules/goltermann/externalTest/Issues.txt

Index: modules/goltermann/externalTest/runTestSuite.pl
+++ modules/goltermann/externalTest/runTestSuite.pl
#!perl;
use strict;
use warnings;
use Test::WWW::Selenium;
use Test::More;
plan tests => 1;
#use Smart::Comments;
use lib ('.','./t');

# -- LONCAPA Testsuite --
# This will run several automated tests for LONCAPA.
# For GUI related tests you need a running Selenium standalone server for browser automatization.
#
# You can run this script with different parameters.
#       * browser     (overrides selenium settings to use the given browser)
#       * module_name (tries to call the run() method of the given module,
#                      this is useful if you dont want to run the whole test suite but only one test module)
#
# Examples:
#       $ perl runTestSuite.pl                          <- to run all tests in all browsers
#       $ perl runTestSuite.pl ProblemTest              <- to run all tests in ProblemTest.pm
#       $ perl runTestSuite.pl firefox ProblemTest      <- to run ProblemTest.pm with firefox
#
# The script creates and starts a selenium instance for communication with browsers.
# Selenium tests will accept a selenium instance as paramter, so all browser tests can share one selenium instance.
# If there is no instance given, the test will (should, if you write your own tests) create a new selenium instance.
#
# If you want to write your own test module you have to give your module an explicit package name.
# Also the module needs a run() function which contains all the tests included in your module.
# It is recommended to start with the Template.pm to create a your own testmodule! Dont forget to add it to the module list below

# connection settings:--------------------------
my %config = (  host => '141.41.35.158',
                port => '4444',
                browser_url => 'http://141.41.35.157/',
                maximize => '1',
              );

my %user =   (  name => 'TestUser1',
                pw => 'testuser1',
                domain => 'fhwfdev2',
              );

# development settings:-------------------------
my $local = 0;              # localhost instead of given ip
my $singleBrowser = 0;      # if u dont want to iterate the whole browser list

# geändert our
my $sel;

# if you want to use chrome you will need this (path on host machine):
my $pathToChrome='C:\Program Files\Google\Chrome\Application\chrome.exe';

# list of browsers to test
my @browsers=('firefox',"googlechrome $pathToChrome",'safari','iexplore');
my @modules=('ProblemTest','InterfaceTest');

if($singleBrowser){
    @browsers = ('firefox'); # you can change this!
}
if($local){
    $config{'host'} = 'localhost';
}

# arguments are used to override the default settings (run all browsers, run all modules)
# you can chose to run a specific browser and/or testmodule
foreach my $arg (@ARGV){
    if($arg ~~ @browsers){
        @browsers = ($arg);
    }elsif($arg ~~ @modules){
        @modules = ($arg);
    }else{
        print "undef argument $arg\n";
        die;
    }
}


print "LON-CAPA testing script Version 0.5.12\nPlease read documentation.\n";


# --- start of testing ---

use_ok('WWW::Selenium');
use_ok('Test::WWW::Selenium');
use_ok('Utility');


# go for all the browsers
foreach my $browser (@browsers){

    print "    starting $browser\n";
    $sel = &Utility::startBrowser($browser, %config);
    subtest 'testrun in '.$browser => sub {
        # go for all the modules
        foreach my $module (@modules){
            subtest 'module '.$module => sub {
                use_ok($module);
                $module->run(%user);
            }

        }# end of modules

    };
    &Utility::endBrowser();
}# end of browsers

END {
    # done_tesing() is important to let the suite know when all tests are done
    done_testing();
    # if there were errors selenium will be closed here
    $sel->stop($sel) if $sel;
}
# testing ended
Index: modules/goltermann/externalTest/t/InterfaceTest.pm
+++ modules/goltermann/externalTest/t/InterfaceTest.pm
#!perl
use strict;
use warnings;
package InterfaceTest;
use Test::More;
use Test::WWW::Selenium;
use Utility;


my $maxTimeout = 30000;
my $sel;


# for now this only sends a internal msg to yourself and checks if this arrived
# TODO delete the sent msg to keep it tidy
sub run(){
    shift; # _important_ to have the right hash to work with
    my %user = @_;

    &Utility::login(%user);
    &sendmessage(%user);
}

# send and recieve of internal messages
# contains 2 tests (for now!)
sub sendmessage{

    $sel = Utility::getActiveBrowser();
    my %user = @_;
    my $address = $sel->{'browser_url'};        # url stem from selenium instance
    my $timestamp = localtime;
    my $uniqueText = 'Dies ist eine Testnachricht.';

    # send a mail
    $sel->open($address.'adm/email?compose=individual');
    $sel->wait_for_page_to_load($maxTimeout);
    $sel->type('recuname', $user{'name'});
    $sel->type('subject', $timestamp);
    $sel->type('message', $uniqueText);
    $sel->click_ok('send', undef, 'mail sent');
    $sel->pause(2000);

    # check for new mail
    my $source = $sel->get_html_source();
    ok($source =~ /<a href="\/adm\/email\?display=(.*?)">$timestamp<\/a>/, 'mail recieved');

    # gettin id for deleting the mail
    $1 =~ /display=(.*?)&/;
    my $clickme = $1;



}
1;
Index: modules/goltermann/externalTest/t/ProblemTest.pm
+++ modules/goltermann/externalTest/t/ProblemTest.pm
#!perl
use strict;
use warnings;
package ProblemTest;
use Test::More;
use Test::WWW::Selenium;
# use Smart::Comments;
use Utility;

my $problemname = 'uniqueTestProb.problem';
my $maxTimeout = 30000;
my $oldConstrSpace = 1;              #set this if you use the old construction space (<version_2_11_X)

my $override = 1; # CAUTION! may cause data loss!
my $autologout = 1;
my $fast = 0;

my $sel;


# runs all tests
sub run(){
    shift; # get rid of some crap, _important_ to have the right hash to work with

    my %user = @_;
    $sel = Utility::getActiveBrowser();

    print "        ProblemTest: Skipping some tests due to fast mode\n" if $fast;

    ### %user

    # start your test functions here:
    &Utility::logout();     # to terminate old sessions
    &Utility::takeScreenshot();
    &badLogin() unless $fast;
    &Utility::login(%user);
    &Utility::takeScreenshot();
    &createProblem($sel, %user);
    &Utility::takeScreenshot();
    &testMaxima() unless $fast;

    # Cleanup and finishing test module:
    &cleanup($sel, %user);
}

END{
    &Utility::logout() if $autologout;
}

# Creates an empty problem
# Contains 3 tests
sub createProblem(){
    my($sel,%user) = @_;

# check if previous login was ok
#    unlike($sel->get_html_source(),qr/href="\/adm\/loginproblems.html"/, 'Login');
    $sel->open('/adm/roles');
    $sel->wait_for_page_to_load($maxTimeout);
    $sel->click("//input[\@name='au".$user{'domain'}."']", undef, 'rolechoice author');
    $sel->wait_for_page_to_load($maxTimeout);

    if($oldConstrSpace){
        $sel->open("/priv/$user{'name'}/");
    }else{
        $sel->open("/priv/$user{'domain'}/$user{'name'}/");
    }
    $sel->wait_for_page_to_load($maxTimeout);
    $sel->select('css=select[name="action"]', 'value=newproblemfile');
    $sel->type('name=newfilename', $problemname);

    # Test1: create .problem file:
    $sel->click_ok('//span/input[3]', undef, 'creating problemfile');
    $sel->wait_for_page_to_load($maxTimeout);

    # Test 2: confirm problem creation
    if($sel->get_html_source() =~ /<span class="LC_warning">/){

        # if test problem already exists
        print "Bail out! $problemname already exists. cancel to prevent data loss." unless $override;
        $sel->click_ok('css=input[type="submit"]', undef, "$problemname overwritten");
    }else{

        # else select blank template
        $sel->click('css=input[type="submit"]');
        $sel->wait_for_page_to_load($maxTimeout);
        $sel->check("//input[\@value='/home/httpd/html/res/adm/includes/templates/blank.problem']");
        $sel->click_ok('name=newfile', undef, 'chose blank problem template');
    }
    $sel->wait_for_page_to_load($maxTimeout);
}

# Inserts the XML of a maxima problem
# Contains 2 tests
sub testMaxima(){
    my($sel,%user) = @_;

    my $maxproblemxml = &Utility::getProblemXML_Maxima();
    # open problem in editor [Test 1]
    if($oldConstrSpace){
        $sel->open_ok("/priv/$user{'name'}/$problemname?forceedit=1", undef, 'open XML editor');
    }else{
        $sel->open_ok("/priv/$user{'domain'}/$user{'name'}/$problemname?editmode=Edit&problemmode=editxml", undef, 'open XML editor');
    }
    $sel->wait_for_page_to_load($maxTimeout);
    # Paste problem xml
    $sel->type('name=editxmltext',$maxproblemxml);

    # [Test 2] finish creating problem
    $sel->click('name=subsaveview');
    $sel->wait_for_page_to_load($maxTimeout);
    # ok if no error message
    unlike($sel->get_html_source(), qr/<span class="LC_error">/, 'create problem');

    # calculate 20 answers, this may take a while
    $sel->click('//div[3]/span/input');
    $sel->pause(1000);

    # waiting time per problem * nr problem * 1000ms:
    $sel->wait_for_page_to_load(2.5*20*1000); #~2.5sec per answer should be fine. if there is a timeout it takes 10sec per timed out answer
}

# Deletes the created problem
# Contains 1 test
sub cleanup(){
    my($sel,%user) = @_;

    if($oldConstrSpace){
        $sel->open("/priv/$user{'name'}/$problemname");
    }else{
        $sel->open("/priv/$user{'domain'}/$user{'name'}/$problemname");
    }
    $sel->wait_for_page_to_load($maxTimeout);
    # clicking the delete button
    if($oldConstrSpace){
        $sel->click('//li[4]/a/span');
    }else{
        $sel->click('//ul/li[4]/a/span');
    }
    $sel->wait_for_page_to_load($maxTimeout);
    # cleaning up[Test 1]
    $sel->click_ok("css=input[type='submit']", undef, 'cleanup done');
    $sel->wait_for_page_to_load($maxTimeout);
}

# Login with bad pw
# Contains 2 tests
sub badLogin(){
    my($sel,%user) = @_;

    # page opens
    $sel->open_ok('/adm/roles', undef, 'login page online');
    $sel->wait_for_page_to_load($maxTimeout);
    $sel->type('id=uname', $user{'name'});
    $sel->type('//div[2]/div/form/input[4]', '§wo at 1g3_f5z??w"i#f'); # make sure that this is not an actual password...
    $sel->submit('name=client');
    $sel->wait_for_page_to_load($maxTimeout);
    # ok if no link to loginproblems help page
    ok($sel->get_html_source() =~ /href="\/adm\/loginproblems.html"/, 'bad password rejected');
}
1;
Index: modules/goltermann/externalTest/t/Template.pm
+++ modules/goltermann/externalTest/t/Template.pm
#!perl
use strict;
use warnings;
package Template;
use Test::Simple;



# optional
BEGIN{
    # e.g. getting running selenium instance # OMG nein! bad example, causes bugs! dont do it
    # but using BEGIN blocks might be useful for some other things
}


# mandatory!
sub run(){
    shift; # you need this if you pass a hash between different packages when using $module->run();
    my($sel, %config) = @_;

    # call your testfunctions here

    # please logout after inBrowser testing, leave it tidy for the person (/test) after you
}


sub myTestFunction(){
    print 'Hi there';
}

# optional
END{
    # e.g. logout
}

1;
Index: modules/goltermann/externalTest/t/Utility.pm
+++ modules/goltermann/externalTest/t/Utility.pm
#!perl
use strict;
use warnings;
package Utility;
use Test::More;
use Test::WWW::Selenium;
# use Smart::Comments;

my $maxTimeout = 30000;
my $screenCount;            # for screenshot naming
my $sel;
my $browser;
my %user;
my %config;

# this starts a selenium instance and a browser
sub startBrowser(){
    ($browser, %config) = @_;

    ### start browser
    ### $browser
    ### %config
    $screenCount = 1;
    $sel = Test::WWW::Selenium->new(   host => $config{'host'},                    # your host
                                       port => $config{'port'},                    # port
                                       browser => "*$browser",                     # the browser you want to use (asterisk is necessary!)
                                       browser_url => $config{'browser_url'},      # url of the loncapa server you want to test
                                      );
    $sel->open('/');        # if you wnat to maximize you first have to open a page
    $sel->window_maximize() if $config{'maximize'};
    return $sel;
}

sub endBrowser(){
    $sel->stop();
    $sel = 0;
}

sub getActiveBrowser(){
    return $sel;
}

# Login for loncapa
sub login(){
    %user = @_;

    $sel->open('/adm/roles');
    $sel->wait_for_page_to_load($maxTimeout);

    if($sel->get_title() =~ /login/i){
        $sel->type('id=uname', $user{'name'});
        $sel->type('//div[2]/div/form/input[4]', $user{'pw'});
        $sel->submit('name=client');

        # skip "entering $coursename..." page:
        $sel->pause(2000);
    }else{
        print 'Assuming you are already logged in...\n';
    }
}

# Simple Logout
sub logout(){
    $sel->open('/adm/logout');
    $sel->wait_for_page_to_load($maxTimeout);
}

sub getProblemXML_Maxima(){
   return <<END;
<problem>
<formularesponse id="214">
<answergroup type="ordered">
<answer name="both" type="ordered">
<value>1</value> <value>2</value> <value>3</value> <value>4</value><value>5</value>
</answer>
</answergroup>
<textline readonly="no"></textline>
<textline readonly="no"></textline>
<textline readonly="no"></textline>
<textline readonly="no"></textline>
<textline readonly="no"></textline>
</formularesponse>
</problem>
END
}


# creates a screenshot (of the whole screen!) and names it automatically
sub takeScreenshot(){
    my $bname = $browser;
    if($bname =~ / /){
        $bname =~ s/(^.*?) .*$/$1/;
    }
    $sel->capture_screenshot($bname."_".$screenCount.".png");
    $screenCount += 1;
}










1;


More information about the LON-CAPA-cvs mailing list