【问题标题】:gradle task and imports on groovy filegroovy 文件上的 gradle 任务和导入
【发布时间】:2016-09-01 19:09:13
【问题描述】:

我有以下buil.gradle

apply plugin: "groovy"

repositories {
    mavenCentral()
        jcenter()
}

dependencies {
    groovy group: "org.codehaus.groovy", name:"groovy-all", version: "1.8.6"
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.53.0'
    compile "org.testng:testng:6.3.1"
    compile group: 'com.jcraft', name: 'jsch', version: '0.1.53'
    compile group: 'net.schmizz', name: 'sshj', version: '0.3.1'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
    compile group: 'net.sf.expectit', name: 'expectit-core', version: '0.8.1'
    compile group: 'net.sf.expectit', name: 'expectit-ant', version: '0.8.1'
    compile group: 'net.sf.expectit', name: 'expectit-parent', version: '0.8.1', ext: 'pom'
    compile "log4j:log4j:1.2.17"
    testCompile "org.spockframework:spock-core:0.7-groovy-1.8"
    testCompile "junit:junit:4.10"
    testCompile "cglib:cglib-nodep:2.2.2"
    testCompile "org.objenesis:objenesis:1.2"
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

sourceSets {
    test { groovy { 
        srcDir 'foo/bar/'
    } }
}

buildscript {
    repositories { 
           mavenCentral() 
           jcenter() 
        }
}

task runA << {
    new GroovyShell().run(file('foo/bar/ATest.groovy'));
}

如果我运行 gradle clean -Dtest.single=A test 它可以工作并且测试运行成功但是如果我运行 gradle -q runA 它会显示无法识别的导入,例如:

foo/bar/ATest.groovy: 16: unable to resolve class net.schmizz.sshj.SSHClient
   @ line 16, column 1.
     import net.schmizz.sshj.SSHClient;
     ^
  foo/bar/ATest.groovy: 28: unable to resolve class org.openqa.selenium.firefox.FirefoxDriver
   @ line 28, column 1.
     import org.openqa.selenium.firefox.FirefoxDriver;
     ^

这是我的 ATest.groovy

package foo.bar;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets;
import java.nio.file.Files
import java.nio.file.Paths;
import java.security.PublicKey

import org.apache.log4j.Logger;
import org.junit.Before
import org.junit.After
import org.junit.Test;
import org.junit.*;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.verification.HostKeyVerifier;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import static net.sf.expectit.filter.Filters.removeColors;
import static net.sf.expectit.filter.Filters.removeNonPrintable;
import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.regexp;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select

import java.util.regex.Pattern

import javax.naming.directory.InvalidAttributesException;

import java.util.concurrent.TimeUnit
import org.apache.log4j.Logger;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class ATest{
    private WebDriver driver;
    private static final Logger logger = Logger.getLogger(ATest.class);
    Properties props = new Properties();
    private StringBuffer verificationErrors = new StringBuffer();

    private Expect expect = null;
    Session session = null;
    SSHClient ssh = null;

    @Before
    public void setUp() throws Exception {
        logger.info("========================================================================");
        logger.info("Starting...");

        // setting up Webdriver instance
        try {
            driver = new FirefoxDriver();           
        }
        catch (Exception e) {
            logger.info("Could not open Firefox instance: " + e.getMessage());
            fail("Could not open Firefox instance: " + e.getMessage());
        }

        // setting up SSH connection
        try {
            ssh = new SSHClient();
            ssh.addHostKeyVerifier(
                    new HostKeyVerifier() {
                        @Override
                        public boolean verify(String s, int i, PublicKey publicKey) {
                            return true;
                        }
                    });

            ssh.connect(props.getProperty("host"), Integer.parseInt(props.getProperty("port")));
            ssh.authPassword(props.getProperty("user"), props.getProperty("password"));
            session = ssh.startSession();
            session.allocateDefaultPTY();
        } catch (Exception e) {
            fail("Could not open SSH connection with " + props.getProperty("host") + "\nError: " + e.getMessage());
        }

        // start a interactive remote shell
        Shell shell = session.startShell();
        expect = new ExpectBuilder().withOutput(shell.getOutputStream())
                .withInputs(shell.getInputStream(), shell.getErrorStream())
                //.withEchoInput(System.out)
                .withInputFilters(removeColors(), removeNonPrintable())
                .withExceptionOnFailure()
                .build();       
        try {
            Thread.sleep(3000);
            // log as 'su' user in the remote shell
            expect.sendLine(props.getProperty("suuser"));
            expect.expect(contains("Password: "));
            expect.sendLine(props.getProperty("supassword"));
            expect.expect(regexp("root@"));
        }
        catch (Exception e) {
            fail("Error: " + e.getMessage());
        }
    }

    @Test
    public void run() {
        ...
    }

    @After
    public void tearDown() throws Exception {
        if (driver != null) {
            driver.quit();
        }
    }
}

我缺少一些配置吗?

【问题讨论】:

    标签: gradle groovy build.gradle


    【解决方案1】:

    这是预期的行为。当您使用 gradle 运行测试时,配置类路径、类加载器是 gradle 的工作 - 即整个环境。

    如果您运行该文件,则在您的脚本上提供配置是您的工作。

    看看GroovyShells、constructor summary。它需要一个ClassLoader 对象和一个CompileConfiguration。提供这些对象是您的工作。

    【讨论】:

    • 谢谢,这个解决方案可以解决我的问题。 Solution
    • @uonr,太好了!如果你觉得我的回答有点用,请至少点赞 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2019-03-02
    • 2016-07-29
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多