Getting started with Selenium 4.0

Reading Time: 3 minutes

Selenium has been in the market for the past many years and has been dominating their competitors. In this blog, we will see how we can set up a selenium 4.0 maven project. We will be using Java as our primary language for coding.

Setting up the project

  • Create a new maven project in intellij.
  • Give a name to your project and click on finish.
  • Go to pom.xml file in your newly created project and add the following dependency.
<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0-alpha-6</version>
    </dependency>
  • Now our project is ready and we can now start with our code.
  • Go to src–>main–>test–>java and create a new java file.

chromedriver execution script

  • We will create a small automation script for initializing our webdriver, hitting a URL, printing the current URL, and then closing the driver instance.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

import static java.lang.System.getProperty;

public class SeleniumChromeTest {
    public static void main(String[] args){
        System.setProperty("webdriver.chrome.driver",getProperty("user.dir") + "/DriverFiles/chromedriver");

        //Setting up the driver and giving the path to the driver file
        WebDriver driver = new ChromeDriver();
        //Initialize the driver

        driver.get("https://google.in");
        //Hitting the URL you want to test
        driver.getTitle();
        //Get the title of the page
        System.out.println(driver.getTitle());
        //printing the title of the page on the console
        System.out.println(driver.getCurrentUrl());
        //Printing the URL for verifying that we have hit the correct URL

        driver.close();
        // To close window that has the focus

    }
}

gecko driver (firefox) execution script



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

import static java.lang.System.getProperty;

public class SeleniumFirefoxTest {
    public static void main(String[] args){
        System.setProperty("webdriver.gecko.driver",getProperty("user.dir") + "/DriverFiles/geckodriver");
        //Setting up the driver and giving the path to the driver file
        WebDriver driver = new FirefoxDriver();
        //Initialize the driver

        driver.get("https://google.in");
        //Hitting the URL you want to test
        driver.getTitle();
        //Get the title of the page
        System.out.println(driver.getTitle());
        //printing the title of the page on the console
        System.out.println(driver.getCurrentUrl());
        //Printing the URL for verifying that we have hit the correct URL

        driver.close();
        // To close window that has the focus

    }
}

This is a small introduction for selenium 4.0. In the next blogs we will continue learning selenium.

References


Knoldus-blog-footer-image

Written by 

Ankur is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.

Leave a Reply