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

File BrowserTestRule.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
17
3
1
108
53
8
0.47
5.67
3
2.67

Classes

Class Line # Actions
BrowserTestRule 56 17 0% 8 2
0.923076992.3%
 

Contributing tests

No tests hitting this source file were found.

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.browser;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.regex.Pattern;
26   
27    import org.junit.internal.AssumptionViolatedException;
28    import org.junit.rules.TestRule;
29    import org.junit.runner.Description;
30    import org.junit.runners.model.Statement;
31    import org.openqa.selenium.Capabilities;
32    import org.openqa.selenium.WebDriver;
33    import org.openqa.selenium.remote.RemoteWebDriver;
34   
35    /**
36    * Allows ignoring some tests for a given browser.
37    *
38    * <pre>
39    * public class MyTestClass
40    * {
41    * &#064;Rule
42    * public BrowserTestRule browseTestRule = new BrowserTestRule(getDriver());
43    *
44    * &#064;Test
45    * &#064;IgnoreBrowser(value = {"firefox"}, reason="some reason for ignoring the test...")
46    * public void myTest()
47    * {
48    * ...
49    * }
50    * }
51    * </pre>
52    *
53    * @version $Id: 0924c3fd58342a4c60e454e6e62fd74b21365fc9 $
54    * @since 3.5M1
55    */
 
56    public class BrowserTestRule implements TestRule
57    {
58    private String currentBrowserName;
59    private String currentBrowserVersion;
60   
 
61  577 toggle public BrowserTestRule(WebDriver driver)
62    {
63  577 Capabilities capability = ((RemoteWebDriver) driver).getCapabilities();
64    // We get the name of the current user Browser
65  577 this.currentBrowserName = capability.getBrowserName();
66    // We get the version of the current used Browser
67  577 this.currentBrowserVersion = capability.getVersion();
68    }
69   
 
70  577 toggle @Override
71    public Statement apply(final Statement base, final Description description)
72    {
73  577 return new Statement() {
 
74  577 toggle @Override
75    public void evaluate() throws Throwable
76    {
77    // The full List with ignored browsers, taken from both annotations
78  577 List<IgnoreBrowser> ignoredBrowsersList = new ArrayList<IgnoreBrowser>();
79   
80    // We check if there is a IgnoreBrowser annotation
81  577 IgnoreBrowser ignoreBrowser = description.getAnnotation(IgnoreBrowser.class);
82  577 if (ignoreBrowser != null) {
83  29 ignoredBrowsersList.add(ignoreBrowser);
84    }
85   
86    // We check if there is a IgnoreBrowsers annotation compound
87  577 IgnoreBrowsers ignoreBrowsers = description.getAnnotation(IgnoreBrowsers.class);
88  577 if (ignoreBrowsers != null) {
89  108 ignoredBrowsersList.addAll(Arrays.asList(ignoreBrowsers.value()));
90    }
91   
92    // We iterate through the list of BrowserIgnore annotations
93  577 for (IgnoreBrowser ignoredBrowser : ignoredBrowsersList) {
94  245 Pattern browserNamePattern = Pattern.compile(ignoredBrowser.value());
95  245 Pattern browserVersionPattern = Pattern.compile(ignoredBrowser.version());
96   
97  245 if (browserNamePattern.matcher(currentBrowserName).matches()
98    && (ignoredBrowser.version().isEmpty()
99    || browserVersionPattern.matcher(currentBrowserVersion).matches()))
100    {
101  0 throw new AssumptionViolatedException(ignoredBrowser.reason());
102    }
103    }
104  577 base.evaluate();
105    }
106    };
107    }
108    }