1

Katalon 自定义“uploadFiles”关键字不加载文件。

Katalon 网站声明 Katalon 关键字“ UploadFile在页面上存在标签时才有效(我的测试页面上不存在该标签)。

建议的解决方法是使用以下自定义关键字;我已添加到我的项目中。当我运行脚本时,“uploadFile”关键字会找到对象并打开“Windows 资源管理器”,然后就坐在那里。我认为问题在于“uploadFile”关键字从不将“文件”传递给“Windows Explorer”进行加载。Katalon 通过了这一步,因为它找到了对象。不知道为什么“关键字”不起作用......谁能看到我哪里出错了?

Package=tools.uploadFiles
//Added for uploadFiles keyword
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent

public class uploadFiles {
@Keyword
def uploadFile (TestObject to, String filePath) {
    WebUI.click(to)
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, 
    null);
            Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    }
}

//Added the following lines to the script's "import" header section
import com.kms.katalon.core.annotation.Keyword as Keyword
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

//Script test steps
WebUI.openBrowser('')
WebUI.navigateToUrl(GlobalVariable.testURL)
WebUI.maximizeWindow()
WebUI.delay(GlobalVariable.shortDelay)

调用关键字的行:CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('FILE_UPLOAD/Page_VirtualWealth/UploadFile0'),'C:\\Users\\eversda\\Downloads\\TestFiles\\BlankCheque.png')

Windows 资源管理器打开时没有文件名

谢谢戴夫

4

1 回答 1

1

“uploadFiles”现在可以在一些故障排除后工作

Keywords.tools.uploadFiles.groovy
//Added for use with "uploadFiles"
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent

public class uploadFiles {
@Keyword
def uploadFile (TestObject to, String filePath) {
    WebUI.click(to)
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    Robot robot = new Robot();      
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.delay(1000)
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.delay(1000)
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);        
    }
}

注意:确保使用正确的对象定位器,在我的情况下,“label_upload fa fa-upload fa-l”的情况如下

// [@class = 'upload fa fa-upload fa-lg pull-right'][count(. | // [@for = 'chooseFile0']) = count(//*[@for = 'chooseFile0'] )]

脚本头...

import com.kms.katalon.core.annotation.Keyword as Keyword
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

脚本片段...

WebUI.delay(GlobalVariable.shortDelay) WebUI.comment('不要在脚本运行时点击进入 Katalon。这样做会导致 uploadFiles 无法工作。')

CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('FILE_UPLOAD/Page_VirtualWealth/label_upload fa-upload fa-l'), 'C:\Users\jdoe\Downloads\TestFiles\TestLicense.jpg') Thread.sleep(2000 ) //等待“打开”对话

WebUI.switchToWindowIndex('0')

CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('FILE_UPLOAD/Page_VirtualWealth/label_upload fa-upload fa-l2'), 'C:\Users\jdoe\Downloads\TestFiles\TestPassport.png') Thread.sleep(2000 ) //等待“打开”对话

WebUI.switchToWindowIndex(0)

CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('FILE_UPLOAD/Page_VirtualWealth/label_upload fa-upload fa-l_1'), 'C:\Users\jdoe\Downloads\TestFiles\Bank.pdf') Thread.sleep(2000 ) //等待“打开”对话

WebUI.switchToWindowIndex(0)

CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('FILE_UPLOAD/Page_VirtualWealth/label_upload fa-upload fa-l_12'), 'C:\Users\jdoe\Downloads\TestFiles\Bill.pdf') Thread.sleep(2000 ) //等待“打开”对话

WebUI.switchToWindowIndex(0) ...

于 2018-01-24T17:37:40.090 回答