Hi guys, we are migrating our test framework from the Selenium to the Playwright and I had run into a problem. We are using the strategy pattern to run our automation tests until everything will be rewritten to the Playwright engine. And I have the following code which executes the JS code.
This is the common code for Selenium and for the Playwright
./some-POM-file.cs
public static void ScrollIntoViewIfNeeded(IControlAdapter element)
{Browser.JavaScriptExecutor.ExecuteScript("arguments[0].scrollIntoViewIfNeeded();", element);
}
./Browser.cs
public static IJSExecutor JavaScriptExecutor => _browserStrategy.JavaScriptExecutor;
./PlaywrightJSExecutor
private object EvaluateJavaScript(string jsScript, IControlAdapter control = null)
{
var evaluatedScript = PreEvaluating(jsScript);
return _page.EvaluateAsync(evaluatedScript, new object[] { control.ToLocator() }).GetAwaiter().GetResult().Value.ToString();
}
private string PreEvaluating(string jsScript) => jsScript.Replace("return ", string.Empty);
public object ExecuteScript(string script, IControlAdapter control = null)
{
return EvaluateJavaScript(script, control);
}
./Extensions.cs
public static ILocator ToLocator(this IControlAdapter controlAdapter)
{
if (controlAdapter is not null)
{
if (controlAdapter is LocatorAdapter locatorAdapter)
{
return locatorAdapter?.GetControlInstance();
}
else
{
throw new Exception($"The {controlAdapter?.GetType().Name} is not applicable to {typeof(ILocator)}.");
}
}
return null;
}
This thread is trying to answer question "How to resolve the 'ReferenceError: arguments is not defined' error when migrating from Selenium to Playwright?"
so, when I try to pass this JS script
arguments[0].scrollIntoViewIfNeeded();
and cast element to ILocator
with ToLocator()
extension method, I got the next error:
Microsoft.Playwright.PlaywrightException : ReferenceError: arguments is not defined
at eval (eval at evaluate (:201:30), <anonymous>:1:1)
at eval (<anonymous>)
at UtilityScript.evaluate (<anonymous>:201:30)
at UtilityScript.<anonymous> (<anonymous>:1:44)
Any ideas guys? Thank u
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].