; Various functions used to control Selenium, Chrome and Chrome.AHK


;---Javascript---
;------------------------------------------------
JS_TryToExecute(JsToExecute,NumberofAttempts := 1,SleepLength:=1000){
    loop, %NumberofAttempts% {
        try driver.executeScript(JsToExecute)
        catch e {
            Continue
        }
        Return 
    }
    Return "Failed"
}



; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithClassName(ClassName:="",ClassIndexNum:=0,ValueToCheck:="textContent",SleepLength:=1000,JSStringText:="TEXT"){
    jsSend = document.getElementsByClassName('%ClassName%')[%ClassIndexNum%].value = "%JSStringText%";
    try driver.executeScript(jsSend)  

    sleep, %SleepLength%

    jsCheck = return document.getElementsByClassName('%ClassName%')[%ClassIndexNum%].%ValueToCheck%;
    try Status := driver.executeScript(jsCheck) 
    ; Msgbox % "Status: " Status

    if(Status = "")
    return "Failed"
    else, 
    return ""
}

; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithNAME(Element:="",IndexNum:=0,ValueToCheck:="textContent",SleepLength:=1000,JSStringText:=""){
    jsSend = document.getElementsByName('%Element%')[%IndexNum%].value = "%JSStringText%";
/*Clipboard := jsSend
Msgbox % "jsSend: " jsSend

*/
; document.getElementsByClassName('%ClassName%')[%ClassIndexNum%].value = "%JSStringText%";
try driver.executeScript(jsSend)  

sleep, %SleepLength%

jsCheck = return document.getElementsByName('%Element%')[%IndexNum%].%ValueToCheck%;
; Clipboard := jscheck
; Msgbox % "jsCheck: " jsCheck
try Status := driver.executeScript(jsCheck) 
; Msgbox % "Status: " Status
if(Status = "")
return "Failed"
else, 
return ""
}

; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithID(Element:="",ValueToCheck:="textContent",SleepLength:=1000,JSStringText:=""){
  ; Msgbox % "Element: " Element
  ; Msgbox % "JSStringText: " JSStringText
  jsSend = document.getElementById('%Element%').value = "%JSStringText%";
  ; Clipboard := jsSend
  ; Msgbox % "jsSend: " jsSend
  try driver.executeScript(jsSend)  

  ; Msgbox % "JSStringText: " JSStringText
  sleep, %SleepLength%

  jsCheck = return document.getElementById('%Element%').%ValueToCheck%;
  try Status := driver.executeScript(jsCheck) 
  if(Status = "")
  return "Failed"
  else, 
  return ""
}

;---\Javascript---
;------------------------------------------------


;---Selenium---
;------------------------------------------------
; When called these will try multiple times to click/input into a web element

Selenium_LoopToClickID(IDName,NumOfLoops:=1,SleepLength:=1000){ 
    loop, %NumOfLoops% {
       try driver.findElementsByID(IDName).item[1].click() ; Click on "upload image" button
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}


Selenium_LoopToClickName(ElementName,NumOfLoops:=1,SleepLength:=1000){ 
    loop, %NumOfLoops% {
       try driver.findElementsByName(ElementName).item[1].click()
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}


Selenium_LoopToSendValueToID(IDName,NumOfLoops:=1,SleepLength:=1000,StringTextContent:=""){
    loop, %NumOfLoops% {
       try driver.findElementsByID(IDName).item[1].sendKeys(StringTextContent) ; Click on "upload image" button
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}
; Selenium_LoopToSendValueByName(ElementName:="NAME",NumOfLoops:=2,SleepLength:=1000,StringTextContent:="TEXT")
Selenium_LoopToSendValueByName(ElementName,NumOfLoops:=1,SleepLength:=1000,StringTextContent:=""){
    loop, %NumOfLoops% {
     try driver.findElementsByName(ElementName).item[1].SendKeys(StringTextContent)
     catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}

Selenium_LoopToSendValueToXpath(Xpath,NumOfLoops:=1,SleepLength:=1000,StringTextContent:=""){
    loop, %NumOfLoops% {
        ; ToolTip, Loop attempt: %A_index%
       try driver.FindElementByXPath(Xpath).sendKeys(StringTextContent) ; Click on "upload image" button
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}


Selenium_LoopToClickXpath(Xpath,NumOfLoops:=1,SleepLength:=1000){
    loop, %NumOfLoops% {
       try driver.FindElementByXPath(Xpath).click()
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}

Selenium_LoopToClickXpathAndWaitForOpenWindow(Xpath,NumOfLoops:=1,SleepLength:=1000,WindowName:="Open"){
    loop, %NumOfLoops% {
        ; TooltipThis("Clicking xpath")
        try driver.FindElementByXPath(Xpath).click()
        catch e {
         if(A_index = NumOfLoops){
            Return "Failed to Click Xpath or Open File window did not show up on click"
        }
        sleep, %SleepLength%
        Continue
    }
    ; tooltipthis("Checking if window exists")
    sleep, 1000
    ; Msgbox % "WindowName: " WindowName
    if(!WinExist(WindowName)){
        Message = %WindowName% not found on %A_index% attempt. 
        ; tooltipthis("Window not found")

        sleep, %SleepLength%
        Continue
    }
    Return 
}
}


Selenium_LoopToClearXpath(Xpath,NumOfLoops:=1,SleepLength:=1000){
    loop, %NumOfLoops% {
       try driver.FindElementByXPath(Xpath).clear()
       catch e {
         if(A_index = NumOfLoops){
            Return "Failed"
        }
        sleep, %SleepLength%
        Continue
    }
    Return 
}
}

;---\Selenium---
;------------------------------------------------
