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

File RegisterTest.java

 

Code metrics

10
67
15
1
238
173
20
0.3
4.47
15
1.33

Classes

Class Line # Actions
RegisterTest 42 67 0% 20 3
0.967391396.7%
 

Contributing tests

This file is covered by 21 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.ui;
21   
22    import java.util.List;
23   
24    import org.junit.Assert;
25    import org.junit.Before;
26    import org.junit.Test;
27    import org.openqa.selenium.By;
28    import org.openqa.selenium.WebElement;
29    import org.openqa.selenium.WebDriverException;
30    import org.xwiki.administration.test.po.AdministrationSectionPage;
31    import org.xwiki.test.ui.browser.IgnoreBrowser;
32    import org.xwiki.test.ui.browser.IgnoreBrowsers;
33    import org.xwiki.test.ui.po.AbstractRegistrationPage;
34    import org.xwiki.test.ui.po.RegistrationPage;
35   
36    /**
37    * Test the user registration feature.
38    *
39    * @version $Id: 578c75c75b777a3fdcaa9c9ad6541f29de530a23 $
40    * @since 2.3M1
41    */
 
42    public class RegisterTest extends AbstractTest
43    {
44    protected AbstractRegistrationPage registrationPage;
45   
 
46  21 toggle @Before
47    public void setUp() throws Exception
48    {
49  21 deleteUser("JohnSmith");
50  21 switchUser();
51  21 this.registrationPage = this.getRegistrationPage();
52   
53    // Switch LiveValidation on or off as needed.
54  21 int x = 0;
55  24 while (this.registrationPage.isLiveValidationEnabled() != useLiveValidation()) {
56  3 AdministrationSectionPage sectionPage = new AdministrationSectionPage("Registration");
57  3 getDriver().get(getUtil().getURLToLoginAsAdminAndGotoPage(sectionPage.getURL()));
58  3 getUtil().recacheSecretToken();
59  3 getUtil().setDefaultCredentials(TestUtils.ADMIN_CREDENTIALS);
60  3 getUtil().assertOnPage(sectionPage.getURL());
61  3 sectionPage.getForm().setFieldValue(By.name("XWiki.Registration_0_liveValidation_enabled"),
62    Boolean.valueOf(useLiveValidation()).toString());
63  3 sectionPage.clickSave();
64  3 if (x > 2) {
65  0 throw new WebDriverException("Unable to set useLiveValidation to " + useLiveValidation());
66    }
67  3 x++;
68  3 this.registrationPage = this.getRegistrationPage();
69    }
70   
71    // The prepareName javascript function is the cause of endless flickering
72    // since it trys to suggest a username every time the field is focused.
73  21 getDriver().executeJavascript("document.getElementById('xwikiname').onfocus = null;");
74   
75  21 this.registrationPage.fillInJohnSmithValues();
76    }
77   
78    /** Become the user needed for the test. Guest for RegisterTest. */
 
79  14 toggle protected void switchUser()
80    {
81    // Fast Logout.
82  14 getUtil().forceGuestUser();
83    }
84   
85    /** To put the registration page someplace else, subclass this class and change this method. */
 
86  16 toggle protected AbstractRegistrationPage getRegistrationPage()
87    {
88  16 return RegistrationPage.gotoPage();
89    }
90   
91    /** To test without javascript validation, subclass this class and change this method. */
 
92  28 toggle protected boolean useLiveValidation()
93    {
94  28 return true;
95    }
96   
 
97  3 toggle @Test
98    @IgnoreBrowsers({
99    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
100    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
101    })
102    public void testRegisterJohnSmith()
103    {
104  3 Assert.assertTrue(validateAndRegister());
105  3 tryToLogin("JohnSmith", "WeakPassword");
106    }
107   
 
108  3 toggle @Test
109    @IgnoreBrowsers({
110    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
111    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
112    })
113    public void testRegisterExistingUser()
114    {
115  3 registrationPage.fillRegisterForm(null, null, "Admin", null, null, null);
116    // Can't use validateAndRegister here because user existence is not checked by LiveValidation.
117  3 Assert.assertFalse(tryToRegister());
118  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("User already exists."));
119    }
120   
 
