View Javadoc

1   package com.atlassian.webdriver.jira.page.user;
2   
3   import com.atlassian.pageobjects.Page;
4   import com.atlassian.pageobjects.binder.Init;
5   import com.atlassian.webdriver.jira.page.JiraAdminAbstractPage;
6   import com.atlassian.webdriver.utils.Check;
7   import com.atlassian.webdriver.utils.by.ByJquery;
8   import org.openqa.selenium.WebElement;
9   import org.openqa.selenium.support.FindBy;
10  
11  import java.util.HashSet;
12  import java.util.Set;
13  
14  /**
15   * Page object implementation for the edit user's group page in JIRA. 
16   *
17   * @since 2.0
18   */
19  public class  EditUserGroupsPage extends JiraAdminAbstractPage
20  {
21  
22      private static final String URI = "/secure/admin/user/EditUserGroups.jspa";
23      private static String ERROR_SELECTOR = ".formErrors ul li";
24  
25      private Set<String> errors = new HashSet<String>();
26  
27      @FindBy (id = "return_link")
28      private WebElement returnLink;
29  
30      @FindBy (name = "join")
31      private WebElement joinButton;
32  
33      @FindBy (name = "leave")
34      private WebElement leaveButton;
35  
36      @FindBy (name = "groupsToJoin")
37      private WebElement groupsToJoinSelect;
38  
39      @FindBy (name = "groupsToLeave")
40      private WebElement groupsToLeaveSelect;
41  
42      @FindBy (name = "jiraform")
43      private WebElement editGroupsForm;
44  
45      public String getUrl()
46      {
47          return URI;
48      }
49  
50      @Init
51      public void parsePage()
52      {
53  
54          if (Check.elementExists(ByJquery.$(ERROR_SELECTOR), driver))
55          {
56              for (WebElement el : driver.findElements(ByJquery.$(ERROR_SELECTOR)))
57              {
58                  errors.add(el.getText());
59              }
60          }
61  
62  
63      }
64  
65      public boolean hasErrors()
66      {
67          return !errors.isEmpty();
68      }
69  
70      public boolean hasError(String errorStr)
71      {
72          return errors.contains(errorStr);
73      }
74  
75      public ViewUserPage returnToUserView()
76      {
77          returnLink.click();
78  
79          return pageBinder.bind(ViewUserPage.class);
80      }
81  
82      /**
83       * Add to groups either redirects the user to another page or returns the user to
84       * the EditUserGroupsPage.
85       * @param groups
86       * @return
87       */
88      public <T extends Page> T addToGroupsAndReturnToPage(Class<T> pageClass, String ... groups)
89      {
90          selectGroups(groupsToJoinSelect, groups);
91  
92          joinButton.click();
93  
94          return pageBinder.bind(pageClass);
95      }
96  
97      public EditUserGroupsPage addToGroupsExpectingError(String ... groups)
98      {
99          return addToGroupsAndReturnToPage(EditUserGroupsPage.class, groups);
100     }
101 
102     public <T extends Page> T removeFromGroupsAndReturnToPage(Class<T> pageClass, String ... groups)
103     {
104         selectGroups(groupsToLeaveSelect, groups);
105 
106         leaveButton.click();
107 
108         return pageBinder.bind(pageClass);
109     }
110 
111     public EditUserGroupsPage removeFromGroupsExpectingError(String ... groups)
112     {
113         return removeFromGroupsAndReturnToPage(EditUserGroupsPage.class, groups);
114     }
115 
116     private void selectGroups(WebElement select, String ... groups)
117     {
118         for (String group : groups)
119         {
120             String groupSelector = "option[value=" + group + "]";
121 
122             if (Check.elementExists(ByJquery.$(groupSelector), select))
123             {
124                 WebElement option = select.findElement(ByJquery.$(groupSelector));
125                 if (!option.isSelected())
126                 {
127                     option.click();
128                 }
129             }
130         }
131 
132     }
133 }