*** Settings ***
Library           REST    https://googlechromelabs.github.io    AS    chromelabs
Library           Process

*** Tasks ***
Geckodriver
    ${platform}=    Set Variable    linux64
    ${executable}=    Set Variable    geckodriver
    ${destination}=    Set Variable    /usr/local/bin/
    ${result}=    Run Process    curl -s -L https://github.com/mozilla/geckodriver/releases/latest \| grep -E href\=\\".*/releases/tag/v.*\\".*class\=\\"Link\\"    stderr=STDOUT    shell=True
    ${gecko_version}=    Set Variable    ${result.stdout.strip().split(' ')}
    ${result}=    Run Process    geckodriver --version    stderr=STDOUT    shell=True
    ${gecko_installed_version}=    Set Variable    ${result.stdout.strip().split(' ')}
    ${gecko_version}=    Set Variable    ${gecko_version[-1].strip('v')}
    ${gecko_installed_version}=    Set Variable    ${gecko_installed_version[1]}
    Log To Console    \nLatest Geckodriver version: ${gecko_version}
    Log To Console    You have Geckodriver version: ${gecko_installed_version}
    ${url}=    Set Variable
    IF    "${gecko_installed_version}" != "${gecko_version}"
    ${url}=    Set Variable    https://github.com/mozilla/geckodriver/releases/download/v${gecko_version}/geckodriver-v${gecko_version}-${platform}.tar.gz
        Log    Download: ${url}    console=True
    END
    IF    "${url}" != ""
        ${result}=    Run Process    wget ${url}    stderr=STDOUT    shell=True
        IF    ${result.rc} == 0
            ${result}=    Run Process    tar xzvf ${CURDIR}${/}geckodriver-v${gecko_version}-${platform}.tar.gz    stderr=STDOUT    shell=True
            ${result}=    Run Process    chmod a+x ${CURDIR}${/}${executable}    stderr=STDOUT    shell=True
            ${result}=    Run Process    sudo mv ${CURDIR}${/}${executable} ${destination}    stderr=STDOUT    shell=True
            # ${result}= \ \ \ Run Process \ \ \ rm -rf ${CURDIR}${/}geckodriver-v${gecko_version}-${platform}.tar.gz
        END
    END

Chromedriver
    ${platform}=    Set Variable    linux64
    ${executable}=    Set Variable    chromedriver
    ${destination}=    Set Variable    /usr/local/bin/
    ${result}=    Run Process    google-chrome --version \| sed \"s/Google Chrome //g\"    stderr=STDOUT    shell=True
    ${chrome_version}=    Set Variable    ${result.stdout.strip()}
    Log To Console    You have Chrome version:${chrome_version}
    &{res}=    GET    /chrome-for-testing/last-known-good-versions-with-downloads.json
    Integer    response status    200
    ${data}=    Set Variable    ${res.body.channels['Stable'].version}    # .channel['Stable'] \ ['${chrome_version}']
    Log    ${data}    console=True
    ${url}=    Set Variable
    IF    "${chrome_version}" == "${data}"
        # Output \ \ \ ${res.body.channels['Stable']}
        @{data}=    Set Variable    ${res.body.channels['Stable'].downloads['chromedriver']}
        FOR    ${idx}    ${dat}    IN ENUMERATE    ${data}
            IF    "${dat[${idx}]['platform']}" == "${platform}"
                ${url}=    Set Variable    ${dat[${idx}]['url']}
                BREAK
            END
        END
        Log    Download: ${url}    console=True
    END
    IF    "${url}" != ""
        ${result}=    Run Process    wget ${url}    stderr=STDOUT    shell=True
        IF    ${result.rc} == 0
            ${result}=    Run Process    unzip -o -q -d ${CURDIR} ${CURDIR}${/}chromedriver-${platform}.zip    stderr=STDOUT    shell=True
            ${result}=    Run Process    chmod a+x ${CURDIR}${/}chromedriver-${platform}${/}${executable}    stderr=STDOUT    shell=True
            ${result}=    Run Process    sudo mv ${CURDIR}${/}chromedriver-${platform}${/}${executable} ${destination}    stderr=STDOUT    shell=True
            # ${result}= \ \ \ Run Process \ \ \ rm -rf chromedriver-${platform}.zip* chromedriver-${platform}
        END
    END
