0

我使用本教程连接到 Gmail API:https ://developers.google.com/gmail/api/quickstart/java 我想在 Katalon Studio 中创建一个关键字,这取决于 Gmail API。我从示例代码修改了该行:

InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);

对此:

InputStream ins = new FileInputStream(CREDENTIALS_FILE_PATH);

已添加 JAR 文件,项目正在运行并打开浏览器窗口以获取令牌。成功授权后,我收到错误消息:

引起:java.lang.NoSuchMethodError: com.google.api.client.http.HttpRequest.setResponseReturnRawInputStream(Z)Lcom/google/api/client/http/HttpRequest;

更新:导入依赖项列表:

commons-codec-1.15.jar
commons-logging-1.2.jar
google-api-client-1.31.3.jar
google-api-client-extensions-1.6.0-beta.jar
google-api-client-jackson2-1.31.3.jar
google-api-client-java6-1.31.3.jar
google-api-services-gmail-v1-rev110-1.25.0.jar
google-http-client-1.39.1.jar
google-http-client-jackson2-1.39.1.jar
google-oauth-client-java6-1.31.4.jar
google-oauth-client-jetty-1.31.4.jar
guava-30.1.1-jre.jar
httpclient-4.5.13.jar
httpcore-4.4.14.jar
j2objc-annotations-1.3.jar
jackson-core-2.12.2.jar
jsr305-3.0.2.jar
4

1 回答 1

1

https://docs.katalon.com/katalon-studio/docs/external-libraries.html#exclude-built-in-libraries

通过删除存储在项目文件夹的 .classpath 文件中的内置库的能力,您可以将内置库替换为外部库,以便在测试项目中灵活使用库。

要求

  • 有效的 Katalon Studio Enterprise 许可证。
  • Katalon Studio 7.8 版。

升级版:

我得到了katalon 7.9.1,在这里我是如何做到的:

将以下类添加到 KS 项目中:

include/scripts/groovy/(默认包)/GroovyBox.java

import groovy.lang.*;
import java.util.regex.Pattern;
import java.util.Map;
import java.util.List;

/** run groovy script in isolated classloader*/
public class GroovyBox {
    GroovyShell gs;
    
    public GroovyBox(ClassLoader parentCL, Pattern excludeClassPattern ) {
        FilteredCL fcl = new FilteredCL(parentCL, excludeClassPattern);
        gs = new GroovyShell(fcl);
    }
    
    public GroovyBox withClassPath(List<String> classPathList) {
        GroovyClassLoader cl = gs.getClassLoader();
        for(String cp: classPathList) cl.addClasspath(cp);
        return this;
    }
    
    public Script parse(String scriptText) {
        return gs.parse(scriptText);
    }
    
    public static class FilteredCL extends GroovyClassLoader{
        Pattern filterOut;
        
        public FilteredCL(ClassLoader parent,Pattern excludeClassPattern){
            super(parent);
            filterOut = excludeClassPattern;
        }
        
        @Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException{
            if(filterOut.matcher(name).matches())throw new ClassNotFoundException("class not found "+ name);
            return super.loadClass(name, resolve);
        }
    }
}

现在添加一个测试用例 - 实际上你可以将代码从测试用例移动到一个类中......

import ... /* all katalon imports here*/

assert method1() == 'HELLO WORLD'

def method1() {
    def gb = new GroovyBox(this.getClass().getClassLoader().getParent(), ~/^com\.google\..*/)
    def script = gb.parse('''
            @Grab(group='com.google.api-client', module='google-api-client', version='1.31.3')
            import com.google.api.client.http.HttpRequest

            def c = HttpRequest.class
            println( "methods execute:: "+c.methods.findAll{it.name=='execute'} )
            println( "methods setResponseReturnRawInputStream:: "+c.methods.findAll{it.name=='setResponseReturnRawInputStream'} )
            println greeting
            return greeting.toUpperCase()
        ''')
    script.setBinding([greeting:'hello world'] as Binding)
    return script.run()
}

定义外部依赖项的选项:

  • @Grab(...)作为解析脚本的第一行 - 从 maven Central 加载所有必需的依赖项(默认情况下)。比如@Grab(group='com.google.api-client', module='google-api-client', version='1.31.3')对应这个神器
  • 有时您需要指定特定的 Maven 存储库,然后添加@GrabResolver(name='central', root='https://repo1.maven.org/maven2/')
  • 如果要指定本地文件依赖项,则在上面的代码中:
def gb = new GroovyBox(...).withClassPath([
    '/path/to/lib1.jar',
    '/path/to/lib2.jar'
])

在此处输入图像描述

于 2021-04-03T15:41:21.527 回答