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
}
}
- For setting up the web driver we need to download the driver on our local system.
- Chrome driver file download : https://sites.google.com/a/chromium.org/chromedriver/
- Test execution on chrome.
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
}
}
- Firefox driver file download : https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
- Test execution on chrome.
This is a small introduction for selenium 4.0. In the next blogs we will continue learning selenium.