How to run selenium script in java using chrome and Firefox browser

How to run selenium script in java using chrome and Firefox browser

How to run selenium script in java using chrome and Firefox browser, In this selenium web driver tutorial, We are going to write an automation script with selenium web driver using chrome browser and Firefox browser. To create a connection between the selenium web driver and chrome browser we need to use chromedriver.exe this driver creates the connection so selenium can read the web elements from the browser.

Selenium Webdriver by default supports firefox that’s by we didn’t face any issue while working with Firefox. In order to execute your script in the different browser like IE, Chrome or safari etc.
When we will work with IE there are different challenges that we will discuss in upcoming tutorials.

Remember: Selenium web driver supports only the latest version of chrome browser.

What is ChromeDriver and why we need chromedriver.exe

Chrome driver is a bridge between the Chrome browser and selenium script. In order to execute test scripts in the Chrome browser, we use ChromeDriver.exe. ChromeDriver is a standalone server when we execute this it will start a server on your machine. At the time of writing the script, we need to define the path of the chromedriver.exe before creating the Instance of the chrome driver.

System.setProperty(“webdriver.chrome.driver”, “c:\\lib\\chromedriver.exe”);

Following are the requirement to write first automation script in selenium using chrome browser.

How to setup Java environment at the machine.

How to setup IDE (Eclipse).

Setup web driver and browser API.

How to run selenium script in java using chrome

Download ChromeDriver.exe. It will start a server at your machine to perform all the test communication.

Download the chrome driver Link–>http://chromedriver.storage.googleapis.com/index.html?path=2.20/

There are multiple options to download Chrome Driver Be careful to download according to Operating System.

Launch Browser Using selenium Web driver.

Create an object of ChromeDriver

WebDriver driver = new ChromeDriver();

You need to set the system property There is Two parameter in setProperty. In 1st   Parameter. enter the name of the driver like “webdriver.chrome.driver” and In 2nd you just need to enter the path of ChromeDriver.exe file that you just download I mention above.

System.setProperty("webdriver.chrome.driver", “c:\\lib\\chromedriver.exe”);

Open Chrome Browser using Selenium Web driver.

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

publicclass Main {
publicstaticvoid main(String[] args) {
  
      System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chromedriver_win32\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
driver.get("http://codebun.com/");
driver.close();
}
}

At the time of working with chrome driver and any other browser like IE and Safari. We will face many issues, errors Like elements is not clickable, Class not found etc here is some common errors with the solution.

ChromeDriver error: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property.
==> To solve this error you need to check the name of the property where you set the path of chrome driver.
The value should be exact “webdriver.chrome.driver”.

How to work with different browsers with selenium web drivers.

Chrome:

System.setProperty(“webdriver.chrome.driver”,”G:\\Lib\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.navigate().to(“https://www.google.com”);

Firefox:

System.setProperty(“webdriver.firefox.bin”,”F:\\Program Files (x86)\\Firefox\\Firefox.exe”);
WebDriver driver = new FirefoxDriver();
driver.navigate().to(“https://www.google.com”);

IE:

System.setProperty(“webdriver.ie.driver”,”G:\\Lib\\internetexplorerdriver.exe”);
WebDriver driver = new InternetExplorerDriver();
driver.navigate().to(“https://www.google.com”);

How to run selenium script in java using Firefox browser

How to run selenium script in Firefox browser, In this selenium web driver tutorial, We are going to write an automation script with selenium web driver using Firefox browser. To create a connection between the selenium web driver and Firefox browser we need to use geckodriver.exe this driver creates the connection so selenium can read the web elements from the browser.

Download the gecko driver

 

Create a demo automation project with Firefox browser

There are multiple options to download gecko driver Be careful to download according to Operating System.

Launch Browser Using selenium Web driver.

Create an object of Firefox Driver

WebDriver driver = new FirefoxDriver();

You need to set the system property There is Two parameter in setProperty. In 1st   Parameter. enter the name of the driver like “webdriver.chrome.driver” and In 2nd you just need to enter the path of ChromeDriver.exe file that you just download I mention above.

System.setProperty("webdriver.chrome.driver", “D:\\Automation\\selenium\\lib_files\\firefox\\geckodriver.exe”);

Complete Source code To open Firefox browser using Selenium Web driver.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {
  public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver",
        "D:\\Automation\\selenium\\lib_files\\firefox\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://codebun.com/");
    driver.close();
  }
}

Run Script and get the result.

 

Add a Comment

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