Untitled Document
Chapter - 3 - Chrome and IE Browser Setup

In our previous chapters we have seen , how to open the webpage in firfox browser which is the default browser supported by selenium . Now to open same webpages in chrome and IE we need to do some setup . Lets see the step by step procedure for this .

Step -1 Go to - http://www.seleniumhq.org/ click on Downloads , Go To - Third Party Drivers, Bindings, and Plugins , Inside Browser Section click on Google Chrome Driver to download the chrome.exe setup . The Link will redirect you to googleapi page .

Step -2 Click on the Downloads link from the - https://sites.google.com/a/chromium.org/chromedriver/ ( Make sure that you are using the Link which you obtain from step -1 , I am mentioning this link just for the reference )

Step -3 You will be Redirected to a new web page after clicking on the link from Step - 2 , on New paage you will see a link - chromedriver.storage.googleapis.com click on it

Step - 4 - The step 3 will redirect to the new web page where you will get the actual setup i.e .exe file for chrome driver . Download the latest version of .zip file. As we are using windows machine we will download windows file , If you are a linux user then download Linux support file . Click on the filename to download the file. the page would display something like this

Step-5

Now download the above zip file and extract the chromedriver .exe file into your prefered location . I have extracted the file in my javaproject where i have installed the java .

Step 6

Now to use this chromedriver we have to set the system property in our java code , we cannot simply call chromedrive like firefox driver . If you try to run the chromedriver without setting up its system property selenium will give you an error .

The highlighted code will set the system property for chromedriver .

Now run the below code in IDE and it will open gmail login page in Google Chrome Browser .

 package MySelenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GmailLogin {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
		WebDriver mybrowser= new ChromeDriver();
		
		mybrowser.get("https://www.google.com/gmail/");
}
}

Step -1 Go to - http://www.seleniumhq.org/ click on Downloads , Go To - The Internet Explorer Driver Server, Inside this click on the download - 32 bit Windows IE. The Link will redirect you to Internet Explorer download page .

Step-2 Now unzip the downloaded file and copy the IEDriverServer.exe to the location where we have copied the chromedriver.exe file . I have extracted the file in my javaproject where i have installed the java . (C:\Users\lenovo\workspace\LearnJava\Library)

Step-3

Now to use this IEdriver we have to set the system property in our java code , we cannot simply call chromedrive like firefox driver . If you try to run the IEdriver without setting up its system property selenium will give you an error .

The highlighted code will set the system property for IEdriver .

Now run the below code in IDE and it will open gmail login page in Internet Explorer Browser .

 package MySelenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class OpenIErowser {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.setProperty("webdriver.ie.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\IEDriverServer.exe");
		WebDriver mybrowser= new InternetExplorerDriver();
		
		mybrowser.get("https://www.google.com/gmail/");

	}

}

 


If you look at the window size its not the full size window to maximize it lets write this line and see what happens .

mangesh.manage().window().maximize();
   // This program will Maximize the Browser Window Size
   package Mytest;
   import org.openqa.selenium.firefox.FirefoxDriver; 
   public class MaximiseTheBrowserSize
   { 
      public static void main (String args[])
       {
         FirefoxDriver mangesh;
         mangesh = new FirefoxDriver();
         mangesh.get("http://www.mangadaku.com/");
         mangesh.manage().window().maximize();
       }
   }

Now you must have observed that when you run this program multiple times a new browser window gets open , which unnecessarly consumes memory , to avoid this we can quit the browser once we are done with it .To quit the browser just add the below lines

mangesh.quit();
   package Mytest;
   import org.openqa.selenium.firefox.FirefoxDriver; 
   public class QuitBrowser
   { 
      public static void main (String args[])
       {
         FirefoxDriver mangesh;
         mangesh = new FirefoxDriver();
 
         //Code Will Open URL in firefox Browser 
           mangesh.get("http://www.mangadaku.com/");

         //code will maximise the browser window size
         mangesh.manage().window().maximize();
         
         //code to close the browser 
         mangesh.quit();
       }
   }