121  3 toggle @Test
122    @IgnoreBrowsers({
123    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
124    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
125    })
126    public void testRegisterPasswordTooShort()
127    {
128  3 this.registrationPage.fillRegisterForm(null, null, null, "short", "short", null);
129  3 Assert.assertFalse(validateAndRegister());
130  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("Please use a longer password."));
131    }
132   
 
133  3 toggle @Test
134    @IgnoreBrowsers({
135    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
136    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
137    })
138    public void testRegisterDifferentPasswords()
139    {
140  3 this.registrationPage.fillRegisterForm(null, null, null, null, "DifferentPassword", null);
141  3 Assert.assertFalse(validateAndRegister());
142  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("The passwords do not match."));
143    }
144   
 
145  3 toggle @Test
146    @IgnoreBrowsers({
147    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
148    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
149    })
150    public void testRegisterEmptyPassword()
151    {
152  3 this.registrationPage.fillRegisterForm(null, null, null, "", "", null);
153  3 Assert.assertFalse(validateAndRegister());
154  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("This field is required."));
155    }
156   
 
157  3 toggle @Test
158    @IgnoreBrowsers({
159    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
160    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
161    })
162    public void testRegisterEmptyUserName()
163    {
164    // A piece of javascript fills in the username with the first and last names so we will empty them.
165  3 this.registrationPage.fillRegisterForm("", "", "", null, null, null);
166  3 Assert.assertFalse(validateAndRegister());
167  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("This field is required."));
168    }
169   
 
170  3 toggle @Test
171    @IgnoreBrowsers({
172    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
173    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
174    })
175    public void testRegisterInvalidEmail()
176    {
177  3 this.registrationPage.fillRegisterForm(null, null, null, null, null, "not an email address");
178  3 Assert.assertFalse(validateAndRegister());
179  3 Assert.assertTrue(this.registrationPage.validationFailureMessagesInclude("Please enter a valid email address."));
180    }
181   
182    /**
183    * If LiveValidation is enabled then it will check that there are no failures with that. If no failures then hits
184    * register button, it then asserts that hitting the register button did not reveal any failures not caught by
185    * LiveValidation. If LiveValidation is disabled then just hits the register button.
186    */
 
187  18 toggle protected boolean validateAndRegister()
188    {
189  18 if (useLiveValidation()) {
190  12 this.registrationPage.triggerLiveValidation();
191  12 if (!this.registrationPage.getValidationFailureMessages().isEmpty()) {
192  10 return false;
193    }
194  2 boolean result = tryToRegister();
195   
196  2 Assert.assertTrue("LiveValidation did not show a failure message but clicking on the register button did.",
197    this.registrationPage.getValidationFailureMessages().isEmpty());
198   
199  2 return result;
200    }
201  6 return tryToRegister();
202    }
203   
 
204  9 toggle protected boolean tryToRegister()
205    {
206  9 this.registrationPage.clickRegister();
207   
208  9 List<WebElement> infos = getDriver().findElements(By.className("infomessage"));
209  9 for (WebElement info : infos) {
210  2 if (info.getText().contains("Registration successful.")) {
211  2 return true;
212    }
213    }
214  7 return false;
215    }
216   
217    /** Deletes specified user if it exists, leaves the driver on undefined page. **/
 
218  21 toggle private void deleteUser(final String userName) throws Exception
219    {
220  21 TestUtils.Session s = getUtil().getSession();
221  21 getUtil().forceGuestUser();
222  21 getDriver().get(getUtil().getURLToLoginAsAdminAndGotoPage(getUtil().getURLToNonExistentPage()));
223  21 getUtil().recacheSecretToken();
224  21 getUtil().setDefaultCredentials(TestUtils.ADMIN_CREDENTIALS);
225  21 getUtil().rest().deletePage("XWiki", userName);
226  21 getUtil().setSession(s);
227    }
228   
 
229  3 toggle protected void tryToLogin(String username, String password)
230    {
231    // Fast logout.
232  3 getUtil().forceGuestUser();
233  3 getDriver().get(getUtil().getURLToLoginAs(username, password));
234  3 Assert.assertTrue(this.registrationPage.isAuthenticated());
235  3 getUtil().recacheSecretToken();
236  3 getUtil().setDefaultCredentials(username, password);
237    }
238    }