View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
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  }