|
| 1 | +using Microsoft.Playwright; |
| 2 | +using Microsoft.Playwright.NUnit; |
| 3 | +using NUnit.Framework; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace TextHub.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Example test demonstrating how to test a specific tool functionality |
| 10 | +/// This serves as a template for creating more specific tool tests |
| 11 | +/// </summary> |
| 12 | +[TestFixture] |
| 13 | +public class ExampleToolTest : PageTest |
| 14 | +{ |
| 15 | + private string BaseUrl => GlobalSetup.GetBaseUrl(); |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public async Task SetUp() |
| 19 | + { |
| 20 | + await Page.GotoAsync(BaseUrl); |
| 21 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task Example_WordCounterTool_ShouldWorkCorrectly() |
| 26 | + { |
| 27 | + // Navigate to the word counter tool |
| 28 | + await Page.GotoAsync($"{BaseUrl}/word-counter"); |
| 29 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 30 | + |
| 31 | + // Verify the page title |
| 32 | + await Expect(Page).ToHaveTitleAsync("Word Counter - Text Hub"); |
| 33 | + |
| 34 | + // Find the text input area |
| 35 | + var textInput = Page.Locator("textarea, input[type='text']").First; |
| 36 | + await Expect(textInput).ToBeVisibleAsync(); |
| 37 | + |
| 38 | + // Test with sample text |
| 39 | + var testText = "Hello world! This is a test with 8 words."; |
| 40 | + await textInput.FillAsync(testText); |
| 41 | + |
| 42 | + // Trigger any change events |
| 43 | + await textInput.BlurAsync(); |
| 44 | + await Page.WaitForTimeoutAsync(500); // Wait for any async processing |
| 45 | + |
| 46 | + // Look for word count display |
| 47 | + var wordCountElement = Page.Locator("[class*='count'], [class*='word'], [class*='result']").First; |
| 48 | + await Expect(wordCountElement).ToBeVisibleAsync(); |
| 49 | + |
| 50 | + // Get the displayed count |
| 51 | + var displayedCount = await wordCountElement.TextContentAsync(); |
| 52 | + |
| 53 | + // Verify the count is reasonable (exact count depends on implementation) |
| 54 | + Assert.That(displayedCount, Is.Not.Null.And.Not.Empty, "Word count should be displayed"); |
| 55 | + |
| 56 | + // You could add more specific assertions based on your implementation |
| 57 | + // For example, if the count is displayed as "8 words": |
| 58 | + // Assert.That(displayedCount, Does.Contain("8")); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task Example_TextCaseConversion_ShouldWorkCorrectly() |
| 63 | + { |
| 64 | + // Navigate to a text case conversion tool (e.g., camel case) |
| 65 | + await Page.GotoAsync($"{BaseUrl}/camel-case"); |
| 66 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 67 | + |
| 68 | + // Verify the page title |
| 69 | + await Expect(Page).ToHaveTitleAsync("camelCase Converter - Text Hub"); |
| 70 | + |
| 71 | + // Find the input and output areas |
| 72 | + var textInput = Page.Locator("textarea, input[type='text']").First; |
| 73 | + var textOutput = Page.Locator("[class*='output'], [class*='result'], [class*='converted']").First; |
| 74 | + |
| 75 | + await Expect(textInput).ToBeVisibleAsync(); |
| 76 | + await Expect(textOutput).ToBeVisibleAsync(); |
| 77 | + |
| 78 | + // Test with sample text |
| 79 | + var inputText = "hello world test"; |
| 80 | + |
| 81 | + await textInput.FillAsync(inputText); |
| 82 | + await textInput.BlurAsync(); |
| 83 | + await Page.WaitForTimeoutAsync(500); // Wait for conversion |
| 84 | + |
| 85 | + // Get the converted text |
| 86 | + var actualOutput = await textOutput.TextContentAsync(); |
| 87 | + |
| 88 | + // Verify the conversion worked |
| 89 | + Assert.That(actualOutput, Is.Not.Null.And.Not.Empty, "Converted text should be displayed"); |
| 90 | + |
| 91 | + // Note: The exact assertion depends on your implementation |
| 92 | + // You might need to adjust this based on how the output is formatted |
| 93 | + // Assert.That(actualOutput.Trim(), Is.EqualTo(expectedOutput)); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public async Task Example_JsonFormatter_ShouldFormatJson() |
| 98 | + { |
| 99 | + // Navigate to the JSON formatter tool |
| 100 | + await Page.GotoAsync($"{BaseUrl}/json-formatter"); |
| 101 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 102 | + |
| 103 | + // Verify the page title |
| 104 | + await Expect(Page).ToHaveTitleAsync("JSON Formatter - Text Hub"); |
| 105 | + |
| 106 | + // Find the input and output areas |
| 107 | + var jsonInput = Page.Locator("textarea, input[type='text']").First; |
| 108 | + var jsonOutput = Page.Locator("[class*='output'], [class*='result'], [class*='formatted']").First; |
| 109 | + |
| 110 | + await Expect(jsonInput).ToBeVisibleAsync(); |
| 111 | + await Expect(jsonOutput).ToBeVisibleAsync(); |
| 112 | + |
| 113 | + // Test with minified JSON |
| 114 | + var minifiedJson = "{\"name\":\"test\",\"value\":123,\"items\":[1,2,3]}"; |
| 115 | + await jsonInput.FillAsync(minifiedJson); |
| 116 | + await jsonInput.BlurAsync(); |
| 117 | + await Page.WaitForTimeoutAsync(500); // Wait for formatting |
| 118 | + |
| 119 | + // Get the formatted JSON |
| 120 | + var formattedJson = await jsonOutput.TextContentAsync(); |
| 121 | + |
| 122 | + // Verify the formatting worked |
| 123 | + Assert.That(formattedJson, Is.Not.Null.And.Not.Empty, "Formatted JSON should be displayed"); |
| 124 | + |
| 125 | + // The formatted JSON should be different from the input (more readable) |
| 126 | + Assert.That(formattedJson, Is.Not.EqualTo(minifiedJson), "Formatted JSON should be different from input"); |
| 127 | + } |
| 128 | + |
| 129 | + [Test] |
| 130 | + public async Task Example_Tool_ShouldHandleEmptyInput() |
| 131 | + { |
| 132 | + // Navigate to any tool (using word counter as example) |
| 133 | + await Page.GotoAsync($"{BaseUrl}/word-counter"); |
| 134 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 135 | + |
| 136 | + // Find the text input |
| 137 | + var textInput = Page.Locator("textarea, input[type='text']").First; |
| 138 | + await Expect(textInput).ToBeVisibleAsync(); |
| 139 | + |
| 140 | + // Test with empty input |
| 141 | + await textInput.FillAsync(""); |
| 142 | + await textInput.BlurAsync(); |
| 143 | + await Page.WaitForTimeoutAsync(500); |
| 144 | + |
| 145 | + // Verify the tool handles empty input gracefully |
| 146 | + // (No errors should occur, and output should show appropriate message) |
| 147 | + var output = Page.Locator("[class*='count'], [class*='result'], [class*='output']").First; |
| 148 | + await Expect(output).ToBeVisibleAsync(); |
| 149 | + |
| 150 | + // The output should indicate zero or empty result |
| 151 | + var outputText = await output.TextContentAsync(); |
| 152 | + Assert.That(outputText, Is.Not.Null, "Output should be displayed even for empty input"); |
| 153 | + } |
| 154 | + |
| 155 | + [Test] |
| 156 | + public async Task Example_Tool_ShouldHandleLargeInput() |
| 157 | + { |
| 158 | + // Navigate to any tool (using word counter as example) |
| 159 | + await Page.GotoAsync($"{BaseUrl}/word-counter"); |
| 160 | + await Page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 161 | + |
| 162 | + // Find the text input |
| 163 | + var textInput = Page.Locator("textarea, input[type='text']").First; |
| 164 | + await Expect(textInput).ToBeVisibleAsync(); |
| 165 | + |
| 166 | + // Test with large input |
| 167 | + var largeText = string.Join(" ", Enumerable.Repeat("word", 1000)); // 1000 words |
| 168 | + await textInput.FillAsync(largeText); |
| 169 | + await textInput.BlurAsync(); |
| 170 | + await Page.WaitForTimeoutAsync(1000); // Wait longer for processing |
| 171 | + |
| 172 | + // Verify the tool handles large input without issues |
| 173 | + var output = Page.Locator("[class*='count'], [class*='result'], [class*='output']").First; |
| 174 | + await Expect(output).ToBeVisibleAsync(); |
| 175 | + |
| 176 | + // The output should show the correct count |
| 177 | + var outputText = await output.TextContentAsync(); |
| 178 | + Assert.That(outputText, Is.Not.Null.And.Not.Empty, "Output should be displayed for large input"); |
| 179 | + } |
| 180 | +} |
0 commit comments