我一直在努力寻找最长的时间,但我无法让它发挥作用。 我要做的是用Fluentlenium作为基础进行负载测试。 当我尝试启动测试时,它只打开一个chrome浏览器,在我的IDE中,同样的方法运行3x(例如)有时测试通过,有时会失败。它假设在不同的端口(由selenium生成)下启动3个chrome浏览器,但它没有,它只启动1个浏览器。
这是我的代码失败的原因:
public class Example extends FluentTestNg {
private final static String DEFAULT_URL = "http://google.com";
@Override
public WebDriver newWebDriver() {
DesiredCapabilities caps = new DesiredCapabilities();
WebDriver driver = new ChromeDriver(caps);
return driver;
}
@Test(invocationCount = 3, threadPoolSize = 3)
public void GoogleTest() throws Exception {
goTo(DEFAULT_URL);
await().until($(".gsfi")).present();
System.out.println(getCookies());
}
}
提供失败的堆栈跟踪
Starting ChromeDriver 2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262) on port 14851
Only local connections are allowed.
Jun 19, 2017 12:53:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Jun 19, 2017 12:53:03 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://localhost:14851: Broken pipe (Write failed)
Jun 19, 2017 12:53:03 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://localhost:14851
org.openqa.selenium.NoSuchSessionException: no such session
(Driver info: chromedriver=2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262),platform=Mac OS X 10.12.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 59 milliseconds
Build info: version: '3.3.1', revision: '5234b325d5', time: '2017-03-10 09:10:29 +0000'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262), userDataDir=/var/folders/1j/tdgn9dcn6d9g9dcd3fs8nj8c0000gn/T/.org.chromium.Chromium.yRHaZe}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.86, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 25c29b733c90f9dff2afb69dee6dc1d5
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.isEnabled(RemoteWebElement.java:148)
at org.fluentlenium.core.proxy.ComponentHandler.isStale(ComponentHandler.java:52)
at org.fluentlenium.core.proxy.AbstractLocatorHandler.present(AbstractLocatorHandler.java:225)
at org.fluentlenium.core.proxy.LocatorProxies.present(LocatorProxies.java:113)
at org.fluentlenium.core.domain.FluentWebElement.present(FluentWebElement.java:132)
at org.fluentlenium.core.conditions.AbstractObjectConditions.verify(AbstractObjectConditions.java:36)
at org.fluentlenium.core.conditions.WebElementConditions.present(WebElementConditions.java:43)
at org.fluentlenium.core.conditions.AbstractFluentListConditions.lambda$present$0(AbstractFluentListConditions.java:79)
at org.fluentlenium.core.conditions.AtLeastOneElementConditions.lambda$buildAtLeastOnePredicate$0(AtLeastOneElementConditions.java:49)
at org.fluentlenium.core.conditions.AtLeastOneElementConditions.verify(AtLeastOneElementConditions.java:35)
at org.fluentlenium.core.conditions.AbstractFluentListConditions.present(AbstractFluentListConditions.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler$2.apply(WaitConditionInvocationHandler.java:216)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler$2.apply(WaitConditionInvocationHandler.java:212)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.lambda$until$0(WaitConditionInvocationHandler.java:156)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
at org.fluentlenium.core.wait.FluentWait.untilPredicate(FluentWait.java:131)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.until(WaitConditionInvocationHandler.java:144)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.until(WaitConditionInvocationHandler.java:175)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.waitForCondition(WaitConditionInvocationHandler.java:212)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.invoke(WaitConditionInvocationHandler.java:190)
at com.sun.proxy.$Proxy8.present(Unknown Source)
at Example.GoogleTest(Example.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
org.openqa.selenium.TimeoutException: Expected condition failed: Elements By.cssSelector: .gsfi (Lazy Element List) is not present (tried for 5 second(s) with 500 MILLISECONDS interval)
Build info: version: '3.3.1', revision: '5234b325d5', time: '2017-03-10 09:10:29 +0000'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:264)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:232)
at org.fluentlenium.core.wait.FluentWait.untilPredicate(FluentWait.java:131)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.until(WaitConditionInvocationHandler.java:144)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.until(WaitConditionInvocationHandler.java:175)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.waitForCondition(WaitConditionInvocationHandler.java:212)
at org.fluentlenium.core.conditions.wait.WaitConditionInvocationHandler.invoke(WaitConditionInvocationHandler.java:190)
at com.sun.proxy.$Proxy8.present(Unknown Source)
at Example.GoogleTest(Example.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
我有另一种方法可以正常工作,但我必须传递newWebDriver方法才能正常工作......
public class Example extends FluentTestNg {
private final static String DEFAULT_URL = "http://google.com";
private static ThreadLocal<WebDriver> drivers = new ThreadLocal<WebDriver>();
public void setWebdriver(WebDriver driver){
drivers.set(driver);
}
@Override
public WebDriver newWebDriver() {
return drivers.get ();
}
@BeforeMethod
public void launchbrowser(){
WebDriver drivers = new ChromeDriver();
setWebdriver(drivers);
}
@Test(invocationCount = 3, threadPoolSize = 3)
public void GoogleTest() throws Exception {
newWebDriver().get(DEFAULT_URL);
}
}
继续使用3个浏览器打开它的控制台日志,但我不能使用Fluentlenium语法。
Starting ChromeDriver 2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262) on port 11700
Only local connections are allowed.
Starting ChromeDriver 2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262) on port 35621
Only local connections are allowed.
Starting ChromeDriver 2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262) on port 31113
Only local connections are allowed.
Jun 19, 2017 12:59:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Jun 19, 2017 12:59:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Jun 19, 2017 12:59:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
唯一的问题是,我不能使用内置的Fluentlenium语法(exp:GoTo,GetCookies,Await等)。我必须使用Selenium语法。
有没有办法让这个只用于Fluentlenium?
答案 0 :(得分:0)
您可以尝试摆脱ThreadLocal并使WebDriver不是静态的。或者使用您当前的代码,可能很简单,只需确保get
字段的ThreadLocal
方法为static
:
public static WebDriver newWebDriver() {
return drivers.get ();
}
我注意到你在某个地方压倒了这个newWebDriver
方法,我不完全清楚你是否真的想要在这里做什么,因为我不知道它在哪里来自。