Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload TestBenchElement sometimes fails to upload and times out #1802

Open
jorgheymans opened this issue Jun 10, 2024 · 0 comments
Open

Upload TestBenchElement sometimes fails to upload and times out #1802

jorgheymans opened this issue Jun 10, 2024 · 0 comments

Comments

@jorgheymans
Copy link

Using the UploadElement.class, we're seeing that it fails seemingly randomly.

      $(UploadElement.class)
          .id("id")
          .upload(new File("src/test/resources/attachments/test.pdf").getAbsoluteFile()); 

When it fails, it's because of org.openqa.selenium.ScriptTimeoutException: script timeout.

	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
	at org.openqa.selenium.remote.RemoteWebDriver.executeAsyncScript(RemoteWebDriver.java:471)
	at com.vaadin.testbench.TestBenchDriverProxy.executeAsyncScript(TestBenchDriverProxy.java:196)
	at com.vaadin.testbench.TestBenchDriverProxy_$$_jvstdb6_0._d7executeAsyncScript(TestBenchDriverProxy_$$_jvstdb6_0.java)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at com.vaadin.testbench.DriverInvocationHandler.invoke(DriverInvocationHandler.java:47)
	at com.vaadin.testbench.TestBenchDriverProxy_$$_jvstdb6_0.executeAsyncScript(TestBenchDriverProxy_$$_jvstdb6_0.java)
	at com.vaadin.flow.component.upload.testbench.UploadElement.waitForUploads(UploadElement.java:147)
	at com.vaadin.flow.component.upload.testbench.UploadElement.upload(UploadElement.java:94)
	at com.vaadin.flow.component.upload.testbench.UploadElement.upload(UploadElement.java:54)
	

The timeout originates in this method:

    private void waitForUploads(int maxSeconds) {
        Timeouts timeouts = getDriver().manage().timeouts();
        timeouts.setScriptTimeout(maxSeconds, TimeUnit.SECONDS);

        String script = "var callback = arguments[arguments.length - 1];"
                + "var upload = arguments[0];"
                + "window.setTimeout(function() {"
                + "  var inProgress = upload.files.filter(function(file) { return file.uploading;}).length >0;"
                + "  if (!inProgress) callback();" //
                + "}, 500);";
        getCommandExecutor().getDriver().executeAsyncScript(script, this);

    }

AI overlord says that above code has a potential bug in it: one logical issue is usage of setTimeout. It only checks for in-progress uploads once after 500ms. If upload takes longer, your callback won't be executed. Use a polling technique instead

and then proposes this:

String script = "var callback = arguments[arguments.length - 1];"
                + "var upload = arguments[0];"
                + "(function poll() {"
                + "   var inProgress = upload.files.filter(function(file) { return file.uploading;}).length > 0;"
                + "   if (!inProgress) callback();"
                + "   else setTimeout(poll, 500);"
                + "})()";

Does this make any sense at all, or should the source of timeout be investigated somewhere else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant