Giter Site home page Giter Site logo

Comments (4)

github-actions avatar github-actions commented on September 13, 2024

@joerg1985, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

from selenium.

pujagani avatar pujagani commented on September 13, 2024

Thank you for raising this. Do you have an example/test that can help reproduce this?

from selenium.

joerg1985 avatar joerg1985 commented on September 13, 2024

@pujagani i can only provide some code to show this without a browser, see below.

As soon as the delay between sending network.beforeRequestSent and network.responseStarted is high enought everything works as expected. As soon as there are alot of events raised they get out of order, i guess at the point when one thread is not able to consume them sequentially.

I think it should be possible to have this in a real browser as described in the issue.


import java.io.UncheckedIOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.module.Network;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Message;
import org.openqa.selenium.remote.http.TextMessage;
import org.openqa.selenium.remote.http.WebSocket;

public class Connection {

    private static WebSocket.Listener LISTENER;

    public static void main(String[] args) throws InterruptedException {
        int delay = 1; // << increase this to e.g. 40ms to see the code is running fine in case there is a delay between the events
        
        HttpClient client = new HttpClient() {
            @Override
            public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
                LISTENER = listener;
                AtomicLong state = new AtomicLong();

                return new WebSocket() {

                    @Override
                    public WebSocket send(Message message) {
                        long id = state.incrementAndGet();
                        
                        if (id < 4) {
                            // answer the register calls
                            LISTENER.accept(new TextMessage(
                                    "{\"id\": " + id + ", \"result\": {}, \"type\": \"success\"}"
                            ));
                        }  
                        
                        return this;
                    }

                    @Override
                    public void close() {

                    }

                };
            }

            @Override
            public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };

        org.openqa.selenium.bidi.Connection connection = new org.openqa.selenium.bidi.Connection(client, "N/A");

        ConcurrentHashMap<String, Boolean> requests = new ConcurrentHashMap<>();

        Network network = new Network(new MockDriver(new BiDi(connection)));


        network.onBeforeRequestSent((before) -> {
            requests.put(before.getRequest().getRequestId(), Boolean.TRUE);
        });

        network.onResponseStarted((started) -> {
            if (requests.putIfAbsent(started.getRequest().getRequestId(), Boolean.FALSE) == null) {
                System.err.println(started.getRequest().getRequestId() + " NOT found in map");
                System.exit(666);
            }
        });

        // wait to ensure all listeners are in place
        Thread.sleep(2000);

        for (long i = 4; i < Long.MAX_VALUE; i++) {
            if (i % 100 == 0) {
                System.out.println("iteration: " + i);
            }
            
            LISTENER.accept(new TextMessage("{\"method\": \"network.beforeRequestSent\", \"params\": {"
                    + "\"context\": null,"
                    + "\"isBlocked\": true,"
                    + "\"navigation\": null,"
                    + "\"redirectCount\": 0,"
                    + "\"request\": {"
                    + "\"request\": \"" + i + "\","
                    + "\"url\": \"https://url\","
                    + "\"method\": \"GET\","
                    + "\"headers\": [],"
                    + "\"cookies\": [],"
                    + "\"headersSize\": 0,"
                    + "\"bodySize\": null"
                    + "},"
                    + "\"timestamp\": " + System.currentTimeMillis()+ ", "
                    + "\"initiator\": {\"type\" :\"other\"}"
                    + "}}"));
            
            if (delay > 0)
                Thread.sleep(delay);
            
            LISTENER.accept(new TextMessage("{\"method\": \"network.responseStarted\", \"params\": {"
                    + "\"context\": null,"
                    + "\"isBlocked\": true,"
                    + "\"navigation\": null,"
                    + "\"redirectCount\": 0,"
                    + "\"request\": {"
                    + "\"request\": \"" + i + "\","
                    + "\"url\": \"https://url\","
                    + "\"method\": \"GET\","
                    + "\"headers\": [],"
                    + "\"cookies\": [],"
                    + "\"headersSize\": 0,"
                    + "\"bodySize\": null"
                    + "},"
                    + "\"timestamp\": " + System.currentTimeMillis() + ", "
                    + "\"response\": {}"
                    + "}}"));
        }

    }

    private static class MockDriver implements WebDriver, HasBiDi {

        private final BiDi _bidi;

        public MockDriver(BiDi bidi) {
            _bidi = bidi;
        }

        @Override
        public void get(String url) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getCurrentUrl() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getTitle() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public List<WebElement> findElements(By by) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public WebElement findElement(By by) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getPageSource() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void close() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void quit() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Set<String> getWindowHandles() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getWindowHandle() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public TargetLocator switchTo() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Navigation navigate() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Options manage() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Optional<BiDi> maybeGetBiDi() {
            return Optional.of(_bidi);
        }

    }
}

from selenium.

pujagani avatar pujagani commented on September 13, 2024

Thank you so much for sharing this!

from selenium.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.