|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace PuppeteerSharp.Tests.Page |
| 10 | +{ |
| 11 | + [Collection("PuppeteerLoaderFixture collection")] |
| 12 | + public class AddScriptTagTests : PuppeteerPageBaseTest |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public async Task ShouldThrowAnErrorIfNoOptionsAreProvided() |
| 16 | + { |
| 17 | + var exception = await Assert.ThrowsAsync<ArgumentException>(() |
| 18 | + => Page.AddScriptTagAsync(new AddScriptTagOptions())); |
| 19 | + Assert.Equal("Provide options with a `Url`, `Path` or `Content` property", exception.Message); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task ShouldWorkWithAUrl() |
| 24 | + { |
| 25 | + await Page.GoToAsync(TestConstants.EmptyPage); |
| 26 | + var scriptHandle = await Page.AddScriptTagAsync(new AddScriptTagOptions { Url = "/injectedfile.js" }); |
| 27 | + Assert.NotNull(scriptHandle.AsElement()); |
| 28 | + Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__injected")); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task ShouldThrowAnErrorIfLoadingFromUrlFail() |
| 33 | + { |
| 34 | + await Page.GoToAsync(TestConstants.EmptyPage); |
| 35 | + var exception = await Assert.ThrowsAsync<PuppeteerException>(() |
| 36 | + => Page.AddScriptTagAsync(new AddScriptTagOptions { Url = "/nonexistfile.js" })); |
| 37 | + Assert.Equal("Loading script from /nonexistfile.js failed", exception.Message); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task ShouldWorkWithAPath() |
| 42 | + { |
| 43 | + await Page.GoToAsync(TestConstants.EmptyPage); |
| 44 | + var scriptHandle = await Page.AddScriptTagAsync(new AddScriptTagOptions |
| 45 | + { |
| 46 | + Path = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("assets", "injectedfile.js")) |
| 47 | + }); |
| 48 | + Assert.NotNull(scriptHandle.AsElement()); |
| 49 | + Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__injected")); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task ShouldIncludeSourcemapWhenPathIsProvided() |
| 54 | + { |
| 55 | + await Page.GoToAsync(TestConstants.EmptyPage); |
| 56 | + await Page.AddScriptTagAsync(new AddScriptTagOptions |
| 57 | + { |
| 58 | + Path = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("assets", "injectedfile.js")) |
| 59 | + }); |
| 60 | + var result = await Page.EvaluateExpressionAsync<string>("__injectedError.stack"); |
| 61 | + Assert.Contains(Path.Combine("assets", "injectedfile.js"), result); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task ShouldWorkWithContent() |
| 66 | + { |
| 67 | + await Page.GoToAsync(TestConstants.EmptyPage); |
| 68 | + var scriptHandle = await Page.AddScriptTagAsync(new AddScriptTagOptions { Content = "window.__injected = 35;" }); |
| 69 | + Assert.NotNull(scriptHandle.AsElement()); |
| 70 | + Assert.Equal(35, await Page.EvaluateExpressionAsync<int>("__injected")); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments