import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.support.ui.WebDriverWait
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.WebElement

System.setProperty("webdriver.chrome.driver", "c:/u/chromedriver.exe")
val chdr = ChromeDriver()
val HOST = "https://prod-delivery-01.gso.internal/"
val HOST = "https://memberonline.qsuper.qld.gov.au/"
val wait = WebDriverWait(chdr, 10)

fun wait_and_find(p01: By): WebElement {
 return wait.until(ExpectedConditions.elementToBeClickable(p01))
}

chdr.get(HOST)
chdr.findElement(By.id("login_username")).sendKeys("[email protected]")
chdr.findElement(By.id("login_password")).sendKeys("Ma1whostai", Keys.ENTER)

// FYI ONLY: shortcut in case I want to browse straight to a URL instead of simulate keystrokes and mouseclicks
// chdr.get(HOST + "your-super/super/investments/your-investments/")
// chdr.get(HOST + "your-super/super/insurance/your-insurance/")
// chdr.get(HOST + "your-super/super/account-history/transactions/")
// chdr.get(HOST + "your-super/super/account-history/request-history/")

// navigate to Money Map and navigate back out
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.linkText("Money Map")).click()
// works but a bit obscure
// wait_and_find(By.id("main")).findElement(By.tagName("input")).click()
// clearer
wait_and_find(By.xpath("//input[@title='Go to Money Map']")).click()
wait_and_find(By.linkText("Back to Your Super")).click()

// navigate to Online Advice and navigate back out
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Advice']")).sendKeys(Keys.ENTER)
wait_and_find(By.linkText("Get Advice")).click()
wait_and_find(By.xpath("//a[@title='Go to Online Advice']")).click()
// wait_and_find(By.xpath("//*[@class='navbar-toggle']")).click() // takes longer than 10sec
WebDriverWait(chdr,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='navbar-toggle']"))).click()
wait_and_find(By.xpath("//button[text()='Back to Your Super']")).click()
wait_and_find(By.xpath("//button[text()='Yes']")).click()

// navigate to Statements
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Super']")).sendKeys(Keys.ENTER)
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Account history']")).sendKeys(Keys.ENTER)
wait_and_find(By.linkText("Statements")).click()

// navigate to Your Insurance
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Super']")).sendKeys(Keys.ENTER)
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Insurance']")).sendKeys(Keys.ENTER)
wait_and_find(By.linkText("Your Insurance")).click()

// navigate to Transactions
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Super']")).sendKeys(Keys.ENTER)
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Account history']")).sendKeys(Keys.ENTER)
wait_and_find(By.linkText("Transactions")).click()

// navigate to Add to super
wait_and_find(By.linkText("Your account")).click()
wait_and_find(By.xpath("//*[@data-ec-expand='Expand Add to super']")).sendKeys(Keys.ENTER)

// logout and exit
wait_and_find(By.partialLinkText("Logout")).click()
chdr.quit()