View Javadoc

1   package org.itracker.selenium;
2   
3   import com.dumbster.smtp.SimpleSmtpServer;
4   import com.dumbster.smtp.SmtpMessage;
5   import org.junit.Test;
6   import org.openqa.selenium.WebDriver;
7   
8   import java.util.Timer;
9   
10  /**
11   * Verifies the ability retrieve/reset forgotten password.
12   *
13   * @author Andrey Sergievskiy <seas@andreysergievskiy.com>
14   */
15  public class ForgotPasswordTest extends AbstractSeleniumTestCase {
16      /**
17       * 1. Go to the Login Page.
18       * 2. Leave all input fields (login and password) empty.
19       * 3. Click Login button.
20       * 4. Wait for page reload.
21       * 5. Check that "Login is required" and "Last Name is required"
22       * message appeared.
23       *
24       * @throws java.lang.Exception
25       */
26      @Test
27      public void testIfBothRequired() throws Exception {
28          log.info("running testIfBothRequired");
29          SeleniumManager.closeSession(selenium);
30          selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
31                  + applicationPath);
32  
33          selenium.click("name=forgotpassword");//("link=Forgot My Password");
34          selenium.waitForPageToLoad(SE_TIMEOUT);
35  
36          assertTrue(selenium.isElementPresent("login"));
37          assertTrue(selenium.isElementPresent("lastName"));
38          selenium.click("//input[@type='submit']");
39          selenium.waitForPageToLoad(SE_TIMEOUT);
40  
41          assertEquals("Login is required\n Last Name is required",
42                  selenium.getText("//span[@class='formError']"));
43      }
44  
45      /**
46       * 1. Go to the Login Page.
47       * 2. Type something into Last Name input field but leave Login empty.
48       * 3. Click Login button.
49       * 4. Wait for page reload.
50       * 5. Check that "Login is required" message has appeared.
51       *
52       * @throws java.lang.Exception
53       */
54      @Test
55      public void testIfLoginRequired() throws Exception {
56          log.info("running testIfLoginRequired");
57          SeleniumManager.closeSession(selenium);
58          selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
59                  + applicationPath);
60  
61          selenium.click("name=forgotpassword");//("link=Forgot My Password");
62          selenium.waitForPageToLoad(SE_TIMEOUT);
63  
64          assertTrue(selenium.isElementPresent("login"));
65          assertTrue(selenium.isElementPresent("lastName"));
66          selenium.type("lastName", "user");
67          selenium.click("//input[@type='submit']");
68          selenium.waitForPageToLoad(SE_TIMEOUT);
69  
70          assertEquals("Login is required", selenium.getText("//span[@class='formError']"));
71      }
72  
73      /**
74       * 1. Go to the Login Page.
75       * 2. Type something into Login input field but leave Last Name empty.
76       * 3. Click Login button.
77       * 4. Wait for page reload.
78       * 5. Check that "Last Name is required" message has appeared.
79       *
80       * @throws java.lang.Exception
81       */
82      @Test
83      public void testIfLastNameRequired() throws Exception {
84          SeleniumManager.closeSession(selenium);
85          selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
86                  + applicationPath);
87  
88          selenium.click("name=forgotpassword");//("link=Forgot My Password");
89          selenium.waitForPageToLoad(SE_TIMEOUT);
90  
91          selenium.type("login", "user");
92          selenium.click("//input[@type='submit']");
93          selenium.waitForPageToLoad(SE_TIMEOUT);
94  
95          assertEquals("Last Name is required", selenium.getText("//span[@class='formError']"));
96      }
97  
98      /**
99       * 1. Go to the Login Page.
100      * 2. Click "Forgot Password" link.
101      * 3. Wait for page reload.
102      * 4. Type valid login into Login input field.
103      * 5. Type valid last name into Last Name input field.
104      * 6. Click "Submit" button.
105      * 7. Wait for page reload.
106      * 8. Check that you was forwarded back to the Login Page.
107      * 9. Obtain email message from locally started mail server.
108      * 10. Extract a new password from there.
109      * 11. Redo a usual login procedure with a new password and make sure
110      * you can authorize to the system with it.
111      *
112      * @throws java.lang.Exception
113      */
114     @Test
115     public void testRetrievingForgottenPassword() throws Exception {
116         log.info("running testRetrievingForgottenPassword");
117         SeleniumManager.closeSession(selenium);
118         selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
119                 + applicationPath);
120 
121         final String newPassword;
122 
123 //        startSMTP();
124 //        try {
125             assertElementPresent("name=forgotpassword");
126             selenium.click("name=forgotpassword");//("link=Forgot My Password");
127             selenium.waitForPageToLoad(SE_TIMEOUT);
128             assertElementPresent("login");
129             selenium.type("login", "user_test1");
130             assertElementPresent("lastName");
131             selenium.type("lastName", "user lastname");
132 
133             assertElementPresent("//input[@type='submit']");
134 //            assertEquals("smtpServer.receivedEmailSize", 0, smtpServer.getReceivedEmailSize());
135             selenium.click("//input[@type='submit']");
136             selenium.waitForPageToLoad(SE_TIMEOUT);
137 //            try {
138 //                Thread.currentThread().sleep(1000);
139 //            } catch (InterruptedException e) {
140 //                fail("Interrupted: " + e.getMessage());
141 //            }
142             assertElementPresent("login");
143             assertElementPresent("password");
144 //            assertEquals("smtpServer.receivedEmailSize", 1, smtpServer.getReceivedEmailSize());
145 //            final SmtpMessage smtpMessage = (SmtpMessage) smtpServer.getReceivedEmail().next();
146 //            final String smtpMessageBody = smtpMessage.getBody();
147 //            assertTrue(smtpMessageBody.contains("Password: "));
148 //            newPassword = smtpMessageBody
149 //                    .replaceAll("\n", "").replaceFirst(".*Password: ", "");
150 //
151 //        } finally {
152 //            stopSMTP();
153 //        }
154 
155 //        assertNotNull("null password from mail", newPassword);
156         SeleniumManager.closeSession(selenium);
157         selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
158                 + applicationPath);
159         assertElementPresent("login");
160         selenium.type("login", "user_test1");
161         assertElementPresent("password");
162 //        selenium.type("password", newPassword);
163 //        assertElementPresent("//input[@type='submit']");
164 //        selenium.click("//input[@type='submit']");
165 //        selenium.waitForPageToLoad(SE_TIMEOUT);
166 //        assertElementPresent("id");
167     }
168 
169     @Override
170     protected String[] getDataSetFiles() {
171         return new String[]{
172                 "dataset/languagebean_init_dataset.xml",
173                 "dataset/languagebean_dataset.xml",
174                 "dataset/userpreferencesbean_dataset.xml",
175                 "dataset/userbean_dataset.xml"
176         };
177     }
178 
179     @Override
180     protected String[] getConfigLocations() {
181         return new String[]{"application-context.xml"};
182     }
183 }