aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/pageobjects/Navigation.java
blob: 8342474a41796b5aa180482b0240b2ff467c7acd (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package pageobjects;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import com.sonar.orchestrator.Orchestrator;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
import org.openqa.selenium.By;
import pageobjects.licenses.LicensesPage;
import pageobjects.settings.SettingsPage;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.page;

public class Navigation extends ExternalResource {

  public static Navigation get(Orchestrator orchestrator) {
    String browser = orchestrator.getConfiguration().getString("orchestrator.browser", "firefox");
    SelenideConfig.INSTANCE
      .setBrowser(browser)
      .setBaseUrl(orchestrator.getServer().getUrl());
    return new Navigation();
  }

  @Override
  protected void before() throws Throwable {
    WebDriverRunner.getWebDriver().manage().deleteAllCookies();
    openHomepage();
  }

  public Navigation openHomepage() {
    return open("/", Navigation.class);
  }

  public ProjectDashboardPage openProjectDashboard(String projectKey) {
    // TODO encode projectKey
    String url = "/dashboard?id=" + projectKey;
    return open(url, ProjectDashboardPage.class);
  }

  public ProjectLinksPage openProjectLinks(String projectKey) {
    // TODO encode projectKey
    String url = "/project/links?id=" + projectKey;
    return open(url, ProjectLinksPage.class);
  }

  public ProjectQualityGatePage openProjectQualityGate(String projectKey) {
    // TODO encode projectKey
    String url = "/project/quality_gate?id=" + projectKey;
    return open(url, ProjectQualityGatePage.class);
  }

  public ProjectHistoryPage openProjectHistory(String projectKey) {
    // TODO encode projectKey
    String url = "/project/history?id=" + projectKey;
    return open(url, ProjectHistoryPage.class);
  }

  public ProjectKeyPage openProjectKey(String projectKey) {
    // TODO encode projectKey
    String url = "/project/key?id=" + projectKey;
    return open(url, ProjectKeyPage.class);
  }

  public BackgroundTasksPage openBackgroundTasksPage() {
    return open("/background_tasks", BackgroundTasksPage.class);
  }

  public SettingsPage openSettings(@Nullable String projectKey) throws UnsupportedEncodingException {
    String url = projectKey != null ?
      "/project/settings?id=" + URLEncoder.encode(projectKey, "UTF-8") :
      "/settings";
    return open(url, SettingsPage.class);
  }

  public LicensesPage openLicenses() {
    return open("/settings/licenses", LicensesPage.class);
  }

  public EncryptionPage openEncryption() {
    return open("/settings/encryption", EncryptionPage.class);
  }

  public ServerIdPage openServerId() {
    return open("/settings/server_id", ServerIdPage.class);
  }

  public MyActivityPage openMyActivity() {
    return open("/account", MyActivityPage.class);
  }

  public void open(String relativeUrl) {
    Selenide.open(relativeUrl);
  }

  public <P> P open(String relativeUrl, Class<P> pageObjectClassClass) {
    return Selenide.open(relativeUrl, pageObjectClassClass);
  }

  public void shouldBeLoggedIn() {
    loggedInDropdown().should(Condition.visible);
  }

  public void shouldNotBeLoggedIn() {
    logInLink().should(Condition.visible);
  }

  public LoginPage logIn() {
    logInLink().click();
    return Selenide.page(LoginPage.class);
  }

  public Navigation logOut() {
    SelenideElement dropdown = loggedInDropdown();
    // click must be on the <a> but not on the dropdown <li>
    // for compatibility with phantomjs
    dropdown.find(".dropdown-toggle").click();
    dropdown.find(By.linkText("Log out")).click();
    return this;
  }

  public RulesPage clickOnRules() {
    $(By.linkText("Rules")).click();
    return page(RulesPage.class);
  }

  public SelenideElement clickOnQualityProfiles() {
    return $(By.linkText("Quality Profiles"));
  }

  public SelenideElement getRightBar() {
    return $("#global-navigation .navbar-right");
  }

  public SelenideElement getFooter() {
    return $("#footer");
  }

  public SelenideElement getErrorMessage() {
    return $("#error");
  }

  private SelenideElement logInLink() {
    return $(By.linkText("Log in"));
  }

  private SelenideElement loggedInDropdown() {
    return $(".js-user-authenticated");
  }

}