public void AfterEachTestTearDown() { _context.Dispose(); _gwb.Dispose(); }
public void AfterEachTestTearDown() { _context.Dispose(); _browser.Dispose(); }
/// <summary> /// 运行js代码 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="jsCode">一段js代码。如:return data.name;</param> /// <param name="data">传到js里面的对象,js可以通过data.*直接使用此参数</param> /// <returns></returns> public T Run <T>(string jsCode, object data) { if (httpServer == null) { throw new Exception("请先调用JsRunner.StartVirtualWebServer启动web服务器"); } var gecko = new GeckoWebBrowser(); gecko.CreateControl(); //加入apicloud.db模拟器 List <ISimulator> simulators = new List <ISimulator>(); foreach (var t in SimulatorTypes) { var obj = (ISimulator)Activator.CreateInstance(t); obj.Init(gecko); simulators.Add(obj); } bool loadFinished = false; gecko.NavigationError += (s, e) => { }; gecko.NSSError += (s, e) => { }; gecko.DocumentCompleted += (s, e) => { loadFinished = true; }; using (System.IO.MemoryStream ms = new MemoryStream()) { System.IO.StreamWriter sw = new StreamWriter(ms, Encoding.UTF8); sw.WriteLine("<!DOCTYPE html>"); sw.WriteLine("<html>"); sw.WriteLine("<body>"); foreach (var simulator in simulators) { simulator.OnWritingScript(sw); } foreach (var path in JsFiles) { if (path[1] == ':') { if (File.Exists(path) == false) { throw new Exception($"文件{path}不存在"); } sw.WriteLine("<script src=\"file:///" + path + "\" type=\"text/javascript\"></script>"); } else { sw.WriteLine("<script src=\"" + path + "\" type=\"text/javascript\"></script>"); } } if (!string.IsNullOrEmpty(this.Html)) { sw.WriteLine(this.Html); } string result; if (_runJsOnWindowLoad) { sw.WriteLine("</body>"); sw.WriteLine("</html>"); sw.Dispose(); var pageid = Guid.NewGuid().ToString("N"); VisitHandler.HtmlContents.TryAdd(pageid, ms.ToArray()); ms.Dispose(); gecko.Navigate($"{ServerUrl}Jack.JSUnitTest?id={pageid}"); while (!loadFinished) { System.Threading.Thread.Sleep(10); System.Windows.Forms.Application.DoEvents(); } var jsContext = new AutoJSContext(gecko.Window); var js = @" (function(d){ var result = (function(data){ try{ " + jsCode + @" } catch(e) { if(typeof e === 'string') return { ______err : e , line:0 }; else { var msg = e.message; if(e.name) msg = e.name + ',' + msg; return { ______err :msg , line:e.lineNumber }; } } })(d); return JSON.stringify(result); })(" + (data == null ? "null" : Newtonsoft.Json.JsonConvert.SerializeObject(data)) + @"); "; jsContext.EvaluateScript(js, out result); jsContext.Dispose(); } else { var guidName = "v" + Guid.NewGuid().ToString("N"); var js = @" <script lang='ja'> var " + guidName + @" = (function(data){ try{ " + jsCode + @" } catch(e) { if(typeof e === 'string') return { ______err : e , line:0 }; else { var msg = e.message; if(e.name) msg = e.name + ',' + msg; return { ______err :msg , line:e.lineNumber }; } } })(" + Newtonsoft.Json.JsonConvert.SerializeObject(data) + @"); " + guidName + @"=JSON.stringify(" + guidName + @"); </script> "; sw.WriteLine(js); sw.WriteLine("</body>"); sw.WriteLine("</html>"); sw.Dispose(); var pageid = Guid.NewGuid().ToString("N"); VisitHandler.HtmlContents.TryAdd(pageid, ms.ToArray()); ms.Dispose(); gecko.Navigate($"{ServerUrl}Jack.JSUnitTest?id={pageid}"); while (!loadFinished) { System.Threading.Thread.Sleep(10); System.Windows.Forms.Application.DoEvents(); } var jsContext = new AutoJSContext(gecko.Window); js = guidName; jsContext.EvaluateScript(js, out result); jsContext.Dispose(); } gecko.Dispose(); if (result.StartsWith("{\"______err\":")) { var errObj = Newtonsoft.Json.JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JObject>(result); string errMsg = errObj.Value <string>("______err"); //int lineNumber = errObj.Value<int>("line") - 3; throw new Exception(errMsg); } if (result.StartsWith("\"") == false && typeof(T) == typeof(string)) { return((T)(object)result); } if (result == "undefined") { return(default(T)); } return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(result)); } }