RF-AutoIT: Cant upload files in Chrome - robotframework

I am trying to create a common exe for 3 browser based file upload using below code.
IE (Choose File to Upload),
Chrome (Open),
Firefox (File Upload)
This is working for IE but not for Chrome and FireFox. There is no error but its not showing given file along with path.
I will be using this .exe in my Robot Framework script.
#include <MsgBoxConstants.au3>
SelectWindowBasedOnTitle()
Func SelectWindowBasedOnTitle()
$winList = WinList()
$wTitle = CheckWindows($winList)
IF $wTitle == "Choose File to Upload" Then
IE()
ElseIf $wTitle == "Open" Then
Chrome()
Else
FireFox()
EndIf
EndFunc
Func CheckWindows($aArray)
For $i = 1 To Ubound($aArray) - 1
If WinActive($aArray[$i][1]) Then $wTitle= $aArray[$i][0] ;MsgBox(0, "Window Check", $aArray[$i][0] & " is active.")
Next
Return $wTitle
EndFunc
Func Example()
$wText = WinGetText("[ACTIVE]")
EndFunc
Func IE()
ControlFocus("Choose File to Upload","","Edit1")
ControlSetText("Choose File to Upload","","Edit1",$CmdLine[1])
ControlClick("Choose File to Upload","","Button1")
EndFunc
Func Chrome()
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1",$CmdLine[1])
ControlClick("Open","","Button1")
EndFunc
Func FireFox()
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1",$CmdLine[1])
ControlClick("File Upload","","Button1")
EndFunc
This way i am using in my Robot script. Could you please help me understand is there a way to automate
${FileInfo} ${CURDIR}\\Xpaths.txt
${AutoIT} ${CURDIR}\\BrowserBasedWindowSelection.exe
Run Process ${AutoIT} ${FileInfo}

Related

AutoIt Firefox _FFClick doesn't work on button? (FF.au3)