On same webpage we have search bar - lets try to read that search bar in our current program and type few words for searching . The below sample program will perform this task we will see the explanation of each line used in this program . Lets create a new class called WebPageSearch

   package Mytest;
   import org.openqa.selenium.By;
   import org.openqa.selenium.firefox.FirefoxDriver; 
   public class WebPageSearch
   { 
      public static void main (String args[])
       {
         FirefoxDriver mangesh;
         mangesh = new FirefoxDriver(); 
         
      //Code Will Open URL in firefox Browser 
           mangesh.get("http://www.mangadaku.com/");
        
      //code will maximise the browser window size
            mangesh.manage().window().maximize();
            
            mangesh.findElement(By.id("gsc-i-id1")).sendKeys("Selenium");
            mangesh.findElement(By.className("gsc-search-button")).click();

         
        //code to close the browser 
         mangesh.quit();
       }
   }

Now lets understand the code line by line . If you have carefully observed the program then you will see a class new import org.openqa.selenium.By; has been added . also we have 2 lines

  • mangesh.findElement(By.id("gsc-i-id1")).sendKeys("Selenium");
  • mangesh.findElement(By.className("gsc-search-button")).click();

findElement(By.id("gsc-i-id1")) - go to the search text area whose id is gsc-i-id1 sendKeys("Selenium") - type the word Selenium inside the text box. mangesh.findElement(By.className("gsc-search-button")) - finds the search button by classname and click() -click on the search button .

In html we have id , class name , form name for text area, radio button, check boxes etc . we can either use their id or name attribute to find these element . It depends upon html coding . In our search button we don't have id so we have used class name . The operation is same but prefer id wherever possible.

click here If you want to to see how we get the web element id and name

Login & Logout -

Consider a Scenario where you have to login to the system by giving username and password followed by submit action . Lets try to login to your gmail account using following script .

   package Mytest;
   import org.openqa.selenium.By;
   import org.openqa.selenium.firefox.FirefoxDriver; 
   public class TestUserLogin
   { 
      public static void main (String args[])
       {
         FirefoxDriver mangesh;
         mangesh = new FirefoxDriver(); 
         
      //Open Gmail Account Home page  
           mangesh.get("https://mail.google.com/");
        
      //code will maximise the browser window size
            mangesh.manage().window().maximize();
       // find the username and password text area and enters the details in it 
            
           mangesh.findElementById("Email").sendKeys("mangeshmpande1987@gmail.com");
           mangesh.findElementById("Passwd").sendKeys("*********");
           mangesh.findElement(By.id("signIn")).click();
         
        //code to close the browser 
         mangesh.quit();
       }
   }

By using above code we have successfully logged in to our gmail account and can see our mail box .

In this chapter till now we have covered some simple methods and operations that we can perform using selenium methods . Lets summarise the method that we have learnt so far .

Note - refer the highlighted methods

Command Function
<webdriver_name>.get("url"); will open the URL in browser
<webdriver_name>.manage().window().maximize(); Will maximize the window size of browser
<webdriver_name>.findElementById(" "); Will search the web element by id ( e.g text area , check box etc)
<webdriver_name>.findElement(By.id(" ")); same as above
<webdriver_name>.findElementById(" ").sendKeys(" "); will send the keys or values to the text area on web page
<webdriver_name>.findElement(By.id(" ")).click(); will click on the web element (e.g submit button , search button etc)
<webdriver_name>.quit(" "); will quit your browser session

Summary -

Lets summarise the topics that we have learnt so far -

  • we have open the firefoxbrowser using and passed the url to it using .get("url");
  • Then we have maximize the browser window .manage().window().maximize();
  • We have searched the word selenium in search bar of webapge using .findElementById(" ").sendKeys(" "); and clicked search button .findElement(By.id(" ")).click();
  • We have logged in to your gmail accound by finding the username and password text area and sent the values to it using .findElementById(" ").sendKeys(" ");
  • We have clicked on submit button using - .findElement(By.id(" ")).click();
  • and finally quit the browser session using - .quit(" ");

So far we have opened the webapges in firefox browser , Selenium by deafult has property to open webpages in firefox , if we want to open the webpages in chrome or IE we need to install the set up in selenium . In our next chapter we will see how to configure chrome and IE browsers and we will open same webpages on these browsers .

Untitled Document