Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@netzulo
Created March 18, 2016 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netzulo/f751496bd2ca0904dd75 to your computer and use it in GitHub Desktop.
Save netzulo/f751496bd2ca0904dd75 to your computer and use it in GitHub Desktop.
Selenium CUSTOM DRIVER CONTROL
package ntz.drivers;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
import ntz.drivers.Bot.BrowserType;
import ntz.drivers.Bot.DriverType;
import ntz.drivers.exceptions.BotException;
import ntz.drivers.exceptions.WebNavException;
import ntz.drivers.navs.WebNav;
import ntz.logs.Log;
/**
* Best Web Scrapper ever
*
*/
public class Bot {
/**Fields************************************************************************************/
// Drivers instances
private WebDriverBackedSelenium gridOneDriver;
private RemoteWebDriver remoteDriver;
public void setRemoteDriver(RemoteWebDriver remoteDriver) {
this.remoteDriver = remoteDriver;
}
public void setLocalDriver(WebDriver localDriver) {
this.localDriver = localDriver;
}
private WebDriver localDriver;
// Driver settings
private Capabilities driverSettings;
// URL of a selenium Hub server
private final static String urlServer = "http://192.168.0.100/wd/hub";
// Helpers browser functions
public WebNav webNav;
public DriverType currentDriveType = null;
public BrowserType currentBrowserType = null;
/**Enums*************************************************************************************/
public enum DriverType {GRIDONE, REMOTE, LOCAL}
public enum BrowserType {FIREFOX, CHROME, IE}
/**Constructors******************************************************************************/
/**
* Exception
* @throws BotException
*/
public Bot() throws BotException {
throw new BotException("[Bot][ERROR]: Constructor denied, need to use an constructor with parameters");
}
/**
* Init driver usando un tipoDriver como parametro
* Tambien genera la instancia y setea el tipo de driver para las subclases: WebNav
*
* @throws BotException
* @throws WebNavException
*/
public Bot(DriverType driverType) throws BotException, WebNavException {
if(driverType == null){
throw new BotException("[Bot][ERROR]: DriverType can't be null");
}
else{
this.currentDriveType = driverType;
}
}
/**
* Init driver using parameter.
* Call to Bot(DriverType) constructor to build driver configuration.
* @throws BotException
* @throws WebNavException
*/
public Bot(DriverType driverType, BrowserType browserType) throws BotException, WebNavException {
this(driverType);//configura el webNav llamando al constructor con menos params
if(browserType == null){
throw new BotException("[Bot][ERROR]: BrowserType can't be null");
}
else{
this.currentBrowserType = browserType;
this.conf(false);
this.confWebNav();
}
}
/**Public methods****************************************************************************/
public void close()throws BotException{
try {
if(this.currentDriveType == DriverType.GRIDONE){
this.gridOneDriver.close();
}
if(this.currentDriveType == DriverType.LOCAL){
this.localDriver.close();
this.localDriver.quit();
}
if(this.currentDriveType == DriverType.REMOTE){
this.remoteDriver.close();
this.remoteDriver.quit();
}
} catch (Exception e) {
new BotException("[Bot.close][WARNING]: Error trying to close driver",e);
}
}
/**
* @throws WebNavException
* @throws BotException
* */
public Bot openBot(DriverType driverType, BrowserType browserType) throws WebNavException, BotException{
Log.info("[BOT]: INIT ");
try {
return new Bot(driverType, browserType);
} catch (BotException e) {
Log.info("[BOT]: ERROR ");
this.close();
throw new BotException(e);
}
}
public void closeBot() throws BotException{
this.close();
Log.info("[BOT]: DONE ");
}
public void exit() throws BotException{this.close();}
public void quit() throws BotException{this.close();}
/**Private methods***************************************************************************/
/**
*
* @throws MalformedURLException
* */
private void conf(boolean isRemote) throws BotException {
//SELECT Capabilities
try {
if(isRemote){
switch (this.currentBrowserType) {
case CHROME:
this.driverSettings = DesiredCapabilities.chrome();
break;
case FIREFOX:
this.driverSettings = DesiredCapabilities.firefox();
break;
case IE:
this.driverSettings = DesiredCapabilities.internetExplorer();
break;
default:
new BotException("[Bot.conf.isRemote=true][ERROR]: BrowserType not selected");
break;
}//switch
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);// habilita javascript
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.TAKES_SCREENSHOT, true);// habilita capturas de pantalla
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);// habilita busquedas CSS
//((DesiredCapabilities) caps).setCapability(CapabilityType.PLATFORM, Platform.WINDOWS);// especifica plataforma
try {
this.remoteDriver = new RemoteWebDriver(new URL(urlServer),this.driverSettings);
} catch (MalformedURLException e) {
new BotException("[Bot.conf][ERROR]: Url of server not working", e);
}
}
else{
switch (this.currentBrowserType) {
case CHROME:
this.driverSettings = DesiredCapabilities.chrome();
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);// habilita javascript
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.TAKES_SCREENSHOT, true);// habilita capturas de pantalla
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);// habilita busquedas CSS
this.localDriver = new ChromeDriver(this.driverSettings);
break;
case FIREFOX:
this.driverSettings = DesiredCapabilities.firefox();
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);// habilita javascript
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.TAKES_SCREENSHOT, true);// habilita capturas de pantalla
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);// habilita busquedas CSS
this.localDriver = new FirefoxDriver(this.driverSettings);
break;
case IE:
this.driverSettings = DesiredCapabilities.internetExplorer();
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);// habilita javascript
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.TAKES_SCREENSHOT, true);// habilita capturas de pantalla
((DesiredCapabilities) this.driverSettings).setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);// habilita busquedas CSS
//((DesiredCapabilities) caps).setCapability(CapabilityType.PLATFORM, Platform.WINDOWS);// especifica plataforma
this.localDriver = new InternetExplorerDriver(this.driverSettings);
break;
default:
new BotException("[Bot.conf.isRemote=false][ERROR]: BrowserType not selected");
break;
}//switch
}//else
} catch (Exception e) {
new BotException("[Bot.conf.isRemote=null][ERROR]: BrowserType not defined");
}
}
/**
* Start WebNav instance
* */
private void confWebNav() throws WebNavException, BotException{
if(this.currentDriveType != null){
switch (this.currentDriveType) {
case GRIDONE:
this.webNav = new WebNav((WebDriver) this.gridOneDriver);
break;
case LOCAL:
this.webNav = new WebNav(this.localDriver);
break;
case REMOTE:
this.webNav = new WebNav(this.remoteDriver);
break;
default:
break;
}
}
else{
throw new BotException("[Bot.confWebNav][ERROR]: Current driver type it's null, can't configure webNav");
}
}
/**
* START EXECUTION OF INSTANCE PROPERTIES DOWN A THREAT
*
* */
// @Override
// public void run() {
// boolean isReady;
//
// try {
// // SELECT driver
// switch (this.currentDriveType) {
// case GRIDONE:
// new BotException("[Bot.run][FATAL]: Grid One not configurated");
// break;
// case LOCAL:
// //Init local driver
// conf(false);
// break;
// case REMOTE:
// //Init remote driver
// conf(true);
// break;
// default:
// new BotException("[Bot.run][FATAL]: DriverType not selected");
// break;
// }
// isReady = true;
// } catch (BotException e) {isReady = false;}
//
// //Try to launch driver on and subThreath
// if(isReady){
// Log.debug("[Bot.run][INFO]: launched thread");
// }
// else{
// Log.debug("[Bot.run][WARNING]: Error at configure before to launch thread");
// }
// }
public WebDriverBackedSelenium getGridOneDriver() {
return gridOneDriver;
}
public RemoteWebDriver getRemoteDriver() {
return remoteDriver;
}
public WebDriver getLocalDriver() {
return localDriver;
}
public DriverType getCurrentDriveType() {
return currentDriveType;
}
public BrowserType getCurrentBrowserType() {
return currentBrowserType;
}
/**Protected methods*************************************************************************/
/**GETs & SETs*******************************************************************************/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment