public async Task AddScriptAsync() { var resources = Mocks.ResourceProvider("http://localhost/script.js", "console.log('in new script');") .Resource("http://localhost", "<html><head></head></html>"); var console = new TestingConsole(); //todo: rewrite it with JS. current test is not stable due to multithreading. var engine = TestingEngine.BuildJint(resources, console); var page = await engine.OpenUrl("http://localhost"); page.Document.AddEventListener("DOMNodeInserted", @event => { console.Log("nodeadded"); }, false); var onloadSignal = new ManualResetEvent(false); var d = (HtmlScriptElement)page.Document.CreateElement("script"); d.Id = "aaa"; d.Async = true; d.Src = "http://localhost/script.js"; d.OnLoad += e => { console.Log("onload"); onloadSignal.Set(); }; page.Document.Head.AppendChild(d); onloadSignal.WaitOne(10000); CollectionAssert.AreEqual(new[] { "nodeadded", "in new script", "onload" }, console.LogHistory); }
public static async Task CancelSubmitFromWindow(string returnValue, string log) { var html = @"<form><button id=b>clickme</button></form>"; var js = "window.onsubmit=function(){console.log('submit')};" + $"window.onclick=function(){{console.log('click'); return {returnValue};}};"; var httpResources = Mocks.HttpResourceProvider() .Resource("http://site.net/", $"{html}<script>{js}</script>"); var console = new TestingConsole(); var engine = TestingEngine.BuildJint(httpResources, console); var page = await engine.OpenUrl("http://site.net"); engine.OnUriChanged += () => console.Log("reload"); var button = page.Document.Get <HtmlButtonElement>("#b").First(); button.Click(); await Task.Delay(1000); Assert.AreEqual(log, string.Join(",", console.LogHistory)); }
public static async Task EventsOrder2() { var html = @"<form id=f><button id=b>clickme</button></form>"; var js = @"var b = document.getElementById('b'); b.addEventListener( 'click', function(){console.log('b.click')}, false ); b.addEventListener( 'click', function(){console.log('b.click bubble')}, true ); b.addEventListener( 'subscribe', function(){console.log('b.subscribe')}, false ); var f = document.getElementById('f'); f.addEventListener( 'click', function(){console.log('f.click')}, false ); f.addEventListener( 'click', function(){console.log('f.click bubble')}, true ); f.addEventListener( 'subscribe', function(){console.log('f.subscribe')}, false ); document.addEventListener( 'click', function(){console.log('d.click')}, false ); document.addEventListener( 'click', function(){console.log('d.click bubble')}, true ); document.addEventListener( 'subscribe', function(){console.log('d.subscribe')}, false );"; var httpResources = Mocks.HttpResourceProvider() .Resource("http://site.net/", $"{html}<script>{js}</script>"); var console = new TestingConsole(); var engine = TestingEngine.BuildJint(httpResources, console); var page = await engine.OpenUrl("http://site.net"); engine.OnUriChanged += () => console.Log("reloaded"); var doc = page.Document; var button = doc.Get <HtmlButtonElement>("#b").First(); button.Click(); await Task.Delay(1000); Assert.AreEqual(new[] { "d.click bubble", "f.click bubble", "b.click", "b.click bubble", "f.click", "d.click", "reloaded" }, console.LogHistory); }