Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. public class DriverFactory {
  2. /**
  3. *
  4. * @param browser Driver type to use in tests.
  5. * @return New instance of {@link WebDriver} object.
  6. */
  7. public static WebDriver initDriver(String browser) {
  8. MutableCapabilities options = getOptions(browser);
  9.  
  10. switch (browser) {
  11. case "firefox":
  12. System.setProperty(
  13. "webdriver.gecko.driver",
  14. new File(DriverFactory.class.getResource("/geckodriver.exe").getFile()).getPath());
  15. return new FirefoxDriver(options);
  16. case "ie":
  17. case "internet explorer":
  18. System.setProperty(
  19. "webdriver.ie.driver",
  20. new File(DriverFactory.class.getResource("/IEDriverServer.exe").getFile()).getPath());;
  21. return new InternetExplorerDriver(options);
  22. case "android":
  23. case "headless-chrome":
  24. case "chrome":
  25. default:
  26. System.setProperty(
  27. "webdriver.chrome.driver",
  28. new File(DriverFactory.class.getResource("/chromedriver.exe").getFile()).getPath());
  29. return new ChromeDriver(options);
  30. }
  31. }
  32.  
  33.  
  34. public static MutableCapabilities getOptions(String browser) {
  35. MutableCapabilities options;
  36.  
  37. switch (browser) {
  38. case "firefox":
  39. options = new FirefoxOptions();
  40. return options;
  41. case "ie":
  42. case "internet explorer":
  43. options = new InternetExplorerOptions().destructivelyEnsureCleanSession();
  44. options.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
  45. options.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  46. return options;
  47. case "android":
  48. Map<String, String> mobileEmulation = new HashMap<>();
  49. mobileEmulation.put("deviceName", "Galaxy S5");
  50. options = new ChromeOptions().setExperimentalOption("mobileEmulation", mobileEmulation);
  51. return options;
  52. case "headless-chrome":
  53. options = new ChromeOptions().addArguments("headless")
  54. .addArguments("window-size=800x600");
  55. return options;
  56. case "chrome":
  57. default:
  58. options = new ChromeOptions();
  59. return options;
  60. }
  61. }
  62.  
  63. /**
  64. *
  65. * @param browser Remote driver type to use in tests.
  66. * @param gridUrl URL to Grid.
  67. * @return New instance of {@link RemoteWebDriver} object.
  68. */
  69. public static WebDriver initDriver(String browser, String gridUrl) {
  70. WebDriver driver;
  71. try {
  72. driver = new RemoteWebDriver(new URL(gridUrl), getOptions(browser));
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. throw new SkipException("Unable to create RemoteWebDriver instance!");
  76. }
  77. return driver;
  78. }
  79.  
  80. }
  81. © 2018 GitHub, Inc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement