Monday 26 February 2018

Driver Setup with selenium wrapper automation

Driver setup is very important and necessary in any selenium automation.

We can setup driver with direclty write below line.

WebDriver vDriver = new FirefoxDriver();

but if we want to driver for other browser then we have to change code and call driver for that browser. It is not good approach that every time we change code as epr browser requirement.

SO, rather change code every time we can create method for setup driver and pass browser name as parameter for whcih browser we want. It is right approach to setup driver in any selenium automation framework.


I have created one call call 'SetupDriver' and create two methods in it. One for simple driver and second is for remote driver.

You can use this call directly and your automation framework. Whenever you want to setup driver then simple create object of 'SetupDriver' class and call method as per your requirement.


  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
168
169
public class SetupDriver 
{

 public WebDriver vDriver = null;
 public Proxy ZAPproxy;

 // Returs the webdriver. Input parameters are 
 // 1)vBrowser - Browser Type like
 // 2)vProxy - True / False
 public WebDriver mGetRemoteDriver(String vBrowser, boolean vProxy) 
 {

  // browser - chrome
  if (vBrowser.equalsIgnoreCase("chrome")) {
   DesiredCapabilities capability = DesiredCapabilities.chrome();

   if (vProxy) 
   {
    ProxyServer BrowserMobproxyServer;
    BrowserMobproxyServer = new ProxyServer(9090);
    try {
     BrowserMobproxyServer.start();
    } catch (Exception e1) {
     System.out.println("Exception during start of BrowserMobProxy");
     e1.printStackTrace();
    }
    Proxy BrowserMobproxy;
    BrowserMobproxy = new Proxy();
    BrowserMobproxy.setHttpProxy("localhost:8080");
    BrowserMobproxy.setSslProxy("localhost:8080");
    capability.setCapability(CapabilityType.PROXY, BrowserMobproxy);
   }
   ChromeOptions options = new ChromeOptions();   
   options.addArguments(Arrays.asList("--start-maximized", "allow-running-insecure-content", "ignore-certificate-errors"));
   capability.setCapability(ChromeOptions.CAPABILITY, options);
   capability.setBrowserName("chrome");
   
   try 
   {
    vDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
   }
   catch (UnreachableBrowserException E)
   {    
    System.out.println("Unreachable browser exception in class GetDriver for chrome browser");    
   }
   catch (MalformedURLException E)
   {    
    System.out.println("Malformed URL exception in class GetDriver for chrome browser");    
   }
  }

  // browser - firefox
  if (vBrowser.equalsIgnoreCase("firefox")) {
   DesiredCapabilities capability = DesiredCapabilities.firefox();
   capability.setBrowserName("firefox");

   if (vProxy) 
   {
    ZAPproxy = new Proxy();
    ZAPproxy.setAutodetect(false);
    ZAPproxy.setProxyType(ProxyType.MANUAL);
    ZAPproxy.setHttpProxy("localhost:8080");
    ZAPproxy.setSslProxy("localhost:8080");
    capability.setCapability(CapabilityType.PROXY, ZAPproxy);
   }
   
   FirefoxProfile fxProfile = new FirefoxProfile();

   fxProfile.setPreference("browser.download.folderList", 2);
   fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
   fxProfile.setPreference("browser.download.dir", "c:\\mydownloads");
   fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
     "application/pdf,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/ms-excel");
   fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
   // Disables PDF preview
   fxProfile.setPreference("pdfjs.disabled", true);
   fxProfile.setPreference("plugin.scan.plid.all", false);
   fxProfile.setPreference("plugin.scan.Acrobat", "99.0");

   capability.setCapability(FirefoxDriver.PROFILE, fxProfile);

   try 
   {
    vDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
   }
   catch (MalformedURLException E)
   {
    System.out.println("Malformed URL exception in class GetDriver for firefox browser");    
   }
   catch (UnreachableBrowserException E) {
    System.out.println("Unreachable browser exception in class GetDriver for firefox browser");    
   }
  }  

  // browser - internet explorer
  if (vBrowser.equalsIgnoreCase("internet explorer")) 
  {
   DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
   capability.setBrowserName("internet explorer");
   try 
   {
    vDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
   } 
   catch (MalformedURLException E)
   {
    System.out.println("Malformed URL exception in class GetDriver for internet explorer browser");    
   }
   catch (UnreachableBrowserException E)
   {
    System.out.println("Unreachable browser exception in class GetDriver for internet explorer browser");    
   }
  }

  return vDriver;
 }
 
 public WebDriver mGetDriver(String vBrowser, boolean vProxy)
 {
  if (vBrowser.equalsIgnoreCase("chrome"))
  {
   try 
   {
    System.setProperty("webdriver.chrome.driver", "path of chrome driver");
    vDriver = new ChromeDriver();
   } 
   catch (UnreachableBrowserException E) 
   {    
    System.out.println("Unreachable browser exception in class GetDriver for chrome browser");    
   } 
   catch (Exception E) 
   {    
    System.out.println("Exception in class GetDriver for chrome browser");    
   }
  }
  else if (vBrowser.equalsIgnoreCase("firefox"))
  {
   try 
   {    
    vDriver = new FirefoxDriver();
   } 
   catch (UnreachableBrowserException E) 
   {    
    System.out.println("Unreachable browser exception in class GetDriver for firefox browser");    
   } 
   catch (Exception E) 
   {    
    System.out.println("Exception in class GetDriver for firefox browser");    
   }
  }
  else if (vBrowser.equalsIgnoreCase("internet explorer"))
  {
   try 
   {
    System.setProperty("webdriver.ie.driver", "path of ie driver");
    vDriver = new InternetExplorerDriver();
   } 
   catch (UnreachableBrowserException E) 
   {    
    System.out.println("Unreachable browser exception in class GetDriver for IE browser");    
   } 
   catch (Exception E) 
   {    
    System.out.println("Exception in class GetDriver for IE browser");    
   }
  }
    
  return vDriver;
 }
}

You can also read below other article on selenium wrapper automation.

How to handle drop down using selenium wrapper automation

Selenium Wrapper Automaton



No comments:

Post a Comment

Popular