Using the firefox plugin ("ff-au3") for AutoIt, how do I click a button?
Here is the HTML of the item I'd like to click:
<input accesskey="s" class="aui-button" id="login-form-submit" name="login" title="Press Alt+s to submit this form" type="submit" value="Log In">
And here is the code snippet to click the button:
;Click the "Log In" button, id is "login-form-submit"
_FFClick("login-form-submit", "id")
At this point, my script is already connected to firefox, already on the page I need, and everything else works (except for this click part!)
Here is the error I get back:
_FFClick ==> No match: $sElement: FFau3.WCD.getElementById('login-form-submit')
Also, this works when I manually run it on the page using a javascript console:
document.getElementById("login-form-submit")
And here is the API for the plugin: http://english.documentation.ff-au3.thorsten-willert.de/ff_functions/_FFClick.php
Anyone see anything I'm doing wrong?
Versions:
Firefox 44.0.1
AutoIt v3.3.14.2
FF V0.6.0.1b-15.au3 (Firefox plugin)
MozRepl v.1.1.2
SciTE-Lite v. 3.5.4
Well this didn't get much traffic but I found the solution! Pretty simple... I disconnected from firefox before executing the click!
When using firefox, you first need to open the firefox exe with the "Run" command, then you need to connect to firefox using the "_FFConnect" command. Next you can start clicking elements. Once you're done, disconnect from firefox using the "ProcessClose" command. The problem I was running into was I connected to firefox, then disconnected immediately, then I tried clicking. So, I made sure I disconnected after I did the clicking...
Working solution: myScript.au3 (See the "LogIn" function at the bottom)
#include <Constants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <EventLog.au3>
#include <FF V0.6.0.1b-15.au3> ;FireFox
OpenLog()
OpenFirefox()
ConnectToFirefox()
LogIn()
; ////////////////////////////////////////////////////
; Configure the Log
; ////////////////////////////////////////////////////
Func OpenLog()
Global $log = FileOpen("K:\Log.txt", 2)
; Check if file opened for reading OK
If $log == -1 Then
FileWrite($log,"[ERROR] Could not open log file." & #CRLF)
MsgBox(0, "Error", "Unable to open log file.", [ timeout = 0])
Exit
Else
FileWrite($log,"[INFO] Opened log file successfully." & #CRLF)
EndIf
EndFunc
; ////////////////////////////////////////////////////
; Open Firefox
; ////////////////////////////////////////////////////
Func OpenFirefox()
;Run Firefox in Maximized
Global $ffPid = Run("C:\Program Files (x86)\Mozilla Firefox\firefox.exe","",#SW_MAXIMIZE)
; Check if firefox was opened OK
If #error <> 0 Then
FileWrite($log,"[ERROR] Could not open firefox." & #CRLF)
MsgBox(0, "Error", "Could not open firefox.", [ timeout = 0])
Exit
Else
FileWrite($log,"[INFO] Firefox opened successfully." & #CRLF)
EndIf
;Wait 10 seconds for Firefox to open
$result = WinWait("[CLASS:MozillaWindowClass]","",10)
; Check if file opened for reading OK
If $result == 0 Then
FileWrite($log,"[ERROR] Unable to open firefox class." & #CRLF)
MsgBox(0, "Error", "Unable to open firefox class.", [ timeout = 0])
Exit
Else
FileWrite($log,"[INFO] Opened firefox class successfully." & #CRLF)
EndIf
;Wait for 2 seconds after opening
Sleep(2000)
EndFunc
; ////////////////////////////////////////////////////
; Connect To Firefox
; ////////////////////////////////////////////////////
Func ConnectToFirefox()
; trying to connect to a running FireFox with MozRepl on
If _FFConnect(Default, Default, 3000) Then
FileWrite($log,"[INFO] Connected to Firefox." & #CRLF)
Else
FileWrite($log,"[ERROR] Can't connect to FireFox!" & #CRLF)
MsgBox(64, "", "Can't connect to FireFox!")
EndIf
;Wait for 2 seconds after opening
Sleep(2000)
EndFunc
; ////////////////////////////////////////////////////
; Log into page
; ////////////////////////////////////////////////////
Func LogIn()
;Load Login Page
_FFOpenURL("http://localhost/login.jsp")
Sleep(2000)
If #error <> 0 Then
FileWrite($log,"[ERROR] Could not open URL." & #CRLF)
MsgBox(0, "Error", "Could not open URL.", [ timeout = 0])
Exit
Else
FileWrite($log,"[INFO] Opened URL successfully." & #CRLF)
EndIf
Sleep(2000)
;Click the "Log In" button, id is "login-form-submit"
;<input accesskey="s" class="aui-button" id="login-form-submit" name="login" title="Press Alt+s to submit this form" type="submit" value="Log In">
_FFClick("login-form-submit", "id")
If #error <> 0 Then
FileWrite($log,"[ERROR] Could not click login button." & #CRLF)
MsgBox(0, "Error", "Could not click login button:", [ timeout = 0])
Exit
Else
FileWrite($log,"[INFO] Found and clicked login button successfully." & #CRLF)
EndIf
EndFunc

Click on the outlook calendar by searching the calendar Image (_ImageSearch)

I am using Imagesearch.au3 to search the calendar image on outlook . Below is the code .
Created a new folder --> ImageSearch.au3 , ImageSearchDLL.dll , and the screenshot of the calendar is placed
#include <ImageSearch.au3>
$x=0
$y=0
start()
while 1
sleep(1000)
WEnd
Func Start()
while 1
$result = _ImageSearch("cal.png",1,$x,$y,0)
if $result=1 Then
ConsoleWrite("Sucesfull")
MouseMove($x,$y,10)
Else
ConsoleWrite("Img not found")
EndIf
WEnd
EndFunc
Problem : Throws an error message
"C:\Users...\Desktop\AutoIT\New folder\img\ImageSearch.au3" (44) : ==> Subscript used on non-accessible variable.:
if $result[0]="0" then return 0
if $result^ ERROR
The dll will not load while running as a script. I'm not sure why but it seems others have the same problem. I was able to compile the script as a 64 bit application and then the application loaded the DLL. After changing the tolerance, I was able to find the image.
Also, I would rewrite your code. You have two infinite while loops for no reason. I added a sleep to the remain while loop. I would increase the tolerance.
#include <ImageSearch.au3>
start()
Func Start()
while 1
$x=0
$y=0
$result = _ImageSearch("cal.png",1,$x,$y,20)
if $result=1 Then
MsgBox(0,"Found Image","moving mouse")
MouseMove($x,$y,10)
Else
MsgBox(0,"Img not found","not found")
EndIf
sleep(10000)
WEnd
EndFunc
I tested with the 64bit imagesearch located at http://www.autoitscript.com/forum/topic/148005-imagesearch-usage-explanation/page-2

Run many AutoIt script files using a main file (XML, Java, AutoIt or any language)

I'm using AutoIt for testing my application. There are different test cases for that. I will create these test cases as au3 files. Now I want to run all these scripts one after other. That is, a main script which call all sub script files one after other. How can I do that?
In a main AutoIt script (main.au3) you just add:
#include <UDF_function_1.au3>
#include <UDF_function_2.au3>
#include <UDF_function_3.au3>
Func _lanch_all()
; In order to run AutoIt function
_function_1() ; from UDF_function_1.au3
_function_2() ; from UDF_function_2.au3
_function_3() ; from UDF_function_3.au3
Run(PATH_to_script\"script_1.bat") ; In order to run a batch script windows
Run(PATH_to_executable\"script_2.exe") ; In order to run an executable
EndFunc
And for example UDF_function_1.au3 contains:
#include-once
Func _function_1()
ConsoleWrite("Call of _function_1"&#CRLF)
EndFunc
If you don't want to compile these test cases, you just compile the main exe:
Compiled.exe [/ErrorStdOut] [/AutoIt3ExecuteScript file] [params ...]
Execute another script file from a compiled AutoIt3 Script File. Then you don't need to fileinstall another copy of AutoIT3.exe in your compiled file.
#include <File.au3>
$Path = #ScriptDir & 'TestCases\'
$files = _FileListToArray($Path, "*.au3")
For $i = 1 To $files[0]
RunWait(#ScriptFullPath & '/AutoIt3ExecuteScript "' & $Path & $files[$i] & '.au3"')
Next
Just create a master AutoIt program and have this program run the other sub programs. If they are present in .au3 format, simply have the master program include the sub programs via the include keyword:
#include "[path\]filename"
This will allow the master program to call functions of the included sub-program.
#include <File.au3>
$Path = #ScriptDir & 'TestCases\'
$files = _FileListToArray($Path, "*.au3")
For $i = 1 To $files[0]
RunWait(#ScriptFullPath & '/AutoIt3ExecuteScript "' & $Path & $files[$i] & '.au3"')
Next
modified code as follows:
#include <File.au3>
$Path = #ScriptDir & '\TestCases\'
$files = _FileListToArray($Path, "*.au3")
MsgBox(0,"path", $files[0])
For $i = 1 To $files[0]
RunWait(#AutoItExe & " /AutoIt3ExecuteScript "& '"'& $Path & $files[$i] & '"')
Next

autoit: run application and open file from internal menu in silent mode

I have an exe-application which I close and run many times in day. Every time when I'm running this application after as it started I must do some actions: choose "File" menu and choose file in there.
This code is worked only if I use #SW_MAXIMIZE, but if I run this code with #SW_HIDE - application not started with congiguraion.cfg file
Example()
Func Example()
Local $iPID = Run("c:\Program Files (x86)\Server.exe", "", #SW_MAXIMIZE)
Local $FileName = "c:\Program Files (x86)\congiguraion.cfg"
Sleep(2000)
Send("^o")
Sleep(2000)
Send($FileName)
Sleep(500)
Send("{ENTER}")
EndFunc ;
I want to make exe-file and put it in scheduler with trigger: begin the task at startup. That's why I need to run in silent mode.
P.S.
Also I changed my program:
Example()
Func Example()
Run("c:\Program Files (x86)\Server.exe")
Local $hWnd = WinWaitActive("Server")
Sleep(2000)
ControlSend($hWnd, "", "", "^o")
Sleep(2000)
ControlSend($hWnd, "", "", "c:\Program Files (x86)\congiguraion.cfg{ENTER}")
EndFunc ;
This solution also doesn't work if I tried to run it with #SW_HIDE key or on hidden mode in scheduler.
There is a function called ControlSend. If u want to use it, I suggest to use 'AutoIt Window Info" which is installed with the usual AutoIt installation. With this tool you just got to run the programm and get the control names/ids and fill it into the function.
If you have questions about how ControlSend excatly works feel free to ask :)
Example()
Func Example()
Run("notepad","")
Local $hWnd = WinWaitActive("Unbenannt - Editor")
WinSetState($hWnd,"", #SW_HIDE )
ControlSend($hWnd, "", "", "Sample Text")
ControlSend($hWnd, "", "", "^o")
ControlSend($hWnd, "", "", "MyText.txt{ENTER}")
WinSetState($hWnd,"", #SW_SHOW )
EndFunc ;

autoit- How to minimize windows open on ff.au3?

Here my code:
#Include <FF.au3>
_FFStart()
If _FFIsConnected() Then
$sStringToSearch = "www.google.com"
sleep (2000)
If _FFSearch($sStringToSearch) Then
; add a new tab with an URL
_FFWindowOpen("http://ff-au3-example.thorsten-willert.de/")
Sleep(1000)
_FFWindowOpen("www.msn.com")
Sleep(1000)
_FFWindowOpen("www.yahoo.com")
Sleep(1000)
_FFWindowOpen("google.com")
EndIf
Else
EndIf
According to the script, when I visit google, it would open the first link followed by the other links; now how do I have the windows minimized once they are open.
Also I keep getting ErrorstdOut any solutions to this?
Try WinSetState ( "title", "text", flag )
Here is the link for the api: http://www.autoitscript.com/autoit3/docs/functions/WinSetState.htm
A sample is :
WinSetState("Calculator", "", #SW_MINIMIZE )

Resources