aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/tb3/TB3Runner.java
blob: 510d200ffaa0476e4ebc5b673b1d2b43dd643341 (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
/*
 * Copyright 2000-2013 Vaadin Ltd.
 * 
 * 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 com.vaadin.tests.tb3;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.vaadin.tests.tb3.AbstractTB3Test.BrowserUtil;

/**
 * This runner is loosely based on FactoryTestRunner by Ted Young
 * (http://tedyoung.me/2011/01/23/junit-runtime-tests-custom-runners/). The
 * generated test names give information about the parameters used (unlike
 * {@link Parameterized}).
 * 
 * @since 7.1
 */
public class TB3Runner extends BlockJUnit4ClassRunner {

    public TB3Runner(Class<?> klass) throws InitializationError {
        super(klass);
        setScheduler(new ParallelScheduler());
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> tests = new LinkedList<FrameworkMethod>();

        // Find all methods in our test class marked with @Parameters.
        for (FrameworkMethod method : getTestClass().getAnnotatedMethods(
                Parameters.class)) {
            // Make sure the Parameters method is static
            if (!Modifier.isStatic(method.getMethod().getModifiers())) {
                throw new IllegalArgumentException("@Parameters " + method
                        + " must be static.");
            }

            // Execute the method (statically)
            Object params;
            try {
                params = method.getMethod().invoke(
                        getTestClass().getJavaClass());
            } catch (Throwable t) {
                throw new RuntimeException("Could not run test factory method "
                        + method.getName(), t);
            }

            // Did the factory return an array? If so, make it a list.
            if (params.getClass().isArray()) {
                params = Arrays.asList((Object[]) params);
            }

            // Did the factory return a scalar object? If so, put it in a list.
            if (!(params instanceof Iterable<?>)) {
                params = Collections.singletonList(params);
            }

            // For each object returned by the factory.
            for (Object param : (Iterable<?>) params) {
                if (!(param instanceof DesiredCapabilities)) {
                    throw new RuntimeException("Unexpected parameter type "
                            + param.getClass().getName()
                            + " when expecting DesiredCapabilities");
                }
                DesiredCapabilities capabilities = (DesiredCapabilities) param;
                // Find any methods marked with @Test.
                for (FrameworkMethod m : getTestClass().getAnnotatedMethods(
                        Test.class)) {
                    tests.add(new TB3Method(m.getMethod(), capabilities));
                }
            }
        }

        return tests;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.junit.runners.BlockJUnit4ClassRunner#withBefores(org.junit.runners
     * .model.FrameworkMethod, java.lang.Object,
     * org.junit.runners.model.Statement)
     */
    @Override
    protected Statement withBefores(final FrameworkMethod method,
            final Object target, Statement statement) {
        if (!(method instanceof TB3Method)) {
            throw new RuntimeException("Unexpected method type "
                    + method.getClass().getName() + ", expected TB3Method");
        }
        final TB3Method tb3method = (TB3Method) method;

        // setDesiredCapabilities before running the real @Befores (which use
        // capabilities)

        final Statement realBefores = super.withBefores(method, target,
                statement);
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                ((AbstractTB3Test) target)
                        .setDesiredCapabilities(tb3method.capabilities);
                try {
                    realBefores.evaluate();
                } catch (Throwable t) {
                    // Give the test a chance to e.g. produce an error
                    // screenshot before failing the test by re-throwing the
                    // exception
                    ((AbstractTB3Test) target).onUncaughtException(t);
                    throw t;
                }
            }
        };
    }

    private static class TB3Method extends FrameworkMethod {
        private DesiredCapabilities capabilities;

        public TB3Method(Method method, DesiredCapabilities capabilities) {
            super(method);
            this.capabilities = capabilities;
        }

        @Override
        public Object invokeExplosively(final Object target, Object... params)
                throws Throwable {
            // Executes the test method with the supplied parameters
            return super.invokeExplosively(target);
        }

        @Override
        public String getName() {
            return String.format("%s[%s]", getMethod().getName(),
                    BrowserUtil.getUniqueIdentifier(capabilities));
        }

    }
}