*** Settings ***

Documentation    Generally useful keywords for Bika tests.
...              To include this file, use this:
...              *** settings ***
...              Resource  keywords.txt


*** Keywords ***

Log in
    [Arguments]  ${userid}  ${password}
    Go to                          http://localhost:55001/plone/login_form
    Maximize Browser Window
    sleep   0.1
    Input text                      __ac_name      ${userid}
    sleep   0.1
    Input text                      __ac_password  ${password}
    Click Button  Log in


Log out
    Go to                          http://localhost:55001/plone/logout
    Wait until page contains       You are now logged out


Select from dropdown
    [Arguments]        ${locator}
    ...                ${search_text}=
    ...                ${item_number}=1
    wait until page contains element    ${locator}
    Input text                          ${locator}   \
    Press Key                           ${locator}   \\9
    sleep           0.3
    Input text                          ${locator}  ${search_text}
    wait until page contains element    xpath=//div[contains(@class,'cg-colItem')][${item_number}]
    sleep           0.5
    Click Element                       xpath=//div[contains(@class,'cg-colItem')][${item_number}]
    sleep           0.5


SelectDate
    [Arguments]  ${Element}=
    ...          ${Date}=
    ${Date} =            Convert To Integer    ${Date}
    ${Date} =            Convert To String     ${Date}
    Click Element  ${Element}
    Wait Until Page Contains Element  ui-datepicker-div
    sleep                1
    Click Link  ${Date}


SelectPrevMonthDate
    [Arguments]  ${Element}=
    ...          ${Date}=
    Click Element        ${Element}
    Wait Until Page Contains Element  ui-datepicker-div
    sleep                1
    Click Element        xpath=//div[@id='ui-datepicker-div']//a[@title='Prev']
    ${Date} =            Convert To Integer    ${Date}
    ${Date} =            Convert To String     ${Date}
    sleep                1
    Click Link           ${Date}

SelectNextMonthDate
    [Arguments]  ${Element}=
    ...          ${Date}=
    Click Element        ${Element}
    Wait Until Page Contains Element  ui-datepicker-div
    sleep                1
    Click Element        xpath=//div[@id='ui-datepicker-div']//a[@title='Next']
    ${Date} =            Convert To Integer    ${Date}
    ${Date} =            Convert To String     ${Date}
    sleep                1
    Click Link           ${Date}

Execute transition ${transition} on item ${ar_id} in ARList
    [Documentation]  Tranistion the specific ar from an AR List.
    Select checkbox     xpath=//input[@item_title="${ar_id}"]
    Set Selenium Timeout        30
    Click Element       ${transition}_transition
    Set Selenium Timeout        10

Execute transition ${transition} on items in form_id ${form_id}
    [Documentation]  Tranistion all items in the form_id
    Select checkbox     xpath=//input[@id='${form_id}_select_all']
    Set Selenium Timeout        30
    Click Element       ${transition}_transition
    Set Selenium Timeout        10

Execute transition ${transition} inside ClientARView/ManageResults
    [Documentation]  Tranistion the element using the right-up label
    ...              tranistion could be-> sample, receive, verify, publish
    Click Element  css=a[title="Change the state of this item"]
    Click Element  workflow-transition-${transition}

Shleep
    [Arguments]  ${amount}=
    ...          ${comment}=
    Log  Sleeping ${amount}: ${comment}  WARN
    sleep  ${amount}

Simple AR Creation
    [Documentation]  Create a simple AR
    [Arguments]  ${Client}
    ...          ${Contact}
    ...          ${SampleType}
    ...          ${ASCategory}
    ...          ${ASTitle}
    @{time} =                   Get Time        year month day hour min sec
    go to                       ${PLONEURL}/clients/
    click link                  ${Client}
    Wait until page contains element    css=body.portaltype-client
    click link                  link=Add
    Select from dropdown        ar_0_Contact                ${Contact}
    SelectDate                  ar_0_SamplingDate           @{time}[2]
    Select from dropdown        ar_0_SampleType             ${SampleType}
    Set Selenium Timeout        30
    click element               xpath=.//th[@id="cat_lab_${ASCategory}"]
    Select checkbox             xpath=//input[@title='${ASTitle}']
    Click Button                Save
    Wait until page contains    created
    ${ar_id} =                  Get text      //dl[contains(@class, 'portalMessage')][2]/dd
    ${ar_id} =                  Set Variable  ${ar_id.split()[2]}
    [Return]                    ${ar_id}


Wait For Ajax
    Wait for Condition   return window.jQuery.active == 0;
