MainMenu

Home Java Overview Maven Tutorials

Saturday 14 January 2017

Selenium code for firefox, chrome and internet explorer



For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


For Video : CLICK HERE



Here we will discuss how to work with different browsers in selenium.
Selenium by default support Firefox browser, but for chrome & Internet Explorer you have to download their respective drivers, they can be downloaded by using below links:
Download chrome driver : click here to download
Download Internet Explorer browser click here to download


How the run the WebDriver script on Chrome Browser?


You have to performe 2 easy steps to run your script on chrome browser
1)Download the chrome driver :
First You need to download the chrome driver
Link :http://www.seleniumhq.org/download/
Now select your driver according to OS & download it.
You will get zip file, extract this zip file & you will get chromedriver.exe file.
2)Use below code to open Chrome Browser
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
WebDriver driver = new ChromeDriver();


How the run the WebDriver script on Internet Explorer Browser?


1).First you need to do some setting in your IE :-
a). Open IE
b). Go to Tools -> Internet Options --> Security
c). Set all zones (Internet, Local intranet, Trusted sites, Restricted sites) to the same protected mode, enabled or disabled should not matter.
d).Finally, set Zoom level to 100% by right clicking on the gear located at the top right corner and enabling the status-bar. Default zoom level is now displayed at the lower right.

2). Download the Internet Explorer Driver by below link
Link : http://docs.seleniumhq.org/download/
You will get zip file, extract this zip file & you will get ie.exe file.
3).Use below code to open IE Browser
System.setProperty("webdriver.ie.driver", "path of the exe file\\IEDriverServer.exe");
driver = new InternetExplorerDriver();

1.Now first of all i will create a class "Browserfactory"

2.after that i will create a method "Browser" which will accept two arguments , browsername & url
3.here by simple if else condition i will initialize the respective browser
4.after that i will access the URL
5.then maximize the window
and return the WebDriver object "driver"
Code :
package chrome;
import java.util.Scanner;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Browserfactory
{
static WebDriver driver;
@Test
public static WebDriver Browser(String browsername, String url)
{
if(browsername.equalsIgnoreCase("firefox"))
{
driver = new FirefoxDriver();
}
else if(browsername.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver", "D:\\study material\\Selenium Final complete\\Chrome driver\\old\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browsername.equalsIgnoreCase("InternetExplorer"))
{
System.setProperty("webdriver.ie.driver", "D:\\study material\\Selenium Final complete\\IE\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
driver.get(url);
driver.manage().window().maximize();
return driver;
}
}

6.Now i will create an another class "launchbrowser"
7.inside this class i will create a method where i will take the input from user as browsername and url
8.finally i will call the method browser of Browserfactory class
Code :
package chrome;
import java.util.Scanner;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class launchbrowser
{
@BeforeTest
public void f()
{
System.out.println("Enter the browser name");
Scanner scan = new Scanner(System.in);
String browsername = scan.next();
System.out.println("Enter the complete url");
String link = scan.next();
Browserfactory.Browser(browsername, link);
}
@Test
public void g()
{
System.out.println("its working");
}
}

When this code will be executed, it will ask user to browsername & url, after that execution will get started.
Tags :

run selenium in IE

How to run selenium in chrome browser

How to run selenium script in chrome browser

No comments:

Post a Comment