2017-08-03 24 views
1

我必須自動化一個場景,例如使用Selenium將圖像從Windows資源管理器上傳到TinyMCE組件。由於Selenium不支持操作系統控制,我使用AutoIt上傳圖像。 AutiIt將圖像路徑設置爲「文件名」,好像它也點擊了「打開」按鈕。但圖像沒有加載到我的「源」字段。從窗口自動上傳圖像用selenium和AutoIT探索

這裏是我的硒命令:

Runtime.getRuntime().exec("C:/XXXXX/src/test/resources/uploadImage.exe"); 
// Path of the AutoIT script file 

AutoIt腳本:

WinActivate("Open") 
Send("C:\LCNGProjects\Screenshots\Images\GifImage1.gif") 
Sleep(5000) 
Send("{ENTER}") 

請參閱隨附的屏幕截圖

Refer the attached screenshot

回答

0

你並不需要使用的AutoIt。自動將使您的腳本只能在Windows機器上可靠。

而不是找到使用上載按鈕的定位器(即xpath)的元素,該上載按鈕的標籤爲type="file"。現在,通過與圖像的絕對路徑,這個簡單的SendKeys在你的機器象下面這樣: -

button2.sendKeys("C:\\UsersDesktop\\logo\\Summit-Logo-900px.png"); 

或者使用

System.getProperty("user.dir")); 

附加文件的路徑exluding項目 例子裏面你的項目路徑和文件名: -

button.sendKeys(System.getProperty("user.dir")+"\\logo\\Summit-Logo-900px.png"); 

下面鏈接中包含更多選項上傳文件象下面這樣: -

  • 使用SendKeys命令。
  • 使用AutoIt腳本。
  • 使用Jacob com界面。

http://www.techbeamers.com/handle-file-upload-selenium-webdriver/#h3

希望它會幫助你:)

0

我不打算在這裏添加完整的源代碼。您可以在測試用例中使用此命令調用AutoIT腳本。

// Call to AutoIT script to upload the image 

Runtime.getRuntime().exec("C:\\xxxxx\\src\\test\\resources\\uploadImage.exe"); 

AutoIt的以股代息

ControlFocus("Open","","Edit1") 
    ControlSetText("Open","","Edit1","C:\xxxx\Images\jpgImage") 
    ControlClick("Open","","Button1") 

欲瞭解更多信息,請參考以下鏈接 https://www.guru99.com/use-autoit-selenium.html