2013-05-28 46 views

回答

2

經過一些試驗和錯誤,我已經達到了以下解決方案。此配置用於Junit測試類

private URI siteBase; 
private static PhantomJSDriverService service; 
private WebDriver driver; 
protected static DesiredCapabilities dCaps; 

@BeforeClass 
public static void createAndStartService() throws IOException { 
    service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(new File("/path/to/phantom/driver")) 
      .usingAnyFreePort() 
      .build(); 
    service.start(); 
} 
@AfterClass 
public static void stopService() throws IOException { 
    service.stop(); 
} 

@Before 
public void setUp() throws Exception { 
     siteBase = new URI("http://localhost:8080/"); 
     dCaps = new DesiredCapabilities(); 
     dCaps.setJavascriptEnabled(true); 
     dCaps.setCapability("takesScreenshot", false); 

     driver = new RemoteWebDriver(service.getUrl(),dCaps); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
} 

@After 
public void tearDown() throws Exception { 
    driver.quit(); 
} 

如果您需要更多信息,請在下面留言。

+0

這是如何在Spring MVC控制器中使用的? –

+1

不確定你的意思。這使我可以調用外部(phantomjs)測試驅動程序,並對我在春季製作的Web應用程序進行功能測試。我認爲,如何使用Node.js或Ruby與測試框架(如水豚)更好地實現這一點。在練習中我學到了通過JUnit運行phantomjs槽可以非常有效。做測試需要更長時間。 –

+0

你能否看看我提出的這個問題:http://stackoverflow.com/questions/21949689/phantomjs-to-screenshot-website-div-for-spring-mvc-tomcat-and-itext-use?lq=1 ?我想做我的網頁的屏幕截圖,PhantomJS被Java視爲視圖。我同意,它需要更長的時間做:( –

相關問題