How to use RuntimeCallFunctionOnRequest class of PuppeteerSharp.Messaging package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Messaging.RuntimeCallFunctionOnRequest

ExecutionContext.cs

Source:ExecutionContext.cs Github

copy

Full Screen

...126                ["awaitPromise"] = true,127                ["userGesture"] = true128            });129        private Task<RemoteObject> EvaluateFunctionInternalAsync(bool returnByValue, string script, params object[] args)130            => ExecuteEvaluationAsync("Runtime.callFunctionOn", new RuntimeCallFunctionOnRequest131            {132                FunctionDeclaration = $"{script}\n{_evaluationScriptSuffix}\n",133                ExecutionContextId = _contextId,134                Arguments = args.Select(FormatArgument),135                ReturnByValue = returnByValue,136                AwaitPromise = true,137                UserGesture = true138            });139        private async Task<RemoteObject> ExecuteEvaluationAsync(string method, object args)140        {141            try142            {143                var response = await _client.SendAsync<EvaluateHandleResponse>(method, args).ConfigureAwait(false);144                if (response.ExceptionDetails != null)145                {146                    throw new EvaluationFailedException("Evaluation failed: " +147                        GetExceptionMessage(response.ExceptionDetails));148                }149                return response.Result;150            }151            catch (MessageException ex)152            {153                if (ex.Message.Contains("Object reference chain is too long") ||154                    ex.Message.Contains("Object couldn't be returned by value"))155                {156                    return default;157                }158                throw new EvaluationFailedException(ex.Message, ex);159            }160        }161        internal JSHandle CreateJSHandle(RemoteObject remoteObject)162            => remoteObject.Subtype == RemoteObjectSubtype.Node && Frame != null163                ? new ElementHandle(this, _client, remoteObject)164                : new JSHandle(this, _client, remoteObject);165        private object FormatArgument(object arg)166        {167            switch (arg)168            {169                case BigInteger big:170                    return new { unserializableValue = $"{big}n" };171                case int integer when integer == -0:172                    return new { unserializableValue = "-0" };173                case double d:174                    if (double.IsPositiveInfinity(d))175                    {176                        return new { unserializableValue = "Infinity" };177                    }178                    if (double.IsNegativeInfinity(d))179                    {180                        return new { unserializableValue = "-Infinity" };181                    }182                    if (double.IsNaN(d))183                    {184                        return new { unserializableValue = "NaN" };185                    }186                    break;187                case JSHandle objectHandle:188                    return objectHandle.FormatArgument(this);189            }190            return new RuntimeCallFunctionOnRequestArgument191            {192                Value = arg193            };194        }195        private static string GetExceptionMessage(EvaluateExceptionResponseDetails exceptionDetails)196        {197            if (exceptionDetails.Exception != null)198            {199                return exceptionDetails.Exception.Description ?? exceptionDetails.Exception.Value;200            }201            var message = exceptionDetails.Text;202            if (exceptionDetails.StackTrace != null)203            {204                foreach (var callframe in exceptionDetails.StackTrace.CallFrames)...

Full Screen

Full Screen

JSHandle.cs

Source:JSHandle.cs Github

copy

Full Screen

...107        {108            var objectId = RemoteObject.ObjectId;109            if (objectId != null)110            {111                var response = await Client.SendAsync<RuntimeCallFunctionOnResponse>("Runtime.callFunctionOn", new RuntimeCallFunctionOnRequest112                {113                    FunctionDeclaration = "function() { return this; }",114                    ObjectId = objectId,115                    ReturnByValue = true,116                    AwaitPromise = true117                }).ConfigureAwait(false);118                return (T)RemoteObjectHelper.ValueFromRemoteObject<T>(response.Result);119            }120            return (T)RemoteObjectHelper.ValueFromRemoteObject<T>(RemoteObject);121        }122        /// <summary>123        /// Disposes the Handle. It will mark the JSHandle as disposed and release the <see cref="JSHandle.RemoteObject"/>124        /// </summary>125        /// <returns>The async.</returns>...

Full Screen

Full Screen

RuntimeCallFunctionOnRequest.cs

Source:RuntimeCallFunctionOnRequest.cs Github

copy

Full Screen

1using System.Collections.Generic;2using Newtonsoft.Json;3namespace PuppeteerSharp.Messaging4{5    internal class RuntimeCallFunctionOnRequest6    {7        public string FunctionDeclaration { get; set; }8        public int? ExecutionContextId { get; set; }9        public IEnumerable<object> Arguments { get; set; }10        public bool ReturnByValue { get; set; }11        public bool AwaitPromise { get; set; }12        public bool UserGesture { get; set; }13        public string ObjectId { get; set; }14    }15}...

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5    {6        static void Main(string[] args)7        {8            Console.WriteLine("Hello World!");9            MainAsync().Wait();10        }11        static async Task MainAsync()12        {13            {14                Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }15            };16            using (var browser = await Puppeteer.LaunchAsync(options))17            {18                using (var page = await browser.NewPageAsync())19                {20                    var response = await page.EvaluateFunctionHandleAsync("() => window");21                    var windowHandle = response as JSHandle;22                    {23                        FunctionDeclaration = "function() { return this; }",24                    };25                    var result = await page.Client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);26                    Console.WriteLine(result.Result.Value);27                }28            }29        }30    }31}

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using PuppeteerSharp.Messaging;3using System;4using System.Threading.Tasks;5{6    {7        public static Task<RuntimeCallFunctionOnResponse> RuntimeCallFunctionOn(this Page page, string function, object[] args)8        {9            {10            };11            return page.Client.SendAsync<RuntimeCallFunctionOnResponse>("Runtime.callFunctionOn", request);12        }13    }14}15using PuppeteerSharp;16using PuppeteerSharp.Messaging;17using System;18using System.Threading.Tasks;19{20    {21        public static Task<RuntimeCallFunctionOnResponse> RuntimeCallFunctionOn(this Page page, string function, object[] args)22        {23            {24            };25            return page.Client.SendAsync<RuntimeCallFunctionOnResponse>("Runtime.callFunctionOn", request);26        }27    }28}29using PuppeteerSharp;30using PuppeteerSharp.Messaging;31using System;32using System.Threading.Tasks;33{34    {35        public static Task<RuntimeCallFunctionOnResponse> RuntimeCallFunctionOn(this Page page, string function, object[] args)36        {37            {38            };39            return page.Client.SendAsync<RuntimeCallFunctionOnResponse>("Runtime.callFunctionOn", request);40        }41    }42}43using PuppeteerSharp;44using PuppeteerSharp.Messaging;45using System;46using System.Threading.Tasks;47{48    {49        public static Task<RuntimeCallFunctionOnResponse> RuntimeCallFunctionOn(this Page page, string function, object[] args)50        {

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1{2    FunctionDeclaration = "function() {return document.querySelector('h1').innerText;}",3};4var runtimeCallFunctionOnResponse = await client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);5var result = runtimeCallFunctionOnResponse.Result.Value.ToString();6Console.WriteLine(result);7{8    FunctionDeclaration = "function() {return document.querySelector('h1').innerText;}",9};10var runtimeCallFunctionOnResponse = await client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);11var result = runtimeCallFunctionOnResponse.Result.Value.ToString();12Console.WriteLine(result);13{14    FunctionDeclaration = "function() {return document.querySelector('h1').innerText;}",15};16var runtimeCallFunctionOnResponse = await client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);17var result = runtimeCallFunctionOnResponse.Result.Value.ToString();18Console.WriteLine(result);19{20    FunctionDeclaration = "function() {return document.querySelector('h1').innerText;}",21};22var runtimeCallFunctionOnResponse = await client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);23var result = runtimeCallFunctionOnResponse.Result.Value.ToString();24Console.WriteLine(result);25{26    FunctionDeclaration = "function() {return document.querySelector('h1').innerText;}",27};28var runtimeCallFunctionOnResponse = await client.Runtime.CallFunctionOnAsync(runtimeCallFunctionOnRequest);

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1var target = await browser.GetTargetAsync();2var client = await target.CreateCDPSessionAsync();3var request = new RuntimeCallFunctionOnRequest()4{5    FunctionDeclaration = "function() { return 1 + 1;}",6};7var response = await client.SendAsync(request);8Console.WriteLine(response.Result.Value);9var target = await browser.GetTargetAsync();10var client = await target.CreateCDPSessionAsync();11var response = await client.RuntimeCallFunctionOnAsync("function() { return 1 + 1;}");12Console.WriteLine(response.Result.Value);

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1var response = await client.Runtime.CallFunctionOnAsync(2    "function() { return 1 + 2; }"3);4Console.WriteLine(response.Result.Value);5var response = await client.Runtime.CallFunctionOnAsync(6    "function() { return 1 + 2; }",7    new CallFunctionOnOptions()8);9Console.WriteLine(response.Result.Value);10var response = await client.Runtime.CallFunctionOnAsync(11    "function() { return 1 + 2; }",12    new CallFunctionOnOptions()13    {14    }15);16Console.WriteLine(response.Result.Value);17var response = await client.Runtime.CallFunctionOnAsync(18    "function() { return 1 + 2; }",19    new CallFunctionOnOptions()20    {21    }22);23Console.WriteLine(response.Result.Value);24var response = await client.Runtime.CallFunctionOnAsync(25    "function() { return 1 + 2; }",26    new CallFunctionOnOptions()27    {28    }29);30Console.WriteLine(response.Result.Value);31var response = await client.Runtime.CallFunctionOnAsync(32    "function() { return 1 + 2; }",33    new CallFunctionOnOptions()34    {35    }36);37Console.WriteLine(response.Result.Value);38var response = await client.Runtime.CallFunctionOnAsync(39    "function() { return 1 + 2; }",40    new CallFunctionOnOptions()41    {

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp.Messaging;2using System;3using System.Threading.Tasks;4{5    {6        public static async Task<string> GetTitleAsync(Page page)7        {8            var expression = @"() => {9                    return document.title;10                }";11            var functionDeclaration = @"function() {12                    return document.title;13                }";14            var result = await page.EvaluateFunctionAsync<string>(functionDeclaration);15            return result;16        }17    }18}19using PuppeteerSharp.Messaging;20using System;21using System.Threading.Tasks;22{23    {24        public static async Task<string> GetTitleAsync(Page page)25        {26            var expression = @"() => {27                    return document.title;28                }";29            var functionDeclaration = @"function() {30                    return document.title;31                }";32            var result = await page.EvaluateFunctionAsync<string>(functionDeclaration);33            return result;34        }35    }36}37using PuppeteerSharp.Messaging;38using System;39using System.Threading.Tasks;40{41    {42        public static async Task<string> GetTitleAsync(Page page)43        {44            var expression = @"() => {45                    return document.title;46                }";47            var functionDeclaration = @"function() {48                    return document.title;49                }";50            var result = await page.EvaluateFunctionAsync<string>(functionDeclaration);51            return result;52        }53    }54}55using PuppeteerSharp.Messaging;56using System;57using System.Threading.Tasks;58{59    {60        public static async Task<string> GetTitleAsync(Page page)61        {62            var expression = @"() => {63                    return document.title;64                }";65            var functionDeclaration = @"function() {66                    return document.title;67                }";68            var result = await page.EvaluateFunctionAsync<string>(functionDeclaration);69            return result;70        }71    }72}

Full Screen

Full Screen

RuntimeCallFunctionOnRequest

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var response = await page.EvaluateFunctionAsync("() => {return 1 + 2;}");3Console.WriteLine(response);4var page = await browser.NewPageAsync();5var response = await page.EvaluateFunctionAsync<int>("() => {return 1 + 2;}");6Console.WriteLine(response);7var page = await browser.NewPageAsync();8var response = await page.EvaluateExpressionAsync("1 + 2");9Console.WriteLine(response);10var page = await browser.NewPageAsync();11var response = await page.EvaluateExpressionAsync<int>("1 + 2");12Console.WriteLine(response);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful