Untitled Document
Chapter - 4 - Basic WebDriver Scripts

Lets run a sample program which opens MangaDaku in Chrome browser .Here is the sample code for it .

package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

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

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

mybrowser.manage().window().maximize();
package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.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

mybrowser.quit(); ------- This will close all the windows
mybrowser.close(); ------- This will close only single window
   package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.manage().window().maximize();
         //code to close the browser 
         mybrowser.quit();
       }
   }

You might have seen that the browser window was launched, the size was maximized and then closed successfully .But all these activities were too fast to recognise for an user . So we will add the waits after each activity so that user should see each activity . There are 2 type of waits we can use in selenium . Lets see the usage of the waits with simple scripts . We have covered Waits in seprate chapter in detail.

For The current Context we will see the simple Java Thread.Sleep to hold the browser window

      package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.manage().window().maximize();
     
//waits for 5 seconds
Thread.sleep(3000);    
//code to close the browser 
         mybrowser.quit();
       }
   }

Our webpage contains different elements like - Forms, Page Title, Text , Links , Image, Radio button, check boxes etc.... Lets try to locate these elements using webdriver classes .

1. Get The Page Title

Every Web page has the Page Title which give one liner info about that page . Lets read the page title of MangDaku Home page . As discussed to Locate the page element we need to use webdriver classes , so in this case get is the class and we need to call the methods of this class

To get The page title use .getTitle()

Copy below code and paste it into your Eclipse IDE and Run it . The Script will fetch the Page tile for the webapge .

      package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.manage().window().maximize();
     
//waits for 5 seconds
Thread.sleep(3000); 
 // Get the Title of the website
String title =mybrowser.getTitle();
System.out.println(title);

//code to close the browser mybrowser.quit(); } }
2. Get The URL Of The Page

Sometimes we need to navigate to the pages , e.g we we login to gmail account , the gmail login page will ask you for the username first then you click on the Next button and it will redirect to new page . Now to get ( locate ) the elements of the next page we need to pass the URL link to webdriver otherwise it will give you error and unable to locate the elements . Inorder to achive this we have to get the URL and need to tell webriver to navigtae to the page and find the web elements . We will see one example for Gmail login account where we have use this method .

To get The page URL use mybrowser.get(URL);

      package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.manage().window().maximize();
     
//waits for 5 seconds
Thread.sleep(3000); 
 // Get the Title of the website
String title =mybrowser.getTitle();
System.out.println(title); // Code to get The current URL
String URL =mybrowser.getCurrentUrl();
System.out.println(URL);
//code to close the browser mybrowser.quit(); } }

3. Get The Page Source

To get The page source use mybrowser.getPageSource();

      package MySelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

public static void main(String[] args) {
		
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
     
     WebDriver mybrowser= new ChromeDriver();
     
     // open the page mangadaku.com
     mybrowser.get("http://www.mangadaku.com");
     mybrowser.manage().window().maximize();
     
//waits for 5 seconds
Thread.sleep(3000); 
 // Get the Title of the website
String title =mybrowser.getTitle();
System.out.println(title); // Code to get The Page source
String Page =mybrowser.getPageSource();
System.out.println(Page);
//code to close the browser mybrowser.quit(); } }
1. Navigate to a Webpage

This is similar to the get method where webdriver will take you to webapge specified in the URL . Run the below sample code which will open mangdaku homepage first and then it will navigate to you on selenium tutorial .

To Navigate to a web page use mybrowser.navigate().to();

package MySelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

	public static void main(String[] args) throws InterruptedException {
		
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
		 WebDriver mybrowser= new ChromeDriver();
		 
		 // open the page mangadaku.com
		 
          mybrowser.get("http://www.mangadaku.com");
         
          // Maximize the window size
          
          mybrowser.manage().window().maximize();
          String URL =mybrowser.getCurrentUrl();
          System.out.println(URL);
          //mybrowser.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
          Thread.sleep(3000);
          
  // Navigate to selenium tutorial to homepage        
          mybrowser.navigate().to("http://mangadaku.com/selenium.php");
          
           // Quit Browser
          mybrowser.quit();
	}
}
2. Refresh a Webpage

This is similar to F5 of the keyboard to refresh the brwoser . Run the below sample code which will Refresh your webapge .

To refresh The web page use mybrowser.navigate().refresh();

package MySelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

	public static void main(String[] args) throws InterruptedException {
		
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
		 WebDriver mybrowser= new ChromeDriver();
		 
		 // open the page mangadaku.com
		 
          mybrowser.get("http://www.mangadaku.com");
         
          // Maximize the window size
          
          mybrowser.manage().window().maximize();
          String URL =mybrowser.getCurrentUrl();
          System.out.println(URL);
          //mybrowser.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
          Thread.sleep(3000);
          
  // Navigate to selenium tutorial to homepage        
         mybrowser.navigate().to("http://mangadaku.com/selenium.php");
          mybrowser.navigate().refresh();
           // Quit Browser
          mybrowser.quit();
	}
}
3. Navigate Back to the webapge

To navigate back to the webpage use mybrowser.navigate().back();

This is similar to of the keyboard to refresh the brwoser . Run the below sample code which will Refresh your webapge .

package MySelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

	public static void main(String[] args) throws InterruptedException {
		
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
		 WebDriver mybrowser= new ChromeDriver();
		 
		 // open the page mangadaku.com
		 
          mybrowser.get("http://www.mangadaku.com");
         
          // Maximize the window size
          
          mybrowser.manage().window().maximize();
          String URL =mybrowser.getCurrentUrl();
          System.out.println(URL);
          //mybrowser.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
          Thread.sleep(3000);
          
  // Navigate to selenium tutorial to homepage        
         mybrowser.navigate().to("http://mangadaku.com/selenium.php");
          mybrowser.navigate().refresh();
          mybrowser.navigate().back();
           // Quit Browser
          mybrowser.quit();
	}
}

4. Navigate Forward to the webapge

To navigate forward to the webpage use mybrowser.navigate().forward();

Run the below sample code which will move you forward .

package MySelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowser {

	public static void main(String[] args) throws InterruptedException {
		
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\workspace\\LearnJava\\Library\\chromedriver.exe");
		 WebDriver mybrowser= new ChromeDriver();
		 
		 // open the page mangadaku.com
		 
          mybrowser.get("http://www.mangadaku.com");
         
          // Maximize the window size
          
          mybrowser.manage().window().maximize();
          String URL =mybrowser.getCurrentUrl();
          System.out.println(URL);
          //mybrowser.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
          Thread.sleep(3000);
          
  // Navigate to selenium tutorial to homepage        
         mybrowser.navigate().to("http://mangadaku.com/selenium.php");
          mybrowser.navigate().refresh();
          mybrowser.navigate().forward();
           // Quit Browser
          mybrowser.quit();
	}
}
Untitled Document