1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.junit;
20
21 import junit.framework.TestCase;
22 import net.sourceforge.jwebunit.api.ITestingEngine;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 public class WebTesterTest extends TestCase {
28 public void testAssertImageSrcContainsShouldDelegateXPathExpressionToUnderlyingTestingEngine() throws Exception {
29 String xpath = "//img[" + contains("src", "SOME URL") + "]";
30
31 ITestingEngine engine = engine(xpath, true);
32 WebTester tester = webTester(engine);
33
34 tester.assertImagePresentPartial("SOME URL", null);
35
36 verify(engine).hasElementByXPath(xpath);
37 }
38
39 public void testAssertImageContainsShouldDelegateXPathExpressionToUnderlyingTestingEngine() throws Exception {
40 String xpath = "//img[" + contains("src", "SOME URL") + " and " + contains("alt", "SOME ALT TEXT") + "]";
41
42 ITestingEngine engine = engine(xpath, true);
43 WebTester tester = webTester(engine);
44
45 tester.assertImagePresentPartial("SOME URL", "SOME ALT TEXT");
46
47 verify(engine).hasElementByXPath(xpath);
48 }
49
50 private String contains(String attribute, String substring) {
51 return "contains(@" + attribute + ", \"" + substring + "\")";
52 }
53
54 private ITestingEngine engine(String xpath, boolean hasElement) {
55 ITestingEngine mock = mock(ITestingEngine.class);
56 when(mock.hasElementByXPath(xpath)).thenReturn(hasElement);
57 return mock;
58 }
59
60 private WebTester webTester(ITestingEngine engine) {
61 WebTester tester = new WebTester();
62 tester.setDialog(engine);
63 return tester;
64 }
65 }