James Moger
2013-03-27 f6b200be4c8b90c26886c6cdd5809abac8c4ac15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2013 akquinet tech@spree GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.akquinet.devops.test.ui.view;
 
import java.util.List;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
 
/**
 * class representing the tabs you can access when you edit a repo.
 * 
 * @author saheba
 * 
 */
public class RepoEditView extends GitblitDashboardView {
 
    public static final String PERMISSION_VIEW_USERS_NAME_PREFIX = "users:";
    public static final String PERMISSION_VIEW_TEAMS_NAME_PREFIX = "teams:";
 
    public static final String PERMISSION_VIEW_MUTABLE = "permissionToggleForm:showMutable";
    public static final String PERMISSION_VIEW_SPECIFIED = "permissionToggleForm:showSpecified";
    public static final String PERMISSION_VIEW_EFFECTIVE = "permissionToggleForm:showEffective";
 
    public static final int RESTRICTION_ANONYMOUS_VCP = 0;
    public static final int RESTRICTION_AUTHENTICATED_P = 1;
    public static final int RESTRICTION_AUTHENTICATED_CP = 2;
    public static final int RESTRICTION_AUTHENTICATED_VCP = 3;
 
    public static final int AUTHCONTROL_RWALL = 0;
    public static final int AUTHOCONTROL_FINE = 1;
 
    public RepoEditView(WebDriver driver) {
        super(driver, null);
    }
 
    public void changeName(String newName) {
        String pathName = "//input[@id = \"name\" ]";
        WebElement field = getDriver().findElement(By.xpath(pathName));
        field.clear();
        field.sendKeys(newName);
    }
 
    public boolean navigateToPermissionsTab() {
        String linkText = "access permissions";
        List<WebElement> found = getDriver().findElements(
                By.partialLinkText(linkText));
        System.out.println("PERM TABS found =" + found.size());
        if (found != null && found.size() == 1) {
            found.get(0).click();
            return true;
        }
        return false;
    }
 
    private void changeOwners(String action,
            String affectedSelection, String username) {
        String xpath = "//select[@name=\"" + affectedSelection
                + "\"]/option[@value = \"" + username + "\" ]";
        WebElement option = getDriver().findElement(By.xpath(xpath));
        option.click();
        String buttonPath = "//button[@class=\"button " + action + "\"]";
        WebElement button = getDriver().findElement(By.xpath(buttonPath));
        button.click();
    }
 
    public void removeOwner(String username) {
        changeOwners("remove", "owners:selection",
                username);
    }
 
    public void addOwner(String username) {
        changeOwners("add", "owners:choices", username);
    }
 
    public WebElement getAccessRestrictionSelection() {
        String xpath = "//select[@name =\"accessRestriction\"]";
        List<WebElement> found = getDriver().findElements(By.xpath(xpath));
        if (found != null && found.size() == 1) {
            return found.get(0);
        }
        return null;
    }
 
    public boolean changeAccessRestriction(int option) {
        WebElement accessRestrictionSelection = getAccessRestrictionSelection();
        if (accessRestrictionSelection == null) {
            return false;
        }
        accessRestrictionSelection.click();
        sleep(100);
        String xpath = "//select[@name =\"accessRestriction\"]/option[@value=\""
                + option + "\"]";
        List<WebElement> found = getDriver().findElements(By.xpath(xpath));
        if (found == null || found.size() == 0 || found.size() > 1) {
            return false;
        }
        found.get(0).click();
        return true;
    }
 
    public boolean changeAuthorizationControl(int option) {
        System.out.println("try to change auth control");
        String xpath = "//input[@name =\"authorizationControl\" and @value=\""
                + option + "\"]";
        List<WebElement> found = getDriver().findElements(By.xpath(xpath));
        System.out.println("found auth CONTROL options " + found.size());
        if (found == null || found.size() == 0 || found.size() > 1) {
            return false;
        }
        found.get(0).click();
        return true;
    }
 
    private boolean isPermissionViewDisabled(String prefix, String view) {
        String xpath = "//[@name =\"" + prefix + view + "\"]";
        List<WebElement> found = getDriver().findElements(By.xpath(xpath));
        if (found == null || found.size() == 0 || found.size() > 1) {
            return false;
        }
        String attrValue = found.get(0).getAttribute("disabled");
        return (attrValue != null) && (attrValue.equals("disabled"));
    }
 
    public boolean isPermissionViewSectionDisabled(String prefix) {
        return isPermissionViewDisabled(prefix, PERMISSION_VIEW_MUTABLE)
                && isPermissionViewDisabled(prefix, PERMISSION_VIEW_SPECIFIED)
                && isPermissionViewDisabled(prefix, PERMISSION_VIEW_EFFECTIVE);
    }
 
    public boolean save() {
        String xpath = "//div[@class=\"form-actions\"]/input[@name =\""
                + "save" + "\"]";
        List<WebElement> found = getDriver().findElements(By.xpath(xpath));
        if (found == null || found.size() == 0 || found.size() > 1) {
            return false;
        }
        found.get(0).click();
        WebDriverWait webDriverWait = new WebDriverWait(getDriver(), 1);
        webDriverWait.until(new Exp.RepoListViewLoaded());
        return true;
    }
}