AbstractBrowser.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package jnpf.selenium.driver;
  2. import jnpf.selenium.SeleniumHelper;
  3. import jnpf.selenium.consts.SeleniumConsts;
  4. import jnpf.selenium.properties.SeleniumProperties;
  5. import lombok.Data;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.openqa.selenium.remote.AbstractDriverOptions;
  8. import org.openqa.selenium.remote.RemoteWebDriver;
  9. import org.openqa.selenium.remote.service.DriverService;
  10. @Data
  11. @Slf4j
  12. public abstract class AbstractBrowser<D extends RemoteWebDriver, S extends DriverService, O extends AbstractDriverOptions<?>> implements SeleniumBrowser<D, S, O> {
  13. protected D driverInstance;
  14. protected S driverServiceInstance;
  15. protected O driverOptionsInstance;
  16. protected AbstractBrowser() {
  17. this(null, null, null);
  18. }
  19. protected AbstractBrowser(D driver, S driverService, O driverOptions) {
  20. this.driverInstance = driver;
  21. this.driverServiceInstance = driverService;
  22. this.driverOptionsInstance = driverOptions;
  23. if (this.driverInstance == null) {
  24. if (this.driverServiceInstance == null && this.driverOptionsInstance == null) {
  25. this.driverServiceInstance = buildDriverService();
  26. this.driverOptionsInstance = buildDriverOptions();
  27. }
  28. if(log.isDebugEnabled()){
  29. log.debug(SeleniumConsts.MARKER, "创建浏览器, {}, {}", this.driverServiceInstance, this.driverOptionsInstance);
  30. }
  31. this.driverInstance = buildDriver(this.driverServiceInstance, this.driverOptionsInstance);
  32. }
  33. }
  34. protected static SeleniumProperties getProperties() {
  35. return SeleniumHelper.getSeleniumProperties();
  36. }
  37. protected abstract O buildDriverOptions();
  38. protected abstract S buildDriverService();
  39. protected abstract D buildDriver(S driverService, O driverOptions);
  40. D buildDriver() {
  41. return buildDriver(buildDriverService(), buildDriverOptions());
  42. }
  43. @Override
  44. public D getDriver() {
  45. return this.driverInstance;
  46. }
  47. @Override
  48. public S getDriverService() {
  49. return this.driverServiceInstance;
  50. }
  51. @Override
  52. public O getDriverOptions() {
  53. return this.driverOptionsInstance;
  54. }
  55. /**
  56. * 初始化调用
  57. */
  58. public void init() {
  59. if(log.isDebugEnabled()){
  60. log.debug(SeleniumConsts.MARKER, "浏览器实例初始化方法调用");
  61. }
  62. if(driverInstance != null) {
  63. setScriptTimeout(getProperties().getScriptTimeout());
  64. setPageLoadTimeout(getProperties().getPageLoadTimeout());
  65. setImplicaitlyWaitTimeout(getProperties().getImplicaitlyWaitTimeout());
  66. }
  67. }
  68. @Override
  69. public void close() {
  70. if(log.isDebugEnabled()){
  71. log.debug(SeleniumConsts.MARKER, "浏览器实例销毁化方法调用");
  72. }
  73. if (driverInstance != null) {
  74. try {
  75. driverInstance.quit();
  76. } catch (Exception e) {
  77. log.error(SeleniumConsts.MARKER, "关闭浏览器出错", e);
  78. }
  79. }
  80. }
  81. }