コード例 #1
0
ファイル: CSSCoverage.cs プロジェクト: APMann/puppeteer-sharp
        internal async Task <CoverageEntry[]> StopAsync()
        {
            if (!_enabled)
            {
                throw new InvalidOperationException("CSSCoverage is not enabled");
            }
            _enabled = false;

            var ruleTrackingResponseTask = _client.SendAsync <CSSStopRuleUsageTrackingResponse>("CSS.stopRuleUsageTracking");
            await Task.WhenAll(
                ruleTrackingResponseTask,
                _client.SendAsync("CSS.disable"),
                _client.SendAsync("DOM.disable")
                ).ConfigureAwait(false);

            _client.MessageReceived -= client_MessageReceived;

            var styleSheetIdToCoverage = new Dictionary <string, List <CoverageResponseRange> >();

            foreach (var entry in ruleTrackingResponseTask.Result.RuleUsage)
            {
                styleSheetIdToCoverage.TryGetValue(entry.StyleSheetId, out var ranges);
                if (ranges == null)
                {
                    ranges = new List <CoverageResponseRange>();
                    styleSheetIdToCoverage[entry.StyleSheetId] = ranges;
                }
                ranges.Add(new CoverageResponseRange
                {
                    StartOffset = entry.StartOffset,
                    EndOffset   = entry.EndOffset,
                    Count       = entry.Used ? 1 : 0,
                });
            }

            var coverage = new List <CoverageEntry>();

            foreach (var styleSheetId in _stylesheetURLs.Keys)
            {
                var url  = _stylesheetURLs[styleSheetId];
                var text = _stylesheetSources[styleSheetId];
                styleSheetIdToCoverage.TryGetValue(styleSheetId, out var responseRanges);
                var ranges = Coverage.ConvertToDisjointRanges(responseRanges ?? new List <CoverageResponseRange>());
                coverage.Add(new CoverageEntry
                {
                    Url    = url,
                    Ranges = ranges,
                    Text   = text
                });
            }
            return(coverage.ToArray());
        }
コード例 #2
0
        internal async Task <CoverageEntry[]> StopAsync()
        {
            if (!_enabled)
            {
                throw new InvalidOperationException("JSCoverage is not enabled");
            }
            _enabled = false;

            var profileResponseTask = _client.SendAsync <ProfilerTakePreciseCoverageResponse>("Profiler.takePreciseCoverage");
            await Task.WhenAll(
                profileResponseTask,
                _client.SendAsync("Profiler.stopPreciseCoverage"),
                _client.SendAsync("Profiler.disable"),
                _client.SendAsync("Debugger.disable")
                ).ConfigureAwait(false);

            _client.MessageReceived -= Client_MessageReceived;

            var coverage = new List <CoverageEntry>();

            foreach (var entry in profileResponseTask.Result.Result)
            {
                _scriptURLs.TryGetValue(entry.ScriptId, out var url);
                if (string.IsNullOrEmpty(url) && _reportAnonymousScripts)
                {
                    url = "debugger://VM" + entry.ScriptId;
                }
                if (string.IsNullOrEmpty(url) ||
                    !_scriptSources.TryGetValue(entry.ScriptId, out var text))
                {
                    continue;
                }

                var flattenRanges = entry.Functions.SelectMany(f => f.Ranges).ToList();
                var ranges        = Coverage.ConvertToDisjointRanges(flattenRanges);
                coverage.Add(new CoverageEntry
                {
                    Url    = url,
                    Ranges = ranges,
                    Text   = text
                });
            }
            return(coverage.ToArray());
        }