0

我一直在尝试最长时间,但我无法让它工作。我想做的是以 Fluentlenium 为基础进行负载测试。当我尝试启动我的测试时,它只打开一个 chrome 浏览器,在我的 IDE 中,相同的方法运行 3 次(例如)有时测试通过,有时它失败。它假设在不同的端口(由 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 语法(例如:GoTo、GetCookies、Await 等)。我必须使用 Selenium 语法。

有什么方法可以让它与 Fluentlenium 一起工作吗?

4

1 回答 1

0

您可以尝试摆脱 ThreadLocal 并使您的 WebDriver 不是静态的。或者使用您当前的代码,它可能就像确保您的领域的get方法一样简单:ThreadLocalstatic

public static WebDriver newWebDriver() {
    return drivers.get ();
}

我注意到你newWebDriver从某个地方覆盖了这个方法,我不完全清楚这是否真的是你想要在这里做的,因为我不知道它来自哪里。

于 2017-06-19T18:07:59.240 回答