1+ import org .junit .jupiter .api .Test ;
2+ import org .openqa .selenium .By ;
3+ import org .openqa .selenium .Keys ;
4+ import org .openqa .selenium .WebElement ;
5+ import org .openqa .selenium .chrome .ChromeOptions ;
6+ import org .openqa .selenium .remote .RemoteWebDriver ;
7+ import org .testcontainers .containers .BrowserWebDriverContainer ;
8+ import org .testcontainers .junit .jupiter .Container ;
9+ import org .testcontainers .junit .jupiter .Testcontainers ;
10+ import java .time .Duration ;
11+ import static org .assertj .core .api .Assertions .assertThat ;
12+
13+ @ Testcontainers
14+ class BaiduSearchTest {
15+
16+ @ Container
17+ public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer ()
18+ .withCapabilities (new ChromeOptions ());
19+
20+ @ Test
21+ void testNormalSearch () {
22+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
23+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
24+
25+ // 打开百度网站
26+ driver .get ("https://www.baidu.com/?a" );
27+
28+ // 找到搜索输入框并输入"Testcontainers"
29+ WebElement searchInput = driver .findElement (By .id ("kw" ));
30+ searchInput .sendKeys ("Testcontainers" );
31+
32+ // 按下回车键进行搜索
33+ searchInput .sendKeys (Keys .RETURN );
34+
35+ // 验证搜索结果是否包含"Testcontainers"
36+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
37+ assertThat (searchResult .getText ()).contains ("Testcontainers" );
38+
39+ driver .quit ();
40+ }
41+
42+ @ Test
43+ void testEmptySearch () {
44+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
45+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
46+
47+ // 打开百度网站
48+ driver .get ("https://www.baidu.com/?a" );
49+
50+ // 找到搜索输入框并输入空字符串
51+ WebElement searchInput = driver .findElement (By .id ("kw" ));
52+ searchInput .sendKeys ("" );
53+
54+ // 按下回车键进行搜索
55+ searchInput .sendKeys (Keys .RETURN );
56+
57+ // 验证是否没有搜索结果或显示提示信息
58+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
59+ assertThat (searchResult .getText ()).isEmpty ();
60+
61+ driver .quit ();
62+ }
63+
64+ @ Test
65+ void testSpecialCharactersSearch () {
66+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
67+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
68+
69+ // 打开百度网站
70+ driver .get ("https://www.baidu.com/?a" );
71+
72+ // 找到搜索输入框并输入特殊字符
73+ WebElement searchInput = driver .findElement (By .id ("kw" ));
74+ searchInput .sendKeys ("!@#$%^&*()_+{}[]|\\ :;'\" <>,.?/" );
75+
76+ // 按下回车键进行搜索
77+ searchInput .sendKeys (Keys .RETURN );
78+
79+ // 验证搜索结果是否包含特殊字符或显示提示信息
80+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
81+ assertThat (searchResult .getText ()).contains ("!@#$%^&*" );
82+
83+ driver .quit ();
84+ }
85+
86+ @ Test
87+ void testLongStringSearch () {
88+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
89+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
90+
91+ // 打开百度网站
92+ driver .get ("https://www.baidu.com/?a" );
93+
94+ // 找到搜索输入框并输入非常长的字符串
95+ WebElement searchInput = driver .findElement (By .id ("kw" ));
96+ String longString = "a" .repeat (1000 );
97+ searchInput .sendKeys (longString );
98+
99+ // 按下回车键进行搜索
100+ searchInput .sendKeys (Keys .RETURN );
101+
102+ // 验证搜索结果是否包含长字符串的部分内容或显示提示信息
103+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
104+ assertThat (searchResult .getText ()).contains ("aaaaa" );
105+
106+ driver .quit ();
107+ }
108+
109+ @ Test
110+ void testSpaceSearch () {
111+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
112+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
113+
114+ // 打开百度网站
115+ driver .get ("https://www.baidu.com/?a" );
116+
117+ // 找到搜索输入框并输入空格
118+ WebElement searchInput = driver .findElement (By .id ("kw" ));
119+ searchInput .sendKeys (" " );
120+
121+ // 按下回车键进行搜索
122+ searchInput .sendKeys (Keys .RETURN );
123+
124+ // 验证是否没有搜索结果或显示提示信息
125+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
126+ assertThat (searchResult .getText ()).isEmpty ();
127+
128+ driver .quit ();
129+ }
130+
131+ @ Test
132+ void testEnglishCharactersSearch () {
133+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
134+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
135+
136+ // 打开百度网站
137+ driver .get ("https://www.baidu.com/?a" );
138+
139+ // 找到搜索输入框并输入英文字符
140+ WebElement searchInput = driver .findElement (By .id ("kw" ));
141+ searchInput .sendKeys ("Hello World" );
142+
143+ // 按下回车键进行搜索
144+ searchInput .sendKeys (Keys .RETURN );
145+
146+ // 验证搜索结果是否包含英文字符
147+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
148+ assertThat (searchResult .getText ()).contains ("Hello World" );
149+
150+ driver .quit ();
151+ }
152+
153+ @ Test
154+ void testSqlInjectionSearch () {
155+ RemoteWebDriver driver = new RemoteWebDriver (chrome .getSeleniumAddress (), new ChromeOptions ());
156+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (30 ));
157+
158+ // 打开百度网站
159+ driver .get ("https://www.baidu.com/?a" );
160+
161+ // 找到搜索输入框并输入SQL注入语句
162+ WebElement searchInput = driver .findElement (By .id ("kw" ));
163+ searchInput .sendKeys ("' OR 1=1 --" );
164+
165+ // 按下回车键进行搜索
166+ searchInput .sendKeys (Keys .RETURN );
167+
168+ // 验证搜索结果是否正常显示或显示错误信息
169+ WebElement searchResult = driver .findElement (By .id ("content_left" ));
170+ assertThat (searchResult .getText ()).isNotEmpty ();
171+
172+ driver .quit ();
173+ }
174+ }
0 commit comments