5
5
package org .owasp .encoder .testing .jakarta_test ;
6
6
7
7
import static org .junit .jupiter .api .Assertions .assertEquals ;
8
+ import static org .junit .jupiter .api .Assertions .assertNull ;
8
9
import org .junit .jupiter .api .BeforeAll ;
9
10
import org .junit .jupiter .api .Test ;
10
11
import org .openqa .selenium .By ;
12
+ import org .openqa .selenium .NoSuchElementException ;
11
13
import org .openqa .selenium .WebElement ;
12
14
import org .openqa .selenium .chrome .ChromeOptions ;
13
15
import org .openqa .selenium .remote .RemoteWebDriver ;
@@ -29,7 +31,6 @@ public class ItemControllerTest {
29
31
@ Container
30
32
static BrowserWebDriverContainer <?> container = new BrowserWebDriverContainer <>().
31
33
withCapabilities (new ChromeOptions ());
32
-
33
34
34
35
@ LocalServerPort
35
36
private int port ;
@@ -42,10 +43,20 @@ static void beforeAll(@Autowired Environment environment) {
42
43
43
44
@ Test
44
45
void shouldDisplayMessage () {
45
- RemoteWebDriver browser = new RemoteWebDriver (container .getSeleniumAddress (), new ChromeOptions ());
46
+ RemoteWebDriver browser = new RemoteWebDriver (container .getSeleniumAddress (), new ChromeOptions ());
46
47
browser .get ("http://host.testcontainers.internal:" + port + "/jakarta-test/item/viewItems" );
47
- assertEquals ("top<script>alert(1)</script>" , browser .findElement (By .id ("b2" )).getText ());
48
- assertEquals ("fancy <script>alert(1)</script>" , browser .findElement (By .id ("c2" )).getText ());
48
+ WebElement first = browser .findElement (By .id ("b2" ));
49
+ WebElement second = browser .findElement (By .id ("c2" ));
50
+ assertEquals ("top<script>alert(1)</script>" , first .getText ());
51
+ assertEquals ("fancy <script>alert(1)</script>" , second .getText ());
52
+ //todo yes - there are much better ways to check for an exception in junit
53
+ NoSuchElementException exception = null ;
54
+ try {
55
+ first .findElement (By .tagName ("script" ));
56
+ } catch (NoSuchElementException ex ) {
57
+ exception = ex ;
58
+ }
59
+ assertNull (exception );
49
60
50
61
}
51
62
}
0 commit comments