Selenium Script in Chrome and Edge

How to write Selenium test script in Chrome and Edge

Writing Selenium script for Chrome and Edge

In this tutorial, you will learn how to write select test script for Chrome and Edge browsers. Selenium v3.0 has introduced Gecko driver, thereby creating similar approaches to write automation scripts for any browser. Selenium v2 had in-built support for Mozilla Firefox browser.

ALSO READ: Writing automation script for Firefox

The Test case description

We will check login functionality of Gmail in (Chrome and Edge)  browser.

Instantiating Chrome Driver

We need to download the latest chrome driver and store it in our machine. On my machine, it is stored in the folder “E:\\Selenium 3\\chromedriver.exe”. Chrome driver is available at the following URL:

https://sites.google.com/a/chromium.org/chromedriver/

Note: Please match the driver version with your Chrome version

//Using the Chrome  Driver
System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);

//Defining driver
WebDriver driver;

//instantiating the driver

driver = new ChromeDriver();

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver .exe”) is the way of employing the chrome driver for Chrome.

System.setProperty uses two parameters. The first parameter tells to set the chrome driver path to “E:\\Selenium 3\\chrome driver.exe”, which is specified  as second parameter.

Instantiating Edge Driver

//Using the Edge  Driver
System.setProperty(“webdriver.edge.driver”, “E:\\Selenium 3\\MicrosoftWebDriver.exe”);

//Defining driver
WebDriver driver;

//instantiating the driver

driver = new EdgeDriver();

System.setProperty(“webdriver.edge.driver”, “E:\\Selenium 3\\ MicrosoftWebDriver.exe”) is the way of employing the edge driver for Edge.

System.setProperty uses two parameters. The first parameter tells to set the Microsoft Edge driver path to E:\\Selenium 3\\MicrosoftWebDriver.exe, which is specified  as second parameter.

To test  Gmail login functionality in chrome(browser)

After we instantiate the Chrome Driver, we are ready to go and launch GMAIL website.

//Launching the gmail login Page
driver.get(“http://www.gmail.com ”);

This is simple, you just provide the URL of the page to driver.get() method. This method will open the page.

The next step is to enter username ,password and click on the sign-in button .

//Enter userid
WebElement element = driver.findElement(By.id(“Email”));
element.sendKeys(“xyz@gmail.com”);

//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Note: We will explain the concepts of WAIT in the new chapters

//Click on next  button (for password)
WebElement elementNext = driver.findElement(By.id(“next”));
elementNext.click();

//Enter Password
WebElement element1 = driver.findElement(By.id(“Passwd”));
element1.sendKeys(“Password”);

/Click on next  button (for login)
WebElement elementNext1 = driver.findElement(By.id(“next1”));
elementNext1.click();

To test  Gmail login functionality in Edge (browser)

You only need to change 3 lines for Microsoft Edge Browser in the previous code:

//Using the Edge  Driver
System.setProperty(“webdriver.edge.driver”, “E:\\Selenium 3\\MicrosoftWebDriver.exe”);

//Defining driver
WebDriver driver;

//instantiating the driver

driver = new EdgeDriver();

Complete Code for the example

For your benefit, I am providing complete code for the program. Replace the class name and package name as the case may be.

package basicSeleniumScripts;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.concurrent.*;

public class Loginfun {

public static void main(String[] args) {

//initialize Chrome driver
System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);

//defining driver

WebDriver driver;

driver= new ChromeDriver();

//Note: Please replace the 3 lines above with Edge code (as explained in previous section), if you are running it for EDGE

//Open gmail
driver.get(“http://www.gmail.com”);

// Enter userd id
WebElement element = driver.findElement(By.id(“Email”));
element.sendKeys(“xyz@gmail.com”);

//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

/Click on next  button (for password)
WebElement elementNext = driver.findElement(By.id(“next”));
elementNext.click();

//Enter Password
WebElement element1 = driver.findElement(By.id(“Passwd”));
element1.sendKeys(“Password”);

/Click on next  button (for login)
WebElement elementNext1 = driver.findElement(By.id(“next1”));
elementNext1.click();

}

}

What’s Next?

In this tutorial, we explained how to write Selenium test script in Chrome and Edge. You can go and read more Selenium tutorials published on this blog.

You can download Selenium and drivers from official website.

Are you looking to become an Automation tester and learn Selenium?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

Selenium-Certification-training-with-Job-readiness