View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2020 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.qa.glnm.junit.seljup;
21  
22  import static java.util.Collections.singletonList;
23  
24  import java.util.List;
25  
26  import org.junit.jupiter.api.extension.ExtensionContext;
27  import org.junit.jupiter.api.extension.ParameterContext;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import io.github.bonigarcia.seljup.BrowserBuilder;
32  import io.github.bonigarcia.seljup.BrowserType;
33  import io.github.bonigarcia.seljup.BrowsersTemplate.Browser;
34  import io.github.bonigarcia.seljup.SeleniumExtension;
35  
36  final class SeleniumJupiterUtil {
37  
38    private static final Logger LOG = LoggerFactory.getLogger(SeleniumJupiterUtil.class);
39    private static final SeleniumExtension SELENIUM_EXTENSION = new SeleniumExtension();
40  
41    private SeleniumJupiterUtil() {
42      // do not instantiate
43    }
44  
45    static List<Browser> asBrowserList(BrowserType type) {
46      BrowserBuilder builder;
47      switch (type) {
48        case EDGE:
49          if (LOG.isDebugEnabled()) {
50            LOG.debug("Using Edge.", type);
51          }
52          builder = BrowserBuilder.edge();
53          break;
54        case FIREFOX:
55          if (LOG.isDebugEnabled()) {
56            LOG.debug("Using Firefox.", type);
57          }
58          builder = BrowserBuilder.firefox();
59          break;
60        case CHROME:
61          if (LOG.isDebugEnabled()) {
62            LOG.debug("Using Chrome.", type);
63          }
64          builder = BrowserBuilder.chrome();
65          break;
66        default:
67          if (LOG.isInfoEnabled()) {
68            LOG.info("BrowserType {0} not supported. Using Chrome.", type);
69          }
70          builder = BrowserBuilder.chrome();
71          break;
72      }
73      Browser browser = builder.browserName(type.name()).version("LATEST").build();
74  
75      return singletonList(browser);
76    }
77  
78    static Object getDriverFromSelJup(ParameterContext driverParamContext, ExtensionContext context) {
79      if (getSeleniumExtension().supportsParameter(driverParamContext, context)) {
80        return getSeleniumExtension().resolveParameter(driverParamContext, context);
81      }
82      if (LOG.isInfoEnabled()) {
83        LOG.info("Selenium Jupiter Extension does not support driver parameter.");
84      }
85      return null;
86    }
87  
88    static SeleniumExtension getSeleniumExtension() {
89      return SELENIUM_EXTENSION;
90    }
91  
92  }