View Javadoc

1   package com.atlassian.webdriver.jira.page.user;
2   
3   import com.atlassian.pageobjects.binder.Init;
4   import com.atlassian.webdriver.jira.data.User;
5   import com.atlassian.webdriver.jira.page.JiraAdminAbstractPage;
6   import com.atlassian.webdriver.utils.by.ByJquery;
7   import com.atlassian.webdriver.utils.Check;
8   import org.openqa.selenium.By;
9   import org.openqa.selenium.WebElement;
10  import org.openqa.selenium.support.FindBy;
11  
12  import java.util.HashMap;
13  import java.util.HashSet;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Set;
17  
18  /**
19   * Page object implementation for the User browser page in JIRA.
20   * TODO: Handle pagination when there are more users
21   */
22  public class UserBrowserPage extends JiraAdminAbstractPage
23  {
24      private static final String URI = "/secure/admin/user/UserBrowser.jspa";
25  
26      private String MAX = "1000000";
27      private String TEN = "10";
28      private String TWENTY = "20";
29      private String FIFTY = "50";
30      private String ONE_HUNDRED = "100";
31  
32      private final Map<String, User> users;
33      private WebElement filterSubmit;
34      
35      @FindBy (id = "add_user")
36      private WebElement addUserLink;
37  
38      @FindBy (xpath = "/html/body/table/tbody/tr/td[3]/table/tbody/tr/td/form/table/tbody/tr[2]/td/p[3]/strong[3]")
39      private WebElement numUsers;
40  
41      @FindBy (name = "max")
42      private WebElement usersPerPageDropdown;
43  
44      @FindBy (id = "user_browser_table")
45      private WebElement userTable;
46  
47      public UserBrowserPage()
48      {
49          users = new HashMap<String, User>();
50      }
51  
52  
53      public String getUrl()
54      {
55          return URI;
56      }
57  
58      @Init
59      public void init()
60      {
61          filterSubmit = driver.findElement(By.cssSelector("form[name=\"jiraform\"] input[type=\"submit\"]"));
62  
63          setUserFilterToShowAllUsers();
64  
65          getUsers();
66      }
67  
68      public boolean hasUser(User user)
69      {
70          return users.containsKey(user.getUsername());
71      }
72  
73      /**
74       * When editing a users groups from this page, EditUserGroups always returns back to
75       * UserBrowser unless there was an error.
76       * @param user
77       * @return
78       */
79      public EditUserGroupsPage editUserGroups(User user)
80      {
81  
82          if (hasUser(user))
83          {
84              String editGroupsId = "editgroups_" + user.getUsername();
85  
86              driver.findElement(By.id(editGroupsId)).click();
87  
88              return pageBinder.bind(EditUserGroupsPage.class);
89          }
90          else
91          {
92              throw new IllegalStateException("User: " + user.getUsername() + " was not found.");
93          }
94  
95      }
96  
97      public Set<String> getUsersGroups(User user)
98      {
99  
100         if (hasUser(user))
101         {
102             Set<String> groups = new HashSet<String>();
103             WebElement groupCol = userTable.findElements(ByJquery.$("#" + user.getUsername()).parents("tr.vcard").find("td")).get(4);
104 
105             for (WebElement groupEl : groupCol.findElements(By.tagName("a")))
106             {
107                 groups.add(groupEl.getText());
108             }
109 
110             return groups;
111         }
112         else
113         {
114             throw new IllegalStateException("User: " + user.getUsername() + " was not found.");
115         }
116     }
117 
118     public ViewUserPage gotoViewUserPage(User user)
119     {
120         if (hasUser(user))
121         {
122             User actualUser = users.get(user.getUsername());
123             WebElement userEmailLink = driver.findElement(By.linkText(actualUser.getEmail()));
124             userEmailLink.click();
125 
126             return pageBinder.bind(ViewUserPage.class);
127         }
128         else
129         {
130             throw new IllegalStateException("The user: " + user + " was not found on the page");
131         }
132     }
133 
134     public int getNumberOfUsers()
135     {
136         return Integer.valueOf(numUsers.getText());
137     }
138 
139     public UserBrowserPage filterByUserName(String username)
140     {
141         driver.findElement(By.name("userNameFilter")).sendKeys(username);
142         filterSubmit.click();
143 
144         return pageBinder.bind(UserBrowserPage.class);
145     }
146 
147     /**
148      * navigates to the addUserPage by activating the add User link
149      * @return
150      */
151     public AddUserPage gotoAddUserPage()
152     {
153         addUserLink.click();
154 
155         return pageBinder.bind(AddUserPage.class);
156     }
157 
158     /**
159      * Takes User object and fills out the addUserPage form and creates the user.
160      * @param user the user to create
161      * @param sendPasswordEmail sets the send email tick box to on or off
162      * @return the user browser page which should have the new user added to the count.
163      */
164     public ViewUserPage addUser(User user, boolean sendPasswordEmail)
165     {
166 
167         AddUserPage addUserPage = gotoAddUserPage();
168 
169         return addUserPage.setUsername(user.getUsername())
170                 .setPassword(user.getPassword())
171                 .setConfirmPassword(user.getPassword())
172                 .setFullname(user.getFullName())
173                 .setEmail(user.getEmail())
174                 .sendPasswordEmail(sendPasswordEmail)
175                 .createUser();
176     }
177 
178     private void setUserFilterToShowAllUsers()
179     {
180         WebElement userOption = usersPerPageDropdown.findElement(By.cssSelector("option[value=\"" + MAX + "\"]"));
181         if (!userOption.isSelected())
182         {
183             userOption.click();
184         }
185         filterSubmit.click();
186     }
187 
188     private void getUsers()
189     {
190         users.clear();
191 
192         List<WebElement> rows = userTable.findElements(By.tagName("tr"));
193 
194         for (WebElement row : rows)
195         {
196             // Check it's not the headings (th) tags.
197             if (Check.elementExists(By.tagName("td"), row))
198             {
199                 List<WebElement> cols = row.findElements(By.tagName("td"));
200 
201                 String username = cols.get(0).getText();
202                 String email = cols.get(1).getText();
203                 String fullName = cols.get(2).getText();
204 
205 //                Set<Group> groups = new HashSet<Group>();
206 //
207 //                for (WebElement group : cols.get(4).findElements(By.tagName("a")))
208 //                {
209 //                    groups.add(new Group(group.getText()));
210 //                }
211 
212                 users.put(username, new User(username, fullName, email));
213             }
214         }
215 
216     }
217 }