|
1 | 1 | namespace AspNetCoreTemplate.Web.Tests
|
2 | 2 | {
|
3 | 3 | using System;
|
4 |
| - using System.Diagnostics; |
5 | 4 | using System.Linq;
|
6 | 5 |
|
7 | 6 | using Microsoft.AspNetCore;
|
|
14 | 13 | public sealed class SeleniumServerFactory<TStartup> : WebApplicationFactory<TStartup>
|
15 | 14 | where TStartup : class
|
16 | 15 | {
|
17 |
| - private readonly Process process; |
18 |
| - private IWebHost host; |
19 |
| - |
20 | 16 | public SeleniumServerFactory()
|
21 | 17 | {
|
22 |
| - this.ClientOptions.BaseAddress = new Uri("https://localhost"); // will follow redirects by default |
23 |
| - this.CreateServer(this.CreateWebHostBuilder()); |
24 |
| - |
25 |
| - this.process = new Process |
26 |
| - { |
27 |
| - StartInfo = new ProcessStartInfo |
28 |
| - { |
29 |
| - FileName = "selenium-standalone", |
30 |
| - Arguments = "start", |
31 |
| - UseShellExecute = true, |
32 |
| - }, |
33 |
| - }; |
34 |
| - this.process.Start(); |
| 18 | + this.ClientOptions.BaseAddress = new Uri("https://localhost"); |
| 19 | + var host = WebHost.CreateDefaultBuilder(Array.Empty<string>()).UseStartup<TStartup>().Build(); |
| 20 | + host.Start(); |
| 21 | + this.RootUri = host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.LastOrDefault(); |
| 22 | + var testServer = new TestServer(new WebHostBuilder().UseStartup<FakeStartup>()); |
35 | 23 | }
|
36 | 24 |
|
37 | 25 | public string RootUri { get; set; }
|
38 | 26 |
|
39 |
| - protected override TestServer CreateServer(IWebHostBuilder builder) |
40 |
| - { |
41 |
| - this.host = builder.Build(); |
42 |
| - this.host.Start(); |
43 |
| - this.RootUri = this.host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.LastOrDefault(); // Last is https://localhost:5001! |
44 |
| - |
45 |
| - // Fake Server we won't use...this is lame. Should be cleaner, or a utility class |
46 |
| - return new TestServer(new WebHostBuilder().UseStartup<FakeStartup>()); |
47 |
| - } |
48 |
| - |
49 |
| - protected override IWebHostBuilder CreateWebHostBuilder() |
50 |
| - { |
51 |
| - var builder = WebHost.CreateDefaultBuilder(Array.Empty<string>()); |
52 |
| - builder.UseStartup<TStartup>(); |
53 |
| - return builder; |
54 |
| - } |
55 |
| - |
56 |
| - protected override void Dispose(bool disposing) |
57 |
| - { |
58 |
| - base.Dispose(disposing); |
59 |
| - if (disposing) |
60 |
| - { |
61 |
| - this.host.Dispose(); |
62 |
| - this.process.CloseMainWindow(); // Be sure to stop Selenium Standalone |
63 |
| - this.process.Dispose(); |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 | 27 | public class FakeStartup
|
68 | 28 | {
|
69 | 29 | public void ConfigureServices(IServiceCollection services)
|
|
0 commit comments