img

Selenium – Running Test on Google Chrome

Finally, the time arrived, what you’re waiting for. In this tutorial, we’re going to create one script and execute it on the chrome browser. Follow the below steps to start the automation.

  • Open Eclipse IDE, and create a new Project, Package and Class for the automation. You can refer the below link to create the Class in Eclipse IDE.
  • Below is the project model designed. You can name anything to class, project or package, for now, we’re going to name the class as ExecuteOnChrome.java as we’re going to execute our code in Chrome browser.

NOTE: You can write code with me line by line or you can also download it from GitHub from the below URL.

  • To start the automation, first of all, we need to create the object of the Selenium WebDriver. For invoking the driver instances we need to create the object of the WebDriver which we’ll create as below.
package chapter1;

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

public class ExecuteOnChrome {

	public static void main(String[] args) {

		// Creating instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();
	}
}
  • Above code will generate the instance of the WebDriver as the driver for the working with automation. You might also notice we have created the driver instance with the ChromeDriver() which will create the instance for the Chrome browser with the WebDriver.
  • Now as we saw previously to communicate with the Chrome browser Selenium will need chromedriver.exe file to run the chrome. You can download it from the Google Chrome site.

Below tutorial will guide you for downloading browser binaries

  • Once you downlaoded the binaries and unzipped it, you’ll need to set those binaries in the system y System.setProperty() command. For the Google Chrome, property set by webdriver.chrome.driver and with value as the path of the chromedriver.exe. as below
package chapter1;

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

public class ExecuteOnChrome {

	public static void main(String[] args) {

		// Setting the Chrome Driver binaries
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\chromedriver.exe"); // You can set path wherever you have unzipped the binary file

		// Creating instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();
	}
}
  • We have configured driver successfully to run with chrome browser, now let’s open the Browser with the URL we pass. To open the browser we execute the command driver.get(<URL>) to open the browser and URL.
package chapter1;

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

public class ExecuteOnChrome {

	public static void main(String[] args) {

		// Setting the Chrome Driver binaries
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\chromedriver.exe");

		// Creating instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Opening the Browser with URL http://www.step2qa.com
		driver.get("http://www.step2qa.com");
	}
}
  • Now, just to confirm that the page is loaded we’ll verify it with printing the page title in the console. This can be done by driver.getTitle() command which will return you the current page title.
package chapter1;

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

public class ExecuteOnChrome {

	public static void main(String[] args) {

		// Setting the Chrome Driver binaries
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\chromedriver.exe");

		// Creating instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Opening the Browser with URL
		driver.get("http://www.step2qa.com");

		// Get and print the page title of the loaded page
		System.out.println(driver.getTitle());
	}
}

Once, the browser opens you’ll need to close the browser instance as well once your execution completed, for closing your instance you just need to pass command driver.quit() which will eventually close your browser and quit the automation session.

package chapter1;

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

public class ExecuteOnChrome {

	public static void main(String[] args) throws InterruptedException {

		// Setting the Chrome Driver binaries
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\chromedriver.exe");

		// Creating instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Opening the Browser with URL
		driver.get("http://www.step2qa.com");

		// Get and print the page title of the loaded page
		System.out.println(driver.getTitle());

		// Wait for the 5000ms (5 seconds), Java Threading concept
		Thread.sleep(5000);

		// Quit the browser session and close the current running browser
		driver.quit();
	}
}
  • That’all we need now. Let’s run this Java file to see chrome browser opening! To run this file, right-click over the Class > Run As > Java Application, it’ll run you Java class and you’ll see the Chrome Browser opening! Cheers!!!
  • In the next tutorial, we’ll launch the Firefox browser with the Selenium.
  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

1 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

It is main inner container footer text