我在PhantomJS 1.9.1和GhostDriver 1.0.4,JDK 1.7和Win 7 OS中运行基于Java的Selenium测试.
我也在使用BrowserMob代理,以便我可以捕获网络流量来验证一些网络呼叫.下面给出了我的代码的样子.
server = new ProxyServer(44444);
server.start();
final Proxy proxy = server.seleniumProxy();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
capabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { "--ignore-ssl-errors=yes" });
capabilities.setCapability("browserType", "phantomjs");
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, System.getenv("PHANTOMJS_EXECUTABLE_PATH_PROPERTY"));
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY, System.getenv("GHOST_DRIVER_HOME") + "\\main.js");
driver = new PhantomJSDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)
然后我执行我的测试.有时测试没有任何错误,有时我会遇到2个不同的错误.我不确定是什么原因造成的.
如果你可以帮助我,或者给我一些关于这里可能出错的线索.如果需要,我可以提供更具体的信息.
[ERROR - 2013-07-30T14:05:46.693Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1375193146688
[ERROR - 2013-07-30T14:05:46.713Z] RouterReqHand - _handle - Thrown => {
"message": "Error Message => …Run Code Online (Sandbox Code Playgroud) 我在尝试模拟 InetAddress 中的静态方法时遇到了奇怪的问题。我成功地能够模拟许多其他类的静态方法,并且一切正常,但 InetAddress 显示不同的行为。我正在使用 JUnit 4.x、Mockito 1.9.5 和 PowerMock 1.5.6。
下面使用 Mockito 和 PowerMock 进行测试,并且 InetAddress 模拟工作正常 -
@RunWith(PowerMockRunner.class)
@PrepareForTest({InetAddress.class})
public class UtilityTest {
@Mock
InetAddress inetAddress;
@Test
public void testGetCurrentHost() throws UnknownHostException {
mockStatic(InetAddress.class);
when(InetAddress.getLocalHost()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn("1.1.1.1");
assertEquals("1.1.1.1", getCurrentHost());
}
private static String getCurrentHost() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
}
Run Code Online (Sandbox Code Playgroud)
当我将下面给出的方法放入某个实用程序中时,InetAddress.getLocalHost() 不再模拟并且测试失败。
将其移至实用程序:
private static String getCurrentHost() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
Run Code Online (Sandbox Code Playgroud)
现在我的测试看起来像(并且失败):
@RunWith(PowerMockRunner.class)
@PrepareForTest({InetAddress.class})
public class UtilityTest {
@Mock
InetAddress inetAddress;
@Test
public void testGetCurrentHost() throws …Run Code Online (Sandbox Code Playgroud)