async Task IAsyncLifetime.InitializeAsync() { Client = new BlazorClient() { CaptureOperations = true, DefaultOperationTimeout = Timeout, }; Client.LoggerProvider = new XunitLoggerProvider(Output); Client.FormatError = (error) => { var logs = string.Join(Environment.NewLine, Logs); return(new Exception(error + Environment.NewLine + logs)); }; InitializeFixture(ServerFixture); _ = ServerFixture.RootUri; // This is needed for the side-effects of starting the server. if (ServerFixture is WebHostServerFixture hostFixture) { TestSink = hostFixture.Host.Services.GetRequiredService <TestSink>(); TestSink.MessageLogged += TestSink_MessageLogged; } await InitializeAsync(); }
static async Task Main(string[] args) { var client = new BlazorClient(); client.JSInterop += async(args) => { if (args.Identifier == "Blazor._internal.navigationManager.navigateTo") { var jsonDocument = JsonDocument.Parse(args.ArgsJson); var array = jsonDocument.RootElement; var uri = array[0].GetString(); if (array[1].GetBoolean()) { throw new NotSupportedException("Force load is not supported."); } await client.NavigateAsync(ServerUrl + "/" + uri); } }; await client.ConnectAsync(new Uri(ServerUrl)); await Navigate(client); await Clicks(client); await NavigateOnClick(client); await TypeAhead(client); Console.WriteLine("Done"); }
public static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.Services.AddLogging(builder => builder .AddBrowserConsole() .SetMinimumLevel(LogLevel.Trace) ); builder.RootComponents.Add <App>("#app"); builder.Services.AddOptions(); builder.Services.AddAuthorizationCore(); builder.Services.AddBlazoredLocalStorage(config => config.JsonSerializerOptions.WriteIndented = true); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddScoped <AuthenticationStateProvider>(s => s.GetRequiredService <ServiceStackStateProvider>()); builder.Services.AddScoped(_ => BlazorClient.Create(builder.HostEnvironment.BaseAddress)); builder.Services.AddScoped <ServiceStackStateProvider>(); await builder.Build().RunAsync(); }
internal Task SelectAsync(BlazorClient client, string value) { if (!Events.TryGetValue("change", out var changeEventDescriptor)) { throw new InvalidOperationException("Element does not have a change event."); } var args = new TestChangeEventArgs { Value = value }; var webEventDescriptor = new WebEventDescriptor { EventHandlerId = changeEventDescriptor.EventId, EventName = "change", EventFieldInfo = new EventFieldInfo { ComponentId = 0, FieldValue = value } }; return(DispatchEventCore(client, webEventDescriptor, args)); }
static async Task Clicks(BlazorClient client) { var batch = await client.ExpectRenderBatch(() => client.NavigateAsync(ServerUrl + "/home")); if (!client.Hive.TryFindElementById("changeState", out var changeState)) { throw new InvalidOperationException($"Expected to have navigated to the home page."); } client.Hive.TryFindElementById("state", out var state); await changeState.ClickAsync(client.HubConnection); await client.PrepareForNextBatch(null); if (state.Attributes["data-state"].ToString() == "Clicked") { Console.WriteLine("State changed to clicked."); } else { throw new InvalidOperationException("State was not 'Clicked'."); } await client.ExpectRenderBatch(() => changeState.DoubleClickAsync(client.HubConnection)); if (state.Attributes["data-state"].ToString() == "DoubleClicked") { Console.WriteLine("State changed to dblclicked."); } else { throw new InvalidOperationException("State was not 'DoubleClicked'."); } }
static async ValueTask VerifyNavigationAsync(BlazorClient client) { await client.ExpectRenderBatch(() => client.NavigateAsync($"{ServerUrl}/counter")); client.Hive.TryFindElementById("counter", out var counter); Debug.Assert(counter != null, "We must have navigated to counter."); }
async static Task RunAsync(Uri uri, BlazorClient client, CancellationToken cancellationToken) { var counterUri = uri.AbsoluteUri + "/counter"; await using (client) { try { await client.ExpectRenderBatch(async() => { await client.HubConnection.InvokeAsync("OnLocationChanged", $"{counterUri.ToString()}", false, cancellationToken); }); while (!cancellationToken.IsCancellationRequested) { await client.ClickAsync("clicker"); await Task.Delay(TimeSpan.FromSeconds(0.5)); } } catch (Exception ex) { Console.WriteLine("Client failed: " + ex); } } }
async static Task <int> Main(string[] args) { if (args.Length == 0) { Console.WriteLine("usage: DemoClient <url> <count>"); return(1); } var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, e) => { Console.Write("Cancelling..."); cts.Cancel(); }; var uri = new Uri(args[0]); var count = int.Parse(args[1]); var tasks = new List <Task>(); for (var i = 0; i < count; i++) { Console.WriteLine($"Creating client {i}..."); var client = new BlazorClient() { DefaultOperationTimeout = TimeSpan.FromSeconds(5), }; while (true) { try { var connected = await client.ConnectAsync(uri, connectAutomatically : true); if (!connected) { throw new InvalidOperationException("Failed to connect."); } break; } catch (HttpRequestException ex) when(ex.Message.Contains("429")) { Console.WriteLine("Getting rate limited. Waiting..."); } await Task.Delay(TimeSpan.FromSeconds(5)); } tasks.Add(Task.Run(() => RunAsync(uri, client, cts.Token))); } Console.WriteLine($"Started {count} clients."); await Task.WhenAll(tasks); return(0); }
static async Task Main(string[] args) { var client = new BlazorClient(); await client.ConnectAsync(new Uri(ServerUrl)); await VerifyNavigationAsync(client); Console.WriteLine("Done"); }
public ComponentHubReliabilityTest(ITestOutputHelper output) { Output = output; Client = new BlazorClient() { CaptureOperations = true, DefaultOperationTimeout = Timeout, }; Client.LoggerProvider = new XunitLoggerProvider(Output); }
private void CreateDefaultConfiguration() { Client = new BlazorClient() { DefaultLatencyTimeout = DefaultLatencyTimeout }; Client.RenderBatchReceived += (id, data) => Batches.Add(new Batch(id, data)); Client.OnCircuitError += (error) => Errors.Add(error); _ = _serverFixture.RootUri; // this is needed for the side-effects of getting the URI. TestSink = _serverFixture.Host.Services.GetRequiredService <TestSink>(); TestSink.MessageLogged += LogMessages; }
static async Task NavigateOnClick(BlazorClient client) { var batch = await client.ExpectRenderBatch(() => client.NavigateAsync(ServerUrl + "/home")); if (!client.Hive.TryFindElementById("navigateOnClick", out var navigateOnClick)) { throw new InvalidOperationException($"Expected to have navigated to the home page."); } await navigateOnClick.ClickAsync(client.HubConnection); // Wait for one or more renders that causes fetchdata to have been updated. await client.ExistsAsync("fetchdata"); Console.WriteLine("Completed navigation on click scenarios"); }
public RemoteRendererBufferLimitTest(AspNetSiteServerFixture serverFixture) { serverFixture.BuildWebHostMethod = TestServer.Program.BuildWebHost; _serverFixture = serverFixture; // Needed here for side-effects _ = _serverFixture.RootUri; Client = new BlazorClient() { DefaultLatencyTimeout = DefaultLatencyTimeout }; Client.RenderBatchReceived += (id, data) => Batches.Add(new Batch(id, data)); Sink = _serverFixture.Host.Services.GetRequiredService <TestSink>(); Sink.MessageLogged += LogMessages; }
public static async Task ExistsAsync(this BlazorClient client, string id, TimeSpan?timeout = default) { timeout ??= TimeSpan.FromSeconds(3); var cts = new CancellationTokenSource(timeout.Value); while (!cts.IsCancellationRequested) { if (client.Hive.TryFindElementById(id, out _)) { return; } await client.PrepareForNextBatch(timeout); } throw new TimeoutException($"Unable to find element with id {id} in {timeout.Value} duration."); }
private void CreateDefaultConfiguration() { Client = new BlazorClient() { DefaultLatencyTimeout = DefaultLatencyTimeout }; Client.RenderBatchReceived += (id, data) => Batches.Add(new Batch(id, data)); Client.OnCircuitError += (error) => Errors.Add(error); Client.LoggerProvider = new XunitLoggerProvider(Output); Client.FormatError = (error) => { var logs = string.Join(Environment.NewLine, Logs); return(new Exception(error + Environment.NewLine + logs)); }; _ = _serverFixture.RootUri; // this is needed for the side-effects of getting the URI. TestSink = _serverFixture.Host.Services.GetRequiredService <TestSink>(); TestSink.MessageLogged += LogMessages; }
static async Task Navigate(BlazorClient client) { var links = new[] { "counter", "fetchdata", "home" }; for (var i = 0; i < 5; i++) { var link = links[i % links.Length]; var batch = await client.ExpectRenderBatch(() => client.NavigateAsync(ServerUrl + "/" + link)); if (!client.Hive.TryFindElementById(link, out _)) { throw new InvalidOperationException($"Expected to have navigated to {link}."); } Console.WriteLine($"Navigated to {link}."); await Task.Delay(50); } Console.WriteLine("Completed navigation scenarios"); }
public Task ClickAsync(BlazorClient client) { if (!Events.TryGetValue("click", out var clickEventDescriptor)) { throw new InvalidOperationException("Element does not have a click event."); } var mouseEventArgs = new TestMouseEventArgs { Type = clickEventDescriptor.EventName, Detail = 1 }; var webEventDescriptor = new WebEventDescriptor { EventHandlerId = clickEventDescriptor.EventId, EventName = "click", }; return(DispatchEventCore(client, webEventDescriptor, mouseEventArgs)); }
public static Task NavigateAsync(this BlazorClient client, string url, CancellationToken cancellationToken = default) { return(client.HubConnection.InvokeAsync("OnLocationChanged", url, false, cancellationToken)); }
private static Task DispatchEventCore(BlazorClient client, WebEventDescriptor descriptor, EventArgs eventArgs) => client.DispatchEventAsync(descriptor, eventArgs);
private Task ValidateClientKeepsWorking(BlazorClient Client, List <(int, int, byte[])> batches) =>