Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REGRESSION]: QuerySelectorAllAsync causes InvalidCastException #2830

Closed
JustusGreiberORGADATA opened this issue Jan 17, 2024 · 3 comments · Fixed by #2831
Closed

[REGRESSION]: QuerySelectorAllAsync causes InvalidCastException #2830

JustusGreiberORGADATA opened this issue Jan 17, 2024 · 3 comments · Fixed by #2831

Comments

@JustusGreiberORGADATA
Copy link

JustusGreiberORGADATA commented Jan 17, 2024

Context:

  • GOOD Playwright Version: 1.40.0
  • BAD Playwright Version: 1.41.0
  • Operating System: Windows
  • Extra: <TargetFramework>net6.0</TargetFramework>

Code Snippet

Project at https://github.com/JustusGreiberORGADATA/playwright-reproduction

using System.Threading.Tasks;
using Microsoft.Playwright;

using (var playwright = await Playwright.CreateAsync())
await using (var browser = await playwright.Chromium.LaunchAsync())
await using (var context = await browser.NewContextAsync()) {
        var page = await context.NewPageAsync();
        await page.GotoAsync("https://www.theverge.com");
        Console.WriteLine((await page.QuerySelectorAllAsync(".flex")).Count);
}

Prints stacktrace

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Microsoft.Playwright.Transport.Converters.ChannelToGuidConverter' to type 'System.Text.Json.Serialization.JsonConverter`1[Microsoft.Playwright.Core.ElementHandle]'.
   at System.Text.Json.Serialization.JsonCollectionConverter`2.GetElementConverter(JsonTypeInfo elementTypeInfo)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](JsonElement element, JsonTypeInfo jsonTypeInfo)
   at Microsoft.Playwright.Helpers.JsonExtensions.ToObject[T](JsonElement element, JsonSerializerOptions options) in /_/src/Playwright/Helpers/JsonExtensions.cs:line 52
   at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](ChannelOwner object, String method, Dictionary`2 dictionary, Boolean keepNulls) in /_/src/Playwright/Transport/Connection.cs:line 222
   at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 action, Boolean isInternal)
   at Microsoft.Playwright.Core.Frame.QuerySelectorAllAsync(String selector) in /_/src/Playwright/Core/Frame.cs:line 645
   at Program.<Main>$(String[] args) in D:\repos\playwright-reproduction\Program.cs:line 9
   at Program.<Main>$(String[] args) in D:\repos\playwright-reproduction\Program.cs:line 10
   at Program.<Main>$(String[] args) in D:\repos\playwright-reproduction\Program.cs:line 10
   at Program.<Main>(String[] args)

Describe the bug

This for some reason only happens if my TargetFramework is net6.0. On .net7.0 and .net8.0 everything works fine.

@mxschmitt
Copy link
Member

I was able to reproduce, thanks for filing!

@daveaglick
Copy link

daveaglick commented Jan 17, 2024

What timing! I actually came here to find/report something similar, but for me it happens when capturing console messages. Same error message:

Microsoft.Playwright.TargetClosedException : Unable to cast object of type 'Microsoft.Playwright.Transport.Converters.ChannelToGuidConverter' to type 'System.Text.Json.Serialization.JsonConverter`1[Microsoft.Playwright.Core.JSHandle]'.
  ----> System.InvalidCastException : Unable to cast object of type 'Microsoft.Playwright.Transport.Converters.ChannelToGuidConverter' to type 'System.Text.Json.Serialization.JsonConverter`1[Microsoft.Playwright.Core.JSHandle]'.
   at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](ChannelOwner object, String method, Dictionary`2 dictionary, Boolean keepNulls) in /_/src/Playwright/Transport/Connection.cs:line 208
   at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 action, Boolean isInternal) in /_/src/Playwright/Transport/Connection.cs:line 538
   at Microsoft.Playwright.Core.Frame.GotoAsync(String url, FrameGotoOptions options) in /_/src/Playwright/Core/Frame.cs:line 792
   at Sipc.WebSite.Portal.Tests.FirstTestFixture.Test() in C:\Code\SIPC\Sipc.BackOffice\tests\Sipc.WebSite.Portal.Tests\FirstTestFixture.cs:line 72
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
   at NUnit.Framework.Internal.Commands.DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
--InvalidCastException
   at System.Text.Json.Serialization.JsonCollectionConverter`2.GetElementConverter(JsonTypeInfo elementTypeInfo)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](JsonElement element, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](JsonElement element, JsonSerializerOptions options)
   at Microsoft.Playwright.Helpers.JsonExtensions.ToObject[T](JsonElement element, JsonSerializerOptions options) in /_/src/Playwright/Helpers/JsonExtensions.cs:line 52
   at Microsoft.Playwright.Core.BrowserContext.OnMessage(String method, Nullable`1 serverParams) in /_/src/Playwright/Core/BrowserContext.cs:line 161
   at Microsoft.Playwright.Transport.Connection.Dispatch(PlaywrightServerMessage message) in /_/src/Playwright/Transport/Connection.cs:line 314

I can also confirm that downgrading the package to 1.40.0 or updating the target to .NET 8 both resolve the issue.

@mxschmitt
Copy link
Member

@daveaglick thanks, we'll release an update soon - .NET 8 is a great workaround in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants