2

如何在junit中使用远程phantomjs驱动器配置selenium?我一直在尝试为此寻找教程,但没有运气。我的目标是使用它在 spring mvc 中为我的单页应用程序进行测试。

4

1 回答 1

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();
}

如果您需要更多信息,请在下方评论。

于 2013-05-28T14:32:03.957 回答