Author - StudySection Post Views - 7 views

Automate the website login page with Selenium in Java

To automate a website login page, you can use Selenium WebDriver, which is a popular tool for web application testing. Below is an example using JAVA to demonstrate the automation process:

1. Set up the environment:

    • Install Java Development Kit (JDK).
    • Set up a Java project in an Integrated Development Environment (IDE) like Eclipse or IntelliJ.
    • Add Selenium WebDriver dependencies to your project. If you’re using Maven, add the Selenium dependency to your pom.xml.

2. Write the automation script:

  • Import the necessary Selenium WebDriver classes.
  • Set up the WebDriver for your preferred browser.
  • Locate the login elements on the web page.
  • Input the login credentials and submit the form.
  • Verify the login process.

Maven Dependency
If you’re using Maven, add the following dependencies to your pom.xml:

Example Code
Here is an example script to automate a website login page using Selenium WebDriver and Java:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginAutomation {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Initialize the ChromeDriver
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
try {
// Open the login page
driver.get("https://example.com/login");
// Locate the username field and enter the username
WebElement usernameField = driver.findElement(By.id("username_field_id"));
usernameField.sendKeys("your_username");
// Locate the password field and enter the password
WebElement passwordField = driver.findElement(By.id("password_field_id"));
passwordField.sendKeys("your_password");
// Locate the login button and click it
WebElement loginButton = driver.findElement(By.id("login_button_id"));
loginButton.click();
// Wait for the login process to complete
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement elementAfterLogin = wait.until(
ExpectedConditions.presenceOfElementLocated(By.id("element_after_login_id"))
);
// Check if login was successful
if (elementAfterLogin.isDisplayed()) {
System.out.println("Login successful");
} else {
System.out.println("Login failed");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}

1. Set WebDriver System Property:

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

Replace “path/to/chromedriver” with the actual path to the ChromeDriver executable on your system.

2. Initialize WebDriver:

WebDriver driver = new ChromeDriver(options);

3. Open the Login Page:

driver.get("https://example.com/login");

4. Locate the Elements and Perform Actions:

WebElement usernameField = driver.findElement(By.id("username_field_id"));
usernameField.sendKeys("your_username");

WebElement passwordField = driver.findElement(By.id("password_field_id"));
passwordField.sendKeys("your_password");

WebElement loginButton = driver.findElement(By.id("login_button_id"));
loginButton.click();

5. Wait for Login to Complete:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement elementAfterLogin = wait.until(
ExpectedConditions.presenceOfElementLocated(By.id("element_after_login_id"))
);

6. Verify the Login Process:

if (elementAfterLogin.isDisplayed()) {
System.out.println("Login successful");
} else {
System.out.println("Login failed");
}

7. Close the Browser:

driver.quit();

Notes:

  • Ensure that the ChromeDriver executable is compatible with your installed version of Google Chrome.
  • Adjust the IDs and URLs (username_field_id, password_field_id, login_button_id, element_after_login_id, https://example.com/login) to match the actual elements and login URL of your target website.

This script opens the browser, navigates to the login page, inputs the credentials, clicks the login button, waits for the post-login element to appear, and then verifies if the login was successful.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri