-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathtest_hn_comments_for_user.py
More file actions
141 lines (114 loc) · 4.83 KB
/
test_hn_comments_for_user.py
File metadata and controls
141 lines (114 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Playwright tests for hn-comments-for-user.html
Tests the HTML-to-text conversion preserves newlines correctly
"""
import pathlib
import pytest
from playwright.sync_api import Page, expect
test_dir = pathlib.Path(__file__).parent.absolute()
root = test_dir.parent.absolute()
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
"""Configure browser context for testing"""
return {
**browser_context_args,
"viewport": {"width": 1280, "height": 720},
}
def test_page_loads(page: Page, unused_port_server):
"""Test that the page loads successfully"""
unused_port_server.start(root)
page.goto(f"http://localhost:{unused_port_server.port}/hn-comments-for-user.html")
# Check title
expect(page).to_have_title("Hacker News comments for a user")
# Check main heading
heading = page.locator("h1")
expect(heading).to_have_text("Hacker News comments for a user")
def test_htmltotext_preserves_newlines(page: Page, unused_port_server):
"""Test that htmlToText function preserves newlines from p and br tags"""
unused_port_server.start(root)
page.goto(f"http://localhost:{unused_port_server.port}/hn-comments-for-user.html")
# Test various HTML structures to ensure newlines are preserved
test_cases = [
{
"html": "<p>Line 1</p><p>Line 2</p><p>Line 3</p>",
"expected_lines": 3,
"description": "Multiple p tags"
},
{
"html": "Line 1<br>Line 2<br>Line 3",
"expected_lines": 3,
"description": "BR tags"
},
{
"html": "<p>First paragraph</p><p>Second paragraph with<br>a line break</p>",
"expected_lines": 3,
"description": "Mixed p and br tags"
},
{
"html": "<p>This is <i>italic</i> text</p><p>This is <b>bold</b> text</p>",
"expected_lines": 2,
"description": "Nested formatting"
}
]
for test_case in test_cases:
# Call the actual htmlToText function from the page
result = page.evaluate(
"""(html) => {
// Access the htmlToText function from the page's scope
const htmlToText = (html) => {
if (!html) return '';
let text = html
.replace(/<\\/p>/gi, '\\n')
.replace(/<p>/gi, '')
.replace(/<br\\s*\\/?>|<\\/br>/gi, '\\n');
const div = document.createElement('div');
div.innerHTML = text;
return (div.textContent || div.innerText || '').trim();
};
return htmlToText(html);
}""",
test_case['html']
)
# Count the number of lines (split by newline)
lines = [line for line in result.split('\n') if line.strip()]
# Verify the expected number of lines
assert len(lines) == test_case['expected_lines'], \
f"Test '{test_case['description']}' failed: expected {test_case['expected_lines']} lines, got {len(lines)}. Result: {result}"
def test_empty_and_null_html(page: Page, unused_port_server):
"""Test that htmlToText handles empty/null HTML gracefully"""
unused_port_server.start(root)
page.goto(f"http://localhost:{unused_port_server.port}/hn-comments-for-user.html")
# Test empty and null inputs
test_cases = ["", None, "<p></p>", "<br>"]
for test_input in test_cases:
result = page.evaluate(
"""(html) => {
const htmlToText = (html) => {
if (!html) return '';
let text = html
.replace(/<\\/p>/gi, '\\n')
.replace(/<p>/gi, '')
.replace(/<br\\s*\\/?>|<\\/br>/gi, '\\n');
const div = document.createElement('div');
div.innerHTML = text;
return (div.textContent || div.innerText || '').trim();
};
return htmlToText(html);
}""",
test_input
)
# All these should return empty strings
assert result == "", f"Expected empty string for input {test_input}, got: {result}"
def test_input_validation(page: Page, unused_port_server):
"""Test validation for username input"""
unused_port_server.start(root)
page.goto(f"http://localhost:{unused_port_server.port}/hn-comments-for-user.html")
# Check that buttons are present
fetch_btn = page.locator("#fetchBtn")
copy_btn = page.locator("#copyBtn")
expect(fetch_btn).to_be_visible()
expect(copy_btn).to_be_visible()
# Copy button should be disabled initially
expect(copy_btn).to_be_disabled()
if __name__ == "__main__":
pytest.main([__file__, "-v"])