2016-09-01 43 views
0

进口我有以下buil.gradlegradle这个任务和常规文件

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 清洁-Dtest.single = A测试它的工作原理和测试成功运行,但如果我跑gradle这个-q鲁纳它显示无法识别的进口,如:

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("[email protected]")); 
     } 
     catch (Exception e) { 
      fail("Error: " + e.getMessage()); 
     } 
    } 

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

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

我是否缺少一些配置?

回答

1

这是预期的行为。当您使用gradle运行测试时,配置类路径classloader(即整个环境)是一项艰苦的工作。

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

看一看GroovyShell s,constructor summary。它需要一个ClassLoader对象和一个CompileConfiguration。提供这些对象是你的工作。

+0

谢谢,这个解决方案解决了我的问题。 [解决方案](http://stackoverflow.com/questions/13763112/running-groovy-script-from-gradle-using-groovyshell-exception-in-thread-main) – uonr

+0

@uonr,太棒了!如果你发现我的回答有用,请至少提升它;) – Opal