1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui.po.editor

File DatePicker.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
33
14
1
198
99
18
0.55
2.36
14
1.29

Classes

Class Line # Actions
DatePicker 39 33 0% 18 9
0.830188783%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui.po.editor;
21   
22    import java.util.List;
23   
24    import org.openqa.selenium.By;
25    import org.openqa.selenium.NotFoundException;
26    import org.openqa.selenium.WebDriver;
27    import org.openqa.selenium.WebElement;
28    import org.openqa.selenium.support.FindBy;
29    import org.openqa.selenium.support.ui.ExpectedCondition;
30    import org.openqa.selenium.support.ui.Select;
31    import org.xwiki.test.ui.po.BaseElement;
32   
33    /**
34    * Represents the actions possible on the date picker.
35    *
36    * @version $Id: cb10ad907611e6f7c93d048c3c92c3c22e7bc5d5 $
37    * @since 4.2M1
38    */
 
39    public class DatePicker extends BaseElement
40    {
41    /**
42    * The element wrapping the date picker.
43    */
44    @FindBy(className = "calendar_date_select")
45    private WebElement container;
46   
 
47  6 toggle public DatePicker()
48    {
49  6 this.waitToLoad();
50    }
51   
52    /**
53    * Selects the specified year.
54    *
55    * @param year the year to select
56    */
 
57  1 toggle public void setYear(String year)
58    {
59  1 Select yearSelector = new Select(container.findElement(By.className("year")));
60  1 yearSelector.selectByVisibleText(year);
61    }
62   
63    /**
64    * @return the selected year
65    */
 
66  2 toggle public String getYear()
67    {
68  2 Select yearSelector = new Select(container.findElement(By.className("year")));
69  2 return yearSelector.getFirstSelectedOption().getText();
70    }
71   
72    /**
73    * Selects the specified month.
74    *
75    * @param month the month to select
76    */
 
77  1 toggle public void setMonth(String month)
78    {
79  1 Select monthSelector = new Select(container.findElement(By.className("month")));
80  1 monthSelector.selectByVisibleText(month);
81    }
82   
83    /**
84    * @return the selected month
85    */
 
86  2 toggle public String getMonth()
87    {
88  2 Select monthSelector = new Select(container.findElement(By.className("month")));
89  2 return monthSelector.getFirstSelectedOption().getText();
90    }
91   
92    /**
93    * Selects the specified day from the current month.
94    *
95    * @param day the day to select
96    */
 
97  3 toggle public void setDay(String day)
98    {
99  3 container.findElement(By.xpath("//*[@class = 'cds_body']//tbody//div[. = '" + day + "' and not(@class)]"))
100    .click();
101    }
102   
103    /**
104    * @return the selected day, if any, otherwise {@code null}
105    */
 
106  3 toggle public String getDay()
107    {
108  3 String xpath = "//*[@class = 'cds_body']//tbody//*[contains(@class, 'selected')]";
109  3 List<WebElement> selected = container.findElements(By.xpath(xpath));
110  3 if (selected.size() == 1) {
111  3 return selected.get(0).getText();
112    }
113  0 return null;
114    }
115   
116    /**
117    * Selects the specified hour.
118    *
119    * @param hour the hour to select
120    */
 
121  1 toggle public void setHour(String hour)
122    {
123  1 Select hourSelector = new Select(container.findElement(By.className("hour")));
124  1 hourSelector.selectByVisibleText(hour);
125    }
126   
127    /**
128    * @return the selected hour
129    */
 
130  1 toggle public String getHour()
131    {
132  1 Select hourSelector = new Select(container.findElement(By.className("hour")));
133  1 return hourSelector.getFirstSelectedOption().getText();
134    }
135   
136    /**
137    * Selects the specified minute.
138    *
139    * @param minute the minute to select
140    */
 
141  1 toggle public void setMinute(String minute)
142    {
143  1 Select minuteSelector = new Select(container.findElement(By.className("minute")));
144  1 if (minuteSelector.getFirstSelectedOption().getText().equals(minute)) {
145    // The specified minute is already selected but that doesn't mean the date text input has exactly the
146    // specified minute. The minute selector has only multiples of 5 so you can have '15' selected while the
147    // actual minutes in the date input is '17'. The date picker rounds down the actual minutes to the closest
148    // multiple of 5 before initializing the minute selector. We need to force a selection change event in order
149    // to update the date text input with the specified minute.
150  0 minuteSelector.selectByIndex(0);
151  0 if (minuteSelector.getFirstSelectedOption().getText().equals(minute)) {
152  0 minuteSelector.selectByIndex(1);
153    }
154    }
155  1 minuteSelector.selectByVisibleText(minute);
156    }
157   
158    /**
159    * @return the selected minute
160    */
 
161  1 toggle public String getMinute()
162    {
163  1 Select minuteSelector = new Select(container.findElement(By.className("minute")));
164  1 return minuteSelector.getFirstSelectedOption().getText();
165    }
166   
167    /**
168    * @return {@code true} if the date picker allows you to select the hour, {@code false} otherwise
169    */
 
170  2 toggle public boolean hasHourSelector()
171    {
172  2 return getDriver().findElementsWithoutWaiting(container, By.className("hour")).size() > 0;
173    }
174   
175    /**
176    * Waits for the DatePicker popup to load.
177    *
178    * @since 6.3M2
179    */
 
180  6 toggle public DatePicker waitToLoad()
181    {
182  6 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
183    {
 
184  6 toggle @Override
185    public Boolean apply(WebDriver driver)
186    {
187  6 try {
188    // Since container is a proxy WebElement, any method on it would call findElement internally. We
189    // chose isDisplayed since it is the most self-describing, even if it's practically useless.
190  6 return container.isDisplayed();
191    } catch (NotFoundException e) {
192  0 return false;
193    }
194    }
195    });
196  6 return this;
197    }
198    }