1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.selenium.framework

File AbstractXWikiTestCase.java

 
testLockingAndUnlocking: [Is This Page Locked true] isn't present.
testCodeToExecuteNotInlineIfNoConfigurationClass: [//span[@class='xwikirenderingerror']] is present.
testCodeToExecuteNotInline: [//span[@class='xwikirenderingerror']] is present.
 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

30
212
127
1
976
685
145
0.68
1.67
127
1.14

Classes

Class Line # Actions
AbstractXWikiTestCase 47 212 0% 145 83
0.7750677577.5%
 

Contributing tests

This file is covered by 331 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.selenium.framework;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.IOException;
24    import java.util.Map.Entry;
25    import java.util.Properties;
26   
27    import org.junit.Assert;
28    import org.junit.Before;
29    import org.openqa.selenium.By;
30    import org.openqa.selenium.Keys;
31    import org.openqa.selenium.Point;
32    import org.openqa.selenium.WebDriver;
33    import org.openqa.selenium.WebElement;
34    import org.openqa.selenium.interactions.Actions;
35    import org.xwiki.test.ui.AbstractTest;
36   
37    import com.thoughtworks.selenium.Selenium;
38    import com.thoughtworks.selenium.SeleniumException;
39    import com.thoughtworks.selenium.Wait;
40    import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
41   
42    /**
43    * All XWiki Selenium tests must extend this class.
44    *
45    * @version $Id: c7cb7a6044a1b7056ddb23ce1a8e62d04da4527e $
46    */
 
47    public abstract class AbstractXWikiTestCase extends AbstractTest implements SkinExecutor
48    {
49    public static final String BASEDIR = System.getProperty("basedir");
50   
51    public static final String DOC = "selenium.browserbot.getCurrentWindow().document.";
52   
53    private static final int WAIT_TIME = 30000;
54   
55    private SkinExecutor skinExecutor = new FlamingoSkinExecutor(this);
56   
57    private Selenium selenium;
58   
 
59  0 toggle public void setSkinExecutor(SkinExecutor skinExecutor)
60    {
61  0 this.skinExecutor = skinExecutor;
62    }
63   
 
64  587 toggle public SkinExecutor getSkinExecutor()
65    {
66  587 return this.skinExecutor;
67    }
68   
 
69  6276 toggle public Selenium getSelenium()
70    {
71  6276 if (this.selenium == null) {
72  333 String baseURL = "http://localhost:" + System.getProperty("xwikiPort", "8080");
73  333 this.selenium = new WebDriverBackedSelenium(getDriver(), baseURL);
74    }
75  6276 return this.selenium;
76    }
77   
 
78  333 toggle @Before
79    public void setUp()
80    {
81  333 loginAsAdmin();
82    }
83   
84    // Convenience methods wrapping Selenium
85   
 
86  902 toggle public void open(String url)
87    {
88  902 getSelenium().open(url);
89    }
90   
 
91  404 toggle public void open(String space, String page)
92    {
93  404 open(getUrl(space, page));
94    }
95   
 
96  26 toggle public void open(String space, String page, String action)
97    {
98  26 open(getUrl(space, page, action));
99    }
100   
 
101  399 toggle public void open(String space, String page, String action, String queryString)
102    {
103  399 open(getUrl(space, page, action, queryString));
104    }
105   
 
106  1 toggle public String getTitle()
107    {
108  1 return getSelenium().getTitle();
109    }
110   
 
111  0 toggle public void assertPage(String space, String page)
112    {
113  0 Assert.assertTrue(getTitle().matches(".*\\(" + space + "." + page + "\\) - XWiki"));
114    }
115   
116    /**
117    * Visits the specified page and checks if it exists, coming back to the current page.
118    *
119    * @param space the space name
120    * @param page the page name
121    * @return {@code true} if the specified page exists
122    */
 
123  7 toggle public boolean isExistingPage(String space, String page)
124    {
125  7 String saveUrl = getSelenium().getLocation();
126   
127  7 open(getUrl(space, page));
128  7 boolean exists = isExistingPage();
129   
130    // Restore original URL
131  7 open(saveUrl);
132   
133  7 return exists;
134    }
135   
136    /**
137    * @return {@code true} if we are on an existing page, {@code false} otherwise
138    */
 
139  26 toggle public boolean isExistingPage()
140    {
141  26 return !getSelenium().isTextPresent("The requested page could not be found.");
142    }
143   
 
144  0 toggle public void assertTitle(String title)
145    {
146  0 Assert.assertEquals(title, getTitle());
147    }
148   
 
149  658 toggle public boolean isElementPresent(String locator)
150    {
151  658 return getSelenium().isElementPresent(locator);
152    }
153   
 
154  422 toggle public boolean isElementPresentWithoutWaiting(By by)
155    {
156  422 return getDriver().hasElementWithoutWaiting(by);
157    }
158   
 
159  0 toggle public boolean isLinkPresent(String text)
160    {
161  0 return isElementPresent("link=" + text);
162    }
163   
 
164  25 toggle public void clickLinkWithText(String text)
165    {
166  25 clickLinkWithText(text, true);
167    }
168   
 
169  27 toggle public void assertTextPresent(String text)
170    {
171  27 Test failure here Assert.assertTrue("[" + text + "] isn't present.", getSelenium().isTextPresent(text));
172    }
173   
 
174  9 toggle public void assertTextNotPresent(String text)
175    {
176  9 Assert.assertFalse("[" + text + "] is present.", getSelenium().isTextPresent(text));
177    }
178   
 
179  576 toggle public void assertElementPresent(String elementLocator)
180    {
181  576 Assert.assertTrue("[" + elementLocator + "] isn't present.", isElementPresent(elementLocator));
182    }
183   
 
184  43 toggle public void assertElementNotPresent(String elementLocator)
185    {
186  43 Test failure here Assert.assertFalse("[" + elementLocator + "] is present.", isElementPresent(elementLocator));
187    }
188   
 
189  254 toggle public void waitPage()
190    {
191  254 waitPage(WAIT_TIME);
192    }
193   
194    /**
195    * @deprecated use {@link #waitPage()} instead
196    */
 
197  254 toggle @Deprecated
198    public void waitPage(int nbMillisecond)
199    {
200  254 getSelenium().waitForPageToLoad(String.valueOf(nbMillisecond));
201    }
202   
 
203  2 toggle public void createPage(String space, String page, String content)
204    {
205  2 createPage(space, page, content, null);
206    }
207   
 
208  14 toggle public void createPage(String space, String page, String content, String syntax)
209    {
210    // If the page already exists, delete it first
211  14 deletePage(space, page);
212  14 if (syntax == null) {
213  2 editInWikiEditor(space, page);
214    } else {
215  12 editInWikiEditor(space, page, syntax);
216    }
217  14 setFieldValue("content", content);
218  14 clickEditSaveAndView();
219    }
220   
 
221  24 toggle public void deletePage(String space, String page)
222    {
223  24 open(space, page, "delete", "confirm=1");
224    }
225   
 
226  1 toggle public void restorePage(String space, String page)
227    {
228  1 open(space, page, "view");
229  1 if (getSelenium().isTextPresent("Restore")) {
230  1 clickLinkWithText("Restore", true);
231    }
232    }
233   
 
234  195 toggle public void clickLinkWithLocator(String locator)
235    {
236  195 clickLinkWithLocator(locator, true);
237    }
238   
 
239  388 toggle public void clickLinkWithLocator(String locator, boolean wait)
240    {
241  388 assertElementPresent(locator);
242  388 getSelenium().click(locator);
243  388 if (wait) {
244  242 waitPage();
245    }
246    }
247   
 
248  34 toggle public void clickLinkWithText(String text, boolean wait)
249    {
250  34 clickLinkWithLocator("link=" + text, wait);
251    }
252   
 
253  1 toggle public boolean isChecked(String locator)
254    {
255  1 return getSelenium().isChecked(locator);
256    }
257   
 
258  38 toggle public String getFieldValue(String fieldName)
259    {
260    // Note: We could use getSelenium().getvalue() here. However getValue() is stripping spaces
261    // and some of our tests verify that there are leading spaces/empty lines.
262  38 return getSelenium().getEval(
263    "selenium.browserbot.getCurrentWindow().document.getElementById(\"" + fieldName + "\").value");
264    }
265   
 
266  275 toggle public void setFieldValue(String fieldName, String value)
267    {
268  275 getSelenium().type(fieldName, value);
269    }
270   
 
271  4 toggle public void checkField(String locator)
272    {
273  4 getSelenium().check(locator);
274    }
275   
 
276  13 toggle public void submit()
277    {
278  13 clickLinkWithXPath("//input[@type='submit']");
279    }
280   
 
281  93 toggle public void submit(String locator)
282    {
283  93 clickLinkWithLocator(locator);
284    }
285   
 
286  87 toggle public void submit(String locator, boolean wait)
287    {
288  87 clickLinkWithLocator(locator, wait);
289    }
290   
 
291  15 toggle public void clickLinkWithXPath(String xpath)
292    {
293  15 clickLinkWithXPath(xpath, true);
294    }
295   
 
296  58 toggle public void clickLinkWithXPath(String xpath, boolean wait)
297    {
298  58 clickLinkWithLocator("xpath=" + xpath, wait);
299    }
300   
 
301  64 toggle public void waitForCondition(String condition)
302    {
303  64 getSelenium().waitForCondition(condition, "" + WAIT_TIME);
304    }
305   
 
306  10 toggle public void waitForTextPresent(final String elementLocator, final String expectedValue)
307    {
308  10 new Wait()
309    {
 
310  20 toggle public boolean until()
311    {
312  20 return getSelenium().getText(elementLocator).equals(expectedValue);
313    }
314  10 }.wait(getSelenium().isElementPresent(elementLocator) ? "Element [" + elementLocator + "] not found"
315    : "Element [" + elementLocator + "] found but it doesn't have the expected value [" + expectedValue + "]");
316    }
317   
 
318  15 toggle public void waitForTextContains(final String elementLocator, final String containsValue)
319    {
320  15 new Wait()
321    {
 
322  19 toggle public boolean until()
323    {
324  19 return getSelenium().getText(elementLocator).indexOf(containsValue) > -1;
325    }
326  15 }.wait(getSelenium().isElementPresent(elementLocator) ? "Element [" + elementLocator + "] not found"
327    : "Element [" + elementLocator + "] found but it doesn't contain the expected value [" + containsValue
328    + "]");
329    }
330   
 
331  19 toggle public void waitForBodyContains(final String containsValue)
332    {
333  19 new Wait()
334    {
 
335  19 toggle public boolean until()
336    {
337  19 try {
338  19 return getSelenium().getBodyText().indexOf(containsValue) > -1;
339    } catch (SeleniumException e) {
340    // The page might not be loaded yet and so the BODY element is missing. Try again later.
341  0 return false;
342    }
343    }
344    }.wait("Body text doesn't contain the value [" + containsValue + "]");
345    }
346   
 
347  239 toggle public void waitForElement(final String elementLocator)
348    {
349  239 new Wait()
350    {
 
351  244 toggle public boolean until()
352    {
353  244 return getSelenium().isElementPresent(elementLocator);
354    }
355    }.wait("element [" + elementLocator + "] not found");
356    }
357   
358    /**
359    * Waits until an alert message appears or the timeout expires. You can use {@link Selenium#getAlert()} to assert
360    * the alert message afterwards.
361    */
 
362  1 toggle public void waitForAlert()
363    {
364  1 new Wait()
365    {
 
366  2 toggle public boolean until()
367    {
368  2 return getSelenium().isAlertPresent();
369    }
370    }.wait("The alert didn't appear.");
371    }
372   
373    /**
374    * Waits until a confirmation message appears or the timeout expires. You can use {@link Selenium#getConfirmation()}
375    * to assert the confirmation message afterwards.
376    */
 
377  6 toggle public void waitForConfirmation()
378    {
379  6 new Wait()
380    {
 
381  6 toggle public boolean until()
382    {
383  6 return getSelenium().isConfirmationPresent();
384    }
385    }.wait("The confirmation didn't appear.");
386    }
387   
388    /**
389    * Waits for a notification message of the specified type with the given message to be displayed.
390    *
391    * @param level the notification type (one of error, warning, done)
392    * @param message the notification message
393    */
 
394  99 toggle private void waitForNotificationMessage(String level, String message)
395    {
396  99 String xpath = String.format("//div[contains(@class,'xnotification-%s') and contains(.,'%s')]", level, message);
397  99 waitForElement(xpath);
398    // In order to improve test speed, clicking on the notification will make it disappear. This also ensures that
399    // this method always waits for the last notification message of the specified level.
400  99 try {
401    // The notification message may disappear before we get to click on it.
402  99 getSelenium().click(xpath);
403    } catch (Exception e) {
404    // Ignore.
405    }
406    }
407   
 
408  0 toggle public void waitForNotificationErrorMessage(String message)
409    {
410  0 waitForNotificationMessage("error", message);
411    }
412   
 
413  0 toggle public void waitForNotificationWarningMessage(String message)
414    {
415  0 waitForNotificationMessage("warning", message);
416    }
417   
 
418  99 toggle public void waitForNotificationSuccessMessage(String message)
419    {
420  99 waitForNotificationMessage("done", message);
421    }
422   
 
423  55 toggle public void clickButtonAndContinue(String locator)
424    {
425  55 submit(locator, false);
426  55 waitForNotificationSuccessMessage("");
427    }
428   
 
429  0 toggle @Override
430    public void clickEditPage()
431    {
432  0 getSkinExecutor().clickEditPage();
433    }
434   
 
435  11 toggle @Override
436    public void clickEditPageInWikiSyntaxEditor()
437    {
438  11 getSkinExecutor().clickEditPageInWikiSyntaxEditor();
439    }
440   
 
441  6 toggle @Override
442    public void clickEditPageInWysiwyg()
443    {
444  6 getSkinExecutor().clickEditPageInWysiwyg();
445    }
446   
 
447  1 toggle @Override
448    public void clickEditPageAccessRights()
449    {
450  1 getSkinExecutor().clickEditPageAccessRights();
451    }
452   
 
453  1 toggle @Override
454    public void clickEditPageInlineForm()
455    {
456  1 getSkinExecutor().clickEditPageInlineForm();
457    }
458   
 
459  0 toggle @Override
460    public void clickDeletePage()
461    {
462  0 getSkinExecutor().clickDeletePage();
463    }
464   
 
465  0 toggle @Override
466    public void clickCopyPage()
467    {
468  0 getSkinExecutor().clickCopyPage();
469    }
470   
 
471  1 toggle @Override
472    public void clickShowComments()
473    {
474  1 getSkinExecutor().clickShowComments();
475    }
476   
 
477  1 toggle @Override
478    public void clickShowAttachments()
479    {
480  1 getSkinExecutor().clickShowAttachments();
481    }
482   
 
483  1 toggle @Override
484    public void clickShowHistory()
485    {
486  1 getSkinExecutor().clickShowHistory();
487    }
488   
 
489  1 toggle @Override
490    public void clickShowInformation()
491    {
492  1 getSkinExecutor().clickShowInformation();
493    }
494   
 
495  3 toggle @Override
496    public void clickEditPreview()
497    {
498  3 getSkinExecutor().clickEditPreview();
499    }
500   
 
501  34 toggle @Override
502    public void clickEditSaveAndContinue()
503    {
504  34 getSkinExecutor().clickEditSaveAndContinue();
505    }
506   
 
507  5 toggle @Override
508    public void clickEditCancelEdition()
509    {
510  5 getSkinExecutor().clickEditCancelEdition();
511    }
512   
 
513  78 toggle @Override
514    public void clickEditSaveAndView()
515    {
516  78 getSkinExecutor().clickEditSaveAndView();
517    }
518   
519    /**
520    * Clicks on the add property button in the class editor. As a result the specified property is added to the edited
521    * class and the class is saved. This method waits for the class to be saved.
522    */
 
523  2 toggle @Override
524    public void clickEditAddProperty()
525    {
526  2 getSkinExecutor().clickEditAddProperty();
527    }
528   
529    /**
530    * Clicks on the add object button in the object editor. As a result an object of the specified class is added to
531    * the edited document and the document is saved. This method waits for the document to be saved.
532    */
 
533  4 toggle @Override
534    public void clickEditAddObject()
535    {
536  4 getSkinExecutor().clickEditAddObject();
537    }
538   
 
539  3 toggle @Override
540    public boolean isAuthenticated()
541    {
542  3 return getSkinExecutor().isAuthenticated();
543    }
544   
 
545  4 toggle @Override
546    public boolean isAuthenticated(String username)
547    {
548  4 return getSkinExecutor().isAuthenticated(username);
549    }
550   
 
551  4 toggle @Override
552    public boolean isAuthenticationMenuPresent()
553    {
554  4 return getSkinExecutor().isAuthenticationMenuPresent();
555    }
556   
 
557  3 toggle @Override