This is a blog post about how to use Selenium to automate web testing in Chrome browser
Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS, and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. We will be working with Java. In this article, let us consider a test case in which we will try to automate the following scenarios in the Google Chrome browser.
# Launch Chrome browser.
# Maximize the browser.
# Open URL: https://www.google.com/
For invoking the chrome browser, we need the Eclipse IDE, Selenium Grid(version 4), and Chrome web Driver.
Installation
Eclipse IDE: One of the prerequisites for running our software is having Java JDK installed on your device. Java JDK is a software development kit that allows you to create and run Java applications. You can download it from the official website and follow the instructions to install it. Another prerequisite is having Eclipse IDE, which is an integrated development environment that supports Java and other programming languages. Eclipse IDE provides a user-friendly interface and many tools for developing and debugging your code. You can download it from the official website and install it as well.
Selenium: Download the Selenium latest stable version
Web Driver: Web drivers is a package to interact with a web browser. Download Chrome Driver according to your Chrome Version. Use this link https://sites.google.com/chromium.org/driver/downloads
Step by Step Implementation
Step 1:
Open the Eclipse IDE and create a new Java project. Right-click on File>New>Project. Select Maven Project then click Next
Step2:
Click on Create a simple project then click Next
Step 3:
New Maven Project box opens where fill out Group id and Artifact id names and in the package the jar files are included. Then click on to Finish button.
Now a new project is created which includes src file, JRE System Library, target folder, pom.xml file (look at the highlighted one left side).
Step 4:
Copy chromedriver.exe file from the downloads and paste it in your project. you can get those exe file from this link https://sites.google.com/chromium.org/driver/downloads .
Step 5:
Double click on pom.xml file to include dependencies for our project. The 2 dependency needed are
1. selenium-java
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.6.0</version> </dependency>
2. webdrivermanager
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.2</version> </dependency>
You can look at https://mvnrepository.com/ to get the latest stable version of those dependency.
Step 6:
Now Save the file which automatically downloads the maven files. Then click on Run As>Maven Build. Now you can see a Maven Dependency folder that includes all jar files that is required.
Step 7:
Now Right click on src/main/java then click New>Package
Then fill out the package name and click Finish button.
Then again Right click on the Package name and click New>Class and fill the class name in the pop up window.
Step 8:
Double click on the java file that is been created and write the code and then right click and then Run java program. Here the Chrome driver is automated and the www.Google.com page is open.
package HelloGoogle; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import io.github.bonigarcia.wdm.WebDriverManager; public class OpenBrowser { public static void main(String[] args) { String URL = "http://www.Google.com/"; WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); // Instantiate a ChromeDriver class. WebDriver driver = new ChromeDriver(options); // Maximize the browser driver.manage().window().maximize(); // Launch Website driver.get(URL); } }
Congratulations! You have successfully created a Java project using Maven in Eclipse IDE. You can now write your own code and run it using Maven commands. In the next tutorial, we will learn how to add some dependencies and plugins to our project using Maven.
hi i appreciate your efforts ..
can you explain a lit bit that why you added this >>>>options.addArguments("--remote-allow-origins=*"); what kind of chrome behaviour is it??what is the use of addArugument using chromeoptions class?/