RusKnyaz / Optimus

Optimus is headless Web Browser fully implemented on .net.
MIT License
82 stars 8 forks source link

Issues with real sample #10

Open MihaMarkic opened 5 years ago

MihaMarkic commented 5 years ago

Using 2.3.0-alpha.39 version and a .NET Core 2.2 console app.

Below is a real sample where I want to load a page (works), await for certain div with id 33-303-tab-2 (works), click on it (works often) and finally wait for an element id 33-303-container to have a child that is not HtmlImageElement.

My problems:

  1. I'm at loss what Document instance to use when. I have two Console.WriteLines where I compare references of different Document instances and all are different. Shouldn't be there a single instance?
  2. When searching for 33-303-container after the Click() I find it if I use tag.OwnerDocument, but not with engine.Document.
  3. Sometimes app deadlocks on tag.Click() method in EventTarget.NotifyListeners method.
  4. Often code deadlocks on lock(doc);
  5. I can never get to 33-303-container having not only a single Img element (the tag.Click() should inject a p node instead when done).

    class Program
    {
        static async Task Main(string[] args)
        {
            const string address = "http://www.kzs.si/clanek/Tekmovanja/Pokal-Spar/cid/72";
            var engine = new Knyaz.Optimus.Engine();
    
            engine.ScriptExecutor.Execute("text/javascript", "function escape(str){return encodeURI(str)};");
            var page = await engine.OpenUrl(address, false);
            Console.WriteLine("Waiting for root");
            var origDoc = engine.Document;
            var tag = (HtmlElement)engine.WaitId("33-303-tab-2");
            Console.WriteLine("Clicking");
            tag.Click();
            Console.WriteLine("Clicked");
            Console.WriteLine($"Documents are equal {ReferenceEquals(engine.Document, origDoc)}");
            Console.WriteLine($"Documents are equal {ReferenceEquals(engine.Document, tag.OwnerDocument)}");
            var elementFromEngine = (HtmlElement)engine.Document.WaitId("33-303-container");
            Console.WriteLine($"Awaited container from engine {!(elementFromEngine is null)}");
            var elementFromTag = (HtmlElement)tag.OwnerDocument.WaitId("33-303-container");
            Console.WriteLine($"Awaited container from tag {!(elementFromTag is null)}");
            Console.WriteLine($"State is {engine.Document.ReadyState}");
            //Console.WriteLine($"State is {engine.Document.ReadyState}");
            engine.WaitDocumentLoad();
            Console.WriteLine("Document loaded");
            for (int i = 0; i < 100; i++)
            {
                var doc = engine.Document;
                //var doc = t2.OwnerDocument;
                lock(doc)
                {
                    Console.WriteLine($"Iteration {i}");
                    var x = doc.GetElementById("33-303-container");
                    var content = doc.InnerHTML;
                    if (x.ChildNodes.Count > 0 && !(x.FirstChild is HtmlImageElement))
                    {
                        break;
                    }
                }
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }
            Console.WriteLine("Done");
        }
    }