Selenium Program to Login to a Specific Web Page
Selenium RC (Selenium Remote Control) is one of the Selenium Components which was developed by Paul Hammant. It is an automation testing tool for web applications. To create test scripts, testers are allowed to use many programmings languages such as Java, Python, Ruby, C#, JavaScript, Perl, and PHP. Here we are using Java for writing test scripts.
Tip: One should be well versed with Eclipse and JDK understanding concepts.
Procedure:
- Create New Project, Click on File
- Create a new java class
- Download the latest Chrome Driver and Selenium Web Driver for Java
- Click on Classpath and Select Add External JARs.
These above steps are as depicted below with help of visual aids for better understanding.
Step 1: To Create New Project, Click on File –> New –> Java Project.
After creating a new project, give the project name “Login_Test” and click on Finish.
Step 2: Create a new java class
For creating a new Java class, Right-click on “Test”, Click on new and select Class. Given the class name “Login_test” and click Finish.
Now we have to import Selenium Web Driver and Chrome Driver.
Step 3: Download the latest Chrome Driver and Selenium Web Driver for Java. After downloading, unzip the downloaded files. Right-click on “Testing”, Click on Build Path, and Select Configure Build Path.
Step 4: Click on Classpath
4.1: Initially alongside select Add External JARs.
4.2: Now go to the Selenium driver downloaded file, select both these jar files and click on ‘Open’.
4.3: Go to the same folder, open the “libs” folder, select all the jar files, and click on ‘Open’.
After adding all these required Jar files, below is the given code to test the login page.
Example
Java
package login_test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args)
{
System.setProperty(
"webdriver.chrome.driver" ,
"C:\\Users\\Admin\\chromedriver_win32\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.findElement(By.id( "luser" ))
.sendKeys( "xyz@gmail.com" );
driver.findElement(By.id( "password" ))
.sendKeys( "xyz12345" );
driver.findElement(By.className( "signin-button" ))
.click();
}
}
|
After adding this code, click on the ‘Run’ button.
Chrome Driver will start successfully and Chrome browser will open. It will automatically open the GeeksforGeeks login page and add details for login. If the details are valid, it will redirect to its website.
Last Updated :
30 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...