We will learn to write a Script to open and Launch a new browser and open the desired URL. Selenium webdriver supports all important browsers like
1) Firefox
2) Chrome
3) IE
4) Safari
5) Opera
Opera and safari browsers are not very stable using selenium. Hence we will concentrate on Firefox, Chrome and IE.
A) Firefox
Browser
1. Write the
below code to open Firefox browser and open the URL.
@Test
public void openfirefox()
{
WebDriver
driver1= new FirefoxDriver ();
driver1.get("https://www.google.co.in");
}
2.
Explanation
of the code
a) WebDriver driver = new FirefoxDriver();
b) WebDriver: It is a predefined imported Class from
“open.openqa.selenium”
c) Driver : This is name of the new object created.
d) driver.get--->WebDriver's get() method is used to launch a
new browser session and directs it to the URL that you specify as its
parameter. You can specify the URL to be opened.
3. The above
code, will launch Firefox browser and open the URL (google.com).
B) Chrome
Browser
1. Write the
below code to open Chrome browser and open the URL.
@Test
public void openchrome()
{
WebDriver
driver2 =new ChromeDriver();
driver2.get("https://in.yahoo.com/?p=us");
}
2. The script
will give an error. “ The path to the
driver executable must be set by the webdriver.ie.driver system property”.
And
download the chrome driver for you PC (64Bit or 32 Bit).
4.
Now save the downloaded chromedriver.exe on your
machine.
5.
Right click on the package and add source
folder. Name the folder chromedriver. Now place the exe file inside this
folder.
6.
Now write the modified code to run chrome
browser.
@Test
public void openchrome()
{
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\ChromeDriver\\chromedriver.exe");
WebDriver
driver2 =new ChromeDriver();
driver2.get("https://in.yahoo.com/?p=us");
}
7.
Explanation of code
a)
setProperties
method changes the set of system properties for the current running
application. These changes are not persistent. That is, changing the system
properties within an application will not affect future invocations of the Java
interpreter for this or any other application. The runtime system
re-initializes the system properties each time its starts up. If changes to
system properties are to be persistent, then the application must write the
values to some file before exiting and read them in again upon startup.
b)
getProperty method returns a
string containing the value of the property. If the property does not exist,
this version of getProperty returns null.
c)
User.dir - User working
directory
In this, you set the chrome driver’s path, using
the chromedriver downloaded and then launch the chrome browser from the project
path.
C) IE Browser
1.
Write the following
code for launching IE browser and opening the URL.
@Test
public void openie()
{
WebDriver
driver3 =new InternetExplorerDriver();
driver3.get("http://www.rediff.com/");
}
2.
When you run the
above code it will give you same error as for chrome browser. The path to the driver executable must
be set by the webdriver.ie.driver system property”.
4.
Now save the exe
file to you folder.
5.
Create a source
folder in your project package and keep the exe file over there.
Rightclick on
Package --> Add new source
folder--> Copy the exe file
to your project.
6.
Now write the modified
code to IE browser
@Test
public void openie()
{
System.setProperty("webdriver.ie.driver","D:\\Workspace\\TestProject\\iedriver\\IEDriverServer.exe");
WebDriver
driver3 =new InternetExplorerDriver();
driver3.get("http://www.rediff.com/");
}
7.
Now the above script will run successfully.
8.
In the above script ,
you set the IE driver’s path, using the IE downloaded and then launch the IE
browser from the project path.
Sample Script for Browser Testing Using Selenium Webdriver.
package TestPackage;
import org.junit.Test;
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.DesiredCapabilities;
public class BrowsersTest {
// Open Firefox Browser
@Test
public void openfirefox()
{
WebDriver driver1= new FirefoxDriver ();
driver1.get("https://www.google.co.in");
}
// Open Chrome Browser
@Test
public void openchrome()
{
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\ChromeDriver\\chromedriver.exe");
System.setProperty(("userdir")+ "chromedriver\\chromedriver.exe","chromedriver.exe");
WebDriver driver2 =new ChromeDriver();
driver2.get("https://in.yahoo.com/?p=us");
}
// Open IE browser
@Test
public void openie()
{
System.setProperty("webdriver.ie.driver","D:\\Workspace\\TestProject\\iedriver\\IEDriverServer.exe");
WebDriver driver3 =new InternetExplorerDriver();
driver3.get("http://www.rediff.com/");
}
}