Tuesday, September 10, 2019

Post#8.Open Browser


Open Browsers

As selenium WebDriver supports only web based application, opening a browser for operation is must. You cannot access already opened browser in selenium
Selenium supports only web-based applications and to open them we need a browser. Selenium can support various browsers for test automation.
Now we will explain how to set up drivers for the different browsers available in the market. Let’s discuss them one by one.

Open Firefox (in Selenium version 2)

WebDriver driver=new FirefoxDriver();

In the above line which opens the firefox browser,I hope reader is familiar with creating object in java.
WebDriver - webdriver is the interface which is inherited from SearchContext 
new - new is the keyword in java which creates an object (address space)in the heap area of CPU
FirefoxDriver() - FirefoxDriver() is a constuructor of FirefoxDriver class which implements all the methods in the present in the webdriver interface and this opens the firefox Browser .
driver - driver is reference variable ,which refers the address space created on heap.
; - line ending statement

We call this process as run-time polymorphism and upcasting in java.

Java Code:
package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenFirefox
{
              public static void main(String[] args) throws Exception
              {
                             WebDriver driver=new FirefoxDriver();
              }
}
WebDriver do not require any additional utility to be set before launching the browser (Up to 2.53 version of Jar File). FirefoxDriver comes as a part of Selenium package in version 2.

Open Firefox (in Selenium version 3)

When using Selenium 3, you have to download geckodriver. Just like the other drivers available to Selenium, Mozilla has released geckodriver executable that will run alongside the browser.

Set the system’s property to point to the location where the geckodriver file is kept.

If you are not doing so, it will throw exception “java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;”

Java Code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenFirefox
{
              public static void main(String[] args) throws Exception
              {
System.setProperty("webdriver.gecko.driver","c:\\path\\geckodriver.exe");
                             WebDriver driver = new FirefoxDriver();
              }
}

Open Chrome:
Java Code:
package pack2;

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

public class OpenChrome
{
              public static void main(String[] args) throws Exception
              {
                             // we have to set the .exe file path to open Google chrome
System.setProperty("webdriver.chrome.driver", "c:\\path\\chromedriver.exe");
                             WebDriver driver = new ChromeDriver();
              }
}

Open Internet Explorer:
Java Code:
package pack2;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class OpenIE
{
              public static void main(String[] args) throws Exception
              {
                             // we have to set the .exe file path to open Internet Explorer
System.setProperty("webdriver.ie.driver", " c:\\path\\IEDriverServer.exe");
                             WebDriver driver = new InternetExplorerDriver();
              }
}
Open Opera:
Java Code:
package pack2;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.opera.OperaDriver;

public class OpenOpera
{
              public static void main(String[] args) throws Exception
              {
                             // we have to set the .exe file path to open Opera
System.setProperty("webdriver.opera.driver", "C:\\OperaDriverServer.exe");
                             WebDriver driver = new OperaDriver()
              }
}

Open Microsoft Edge:
Java Code:
package pack2;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class OpenEdge
{
              public static void main(String[] args) throws Exception
              {
                             // we have to set the .exe file path to open ms Edge
System.setProperty("webdriver.edge.driver", "C:\\MicrosoftWebDriver.exe");
                             WebDriver driver = new EdgeDriver();
              }
}


Open Safari browser:
SafariDriver comes bundled with the Selenium server’s package, thus user is not required to download or set any external entity.
Java Code:
package pack2;

import org.openqa.selenium.safari.SafariDriver;

public class OpenSafariBrowser {
              public static void main(String[] args) {
                            
                             SafariDriver driver = new SafariDriver();
              }
}

No comments:

Post a Comment