Kernel::System::UnitTest::Selenium

NAME

Kernel::System::UnitTest::Selenium – run front end tests

This class inherits from Selenium::Remote::Driver. You can use its full API (see http://search.cpan.org/~aivaturi/Selenium-Remote-Driver-0.15/lib/Selenium/Remote/Driver.pm).

Every successful Selenium command will be logged as a successful unit test. In case of an error, an exception will be thrown that you can catch in your unit test file and handle with HandleError() in this class. It will output a failing test result and generate a screen shot for analysis.

new()

create a selenium object to run front end tests.

To do this, you need a running selenium or phantomjs server.

Specify the connection details in Config.pm, like this:

    # For testing with Firefox until v. 47 (testing with recent FF and marionette is currently not supported):
    $Self->{'SeleniumTestsConfig'} = {
        remote_server_addr  => 'localhost',
        port                => '4444',
        platform            => 'ANY',
        browser_name        => 'firefox',
        extra_capabilities => {
            marionette     => \0,   # Required to run FF 47 or older on Selenium 3+.
        },
    };

    # For testing with Chrome/Chromium (requires installed geckodriver):
    $Self->{'SeleniumTestsConfig'} = {
        remote_server_addr  => 'localhost',
        port                => '4444',
        platform            => 'ANY',
        browser_name        => 'chrome',
        extra_capabilities => {
            chromeOptions => {
                # disable-infobars makes sure window size calculations are ok
                args => [ "disable-infobars" ],
            },
        },
    };

Then you can use the full API of Selenium::Remote::Driver on this object.

RunTest()

runs a selenium test if Selenium testing is configured.

    $SeleniumObject->RunTest( sub { ... } );

FixedTimeSet()

provide a fixed time in the context of the current test as well as on the server and the client for the following Selenium requests that load an application.

    $SeleniumObject->FixedTimeSet( $TimeToSave );

Please note that this does NOT work in webpack development server environments, only with the Perl production or development web servers.

FixedTimeUnset()

remove a previously set fixed time in the context of the current test as well as on the server and the client for the following Selenium requests that load an application.

    $SeleniumObject->FixedTimeUnset();

get()

Override get method of base class to prepend the correct base URL.

    my $Result = $SeleniumObject->get(
        $URL,
    );

get_alert_text()

Override get_alert_text() method of base class to return alert text as string.

    my $AlertText = $SeleniumObject->get_alert_text();

returns

    my $AlertText = 'Some alert text!'

VerifiedGet()

perform a get() call, but wait for the page to be fully loaded (works only within OTRS). Will die() if the verification fails.

    $SeleniumObject->VerifiedGet(
        $URL,
    );

VerifiedRefresh()

perform a refresh() call, but wait for the page to be fully loaded (works only within OTRS). Will die() if the verification fails.

    $SeleniumObject->VerifiedRefresh();

Login()

Login to agent, customer or external interface.

    $SeleniumObject->Login(
        Type     => 'Agent',            # Agent|Customer|AgentFrontend|External
        User     => 'someuser',
        Password => 'somepassword',
        Mobile   => 0,                  # Login using Mobile layout (default 0)
    );

WaitFor()

wait with increasing sleep intervals until the given condition is true or the wait time is over. Exactly one condition (JavaScript or WindowCount) must be specified.

    my $Success = $SeleniumObject->WaitFor(
        AlertPresent     => 1,                                 # Wait until an alert, confirm or prompt dialog is present
        Callback         => sub { ... }                        # Wait until function returns true
        ElementExists    => 'xpath-selector'                   # Wait until an element is present
        ElementExists    => ['css-selector', 'css'],
        ElementMissing   => 'xpath-selector',                  # Wait until an element is not present
        ElementMissing   => ['css-selector', 'css'],
        ComponentLoaded  => 'xpath-selector',                  # Wait until Vue.js component has been loaded
        ComponentLoaded  => ['css-selector', 'css'],           #   Please target the component root element only
        TestFlag         => 'Flag::Name',                      # Wait until a test flag is set to a true value
        JavaScript       => 'return $(".someclass").length',   # Javascript code that checks condition
        WindowCount      => 2,                                 # Wait until this many windows are open

        PreserveTestFlag => 1,                                 # optional, whether to preserve test flag value (default 0)
        Time             => 20,                                # optional, wait time in seconds (default 20)
        ErrorMessage     => 'Title not found.',                # optional, human readable error message (if condition is not met)
    );

FindElement()

Wrapper for CPAN find_element that automatically waits for the element.

    my $Success = $SeleniumObject->FindElement(
        'a.Ticket',    # (required) Selector
        'css',         # (optional) Selector method, by default xpath
                       #    Possible options: class, class_name, css, id, link, link_text,
                       #    partial_link_text, tag_name, name, xpath
        0,             # (optional) If set, do not wait until element is shown. By default 0.
    );

ClearTestFlag()

Clear the test flag value.

    my $Success = $SeleniumObject->ClearTestFlag(
        TestFlag => 'Flag::Name',     # Name of the test flag to clear
    );

SwitchToFrame()

Change focus to another frame on the page. If WaitForLoad is passed, it will wait until the frame has loaded the page completely.

    my $Success = $SeleniumObject->SwitchToFrame(
        FrameSelector => '.Iframe',     # (required) CSS selector of the frame element
        WaitForLoad   => 1,             # (optional) Wait until the frame has loaded, if necessary
        Time          => 20,            # (optional) Wait time in seconds (default 20)
    );

DragAndDrop()

Drag and drop an element.

    $SeleniumObject->DragAndDrop(
        Element         => '.Element', # (required) css selector of element which should be dragged
        Target          => '.Target',  # (required) css selector of element on which the dragged element should be dropped
        TargetOffset    => {           # (optional) Offset for target. If not specified, the mouse will move to the middle of the element.
            X   => 150,
            Y   => 100,
        }
    );

HandleError()

use this method to handle any Selenium exceptions.

    $SeleniumObject->HandleError($@);

It will create a failing test result and store a screen shot of the page for analysis (in folder /var/otrs-unittest if it exists, in $Home/var/httpd/htdocs otherwise).

DEMOLISH()

override DEMOLISH from Selenium::Remote::Driver (required because this class is managed by Moo). Performs proper error handling (calls HandleError() if needed). Adds a unit test result to indicate the shutdown, and performs some clean-ups.

DEPRECATED FUNCTIONS

WaitForjQueryEventBound()

waits until event handler is bound to the selected jQuery element. Deprecated – it will be removed in the future releases.

    $SeleniumObject->WaitForjQueryEventBound(
        CSSSelector => 'li > a#Test',       # (required) css selector
        Event       => 'click',             # (optional) Specify event name. Default 'click'.
    );

InputFieldValueSet()

sets modernized input field value.

    $SeleniumObject->InputFieldValueSet(
        Element => 'css-selector',              # (required) css selector
        Value   => 3,                           # (optional) Value
    );

FormSelectValueSet()

Sets value of the form select field.

    $SeleniumObject->FormSelectValueSet(
        FieldLabel => 'State',              # (required) Label of the select field

        OptionLabel => 'closed successful', # (required) Label of the dropdown option
        # or
        OptionLabel => [
            'closed unsuccessful',
            'closed successful',
        ],

        ExactMatch    => 1,                 # (optional) Determines if field or option label must be exact match. By default 1.
                                                         Otherwise, check if field or option label contains value.
        ParentElement => 'xpath-selector',  # (optional) Parent element selector
        Multiselect   => 1,                 # (optional) Skip autodetection mechanism and handle the field as a
                                                         multiselect dropdown, which will not close on selection.

        AutoComplete  => 1                  # (optional) because it's an auto-complete, it writes the first
                                                         option in the input, only then waits for it to show.
    );

FormSelectValueCheck()

Checks for existence or non-existence of an option value in the form select field.

    $SeleniumObject->FormSelectValueCheck(
        FieldLabel => 'State',              # (required) Label of the select field

        OptionLabel => 'closed successful', # (required) Label of the dropdown option
        # or
        OptionLabel => [
            'closed unsuccessful',
            'closed successful',
        ],
        ExactMatch    => 1,                 # (optional) Determines if field or option label must be exact match. By default 1.
                                                         Otherwise, check if field or option label contains value.
        ParentElement => 'xpath-selector',  # (optional) Parent element selector
        CheckMissing  => 1,                 # (optional) Whether to check for element existence (0) or non-existence (1)
    );

_FormSelectExpandDropdown()

Expands the FormSelect drop down and returns the selector and option label as an array.

SeleniumSettingsSet()

Sets needed settings when selenium tests are executed.

    $SeleniumObject->SeleniumSettingsSet();

VueFormFieldValueSet()

sets value of the form field.

    $SeleniumObject->VueFormFieldValueSet(
        URL        => 'frontend/agent/form/ticket/create/email',   # (required) form schema URL, used as form identifier
        Label      => 'Queue',                                     # (required) form Field label, used to identify the field
        Value      => 3,                                           # (optional) new form field value
        ExactMatch => 1,                                           # (optional) Determines if label must be exact match, default is 1
    );
Scroll to Top