#!/bin/sh

set -exu

python3 -m http.server 8000 --bind 127.0.0.1 --directory="$(pwd)" &
pid=$!
trap "kill $pid" EXIT

cat << END | python3
import sys
sys.path.remove('')
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--remote-debugging-port=9229")
chrome_options.add_argument("--window-size=1024x768")
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument("--no-sandbox")

driver = webdriver.Chrome(options = chrome_options)

print("Getting data from http://127.0.0.1:8000")

if driver.get("http://127.0.0.1:8000") == None:
    print("Success.")
else:
    print("Failed!")

print("Looking for a link named 'debian/'")
link = driver.find_element(By.LINK_TEXT, "debian/")

if link.click() == None:
    print("Success.")
else:
    print("Failed!")

print("\nTest seems to be successful!\nTest was using the following HTML data to test the Chrome webdriver.\n")
print("------------------------------- %< -------------------------------")
print(driver.page_source)
print("------------------------------- >% -------------------------------")
END
