How to start with Selenium in Java (Chromium)

/, Selenium, Testing/How to start with Selenium in Java (Chromium)

With this code you can start a Browser Session from Java using Google Chromedriver and Selenium.

There are random user agents and mobile devices.

You also need to make a new Profile.

Adding extensions is also possible.

Download Selenium for Java : http://www.seleniumhq.org/download/

Download Chromedriver : https://sites.google.com/a/chromium.org/chromedriver/downloads

More Chromium commands here : http://peter.sh/experiments/chromium-command-line-switches/

Here is my example :

public static ChromeDriver StartBrowser() throws Exception{

 int startRand = 100; 
 Random start = new Random();
 startRand = start.nextInt((100 - 0) + 1) + 0;

 if(startRand > 70){
 isMobile = true; 
 }

 String profile = ""+Functions.locations+"/data/Profile";

 System.setProperty("webdriver.chrome.driver",chromium);

 DesiredCapabilities capabilities = DesiredCapabilities.chrome();

 Map<String, Object> chromeOptions = new HashMap<String, Object>();

 //Mobile 
 if(isMobile == true){

 String[] mobile = {"Apple iPhone 5","Apple iPhone 6","Apple iPhone 6 Plus","Google Nexus 4","Google Nexus 5","Google Nexus 6","Google Nexus 7","Google Nexus 10","Samsung Galaxy Note 3","Samsung Galaxy Note II","Samsung Galaxy S III","Samsung Galaxy S4"};

 int randomNum = 11;

 Random rand = new Random();
 randomNum = rand.nextInt((11 - 0) + 1) + 0; 
 Functions.mobile_name = mobile[randomNum];
 
 
 Functions.raport("Device : " + Functions.mobile_name);

 Map<String, String> mobileEmulation = new HashMap<String, String>();
 mobileEmulation.put("deviceName", mobile[randomNum]); 

 chromeOptions.put("mobileEmulation", mobileEmulation); 


 List<String> cmd = Arrays.asList("user-data-dir="+profile+"", "--no-proxy-server", "--test-type=ui", "--disable-notifications", "--disable-translate", "--no-default-browser-check ", "--disable-infobars", "--disable-session-crashed-bubble", "--mute-audio" /*, "--start-maximized" *//* ,"--load-extension="+Alexa+"" */ ); 
 
 if(Functions.isIncognito == true){
 cmd = Arrays.asList("user-data-dir="+profile+"", "--no-proxy-server", "--test-type=ui", "--disable-notifications", "--disable-translate", "--no-default-browser-check ", "--disable-infobars", "--disable-session-crashed-bubble", "--mute-audio" , "--incognito", "--disable-extensions");
 }

 chromeOptions.put("args", cmd);

 }

 // chrome://flags/ http://peter.sh/experiments/chromium-command-line-switches/

 //Non-Mobile 
 if(isMobile == false){ 

 //Random User Agent 
 String[] agent = new String[22];

 agent[0] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246";
 agent[1] = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36";
 agent[2] = "Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36";
 agent[3] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9";
 agent[4] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36";
 agent[5] = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0";
 agent[6] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
 agent[7] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
 agent[8] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
 agent[9] = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0";
 agent[10]= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0";
 agent[11]= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
 agent[12]= "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
 agent[13]= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko) Version/9.1.3 Safari/601.7.8";
 agent[14]= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
 agent[15]= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
 agent[16]= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7";
 agent[17]= "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
 agent[18]= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0";
 agent[19]= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586";
 agent[20]= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
 agent[21]= "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0";

 int randomNum = 0; 
 Random rand = new Random();
 randomNum = rand.nextInt((21 - 0) + 1) + 0; 
 Functions.user_agent = agent[randomNum]; 

 List<String> cmd = Arrays.asList("user-data-dir="+profile+"", "--no-proxy-server", "--disk-cache-size=1" , "--media-cache-size=1", "--test-type=ui", "--disable-notifications", "--disable-translate", "--no-default-browser-check ", "--disable-infobars", "--disable-session-crashed-bubble", "--mute-audio" , "--user-agent="+agent[randomNum]+"" , "--start-maximized" );
 
 rand = new Random();
 if (rand.nextInt(5) < 2){
 Functions.user_agent = "Chrome User Agent )";
 cmd = Arrays.asList("user-data-dir="+profile+"", "--no-proxy-server", "--disk-cache-size=1" , "--media-cache-size=1", "--test-type=ui", "--disable-notifications", "--disable-translate", "--no-default-browser-check ", "--disable-infobars", "--disable-session-crashed-bubble", "--mute-audio" , "--start-maximized" );
 }

 
 chromeOptions.put("args", cmd);
 }

 capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
 
 capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);

 capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT); 
 

 driver = new ChromeDriver(capabilities);

 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
 driver.navigate().to("https://google.com");
By | 2017-02-11T12:28:49+00:00 February 10th, 2017|0 Comments

Leave A Comment