0% found this document useful (0 votes)
31 views6 pages

Selenium Login Automation Example

The document contains 6 Java programs that automate tasks using Selenium WebDriver. Program 1 logs into a web page, Program 2 checks if a test case passed or failed, Program 3 counts the number of hyperlinks on a page, Program 4 counts the number of options in a dropdown menu, and Programs 5 and 6 open browsers and navigate to pages.

Uploaded by

Shahnawaz Sheikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Selenium Login Automation Example

The document contains 6 Java programs that automate tasks using Selenium WebDriver. Program 1 logs into a web page, Program 2 checks if a test case passed or failed, Program 3 counts the number of hyperlinks on a page, Program 4 counts the number of options in a dropdown menu, and Programs 5 and 6 open browsers and navigate to pages.

Uploaded by

Shahnawaz Sheikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM 3 : AUTOMATION PROGRAM TO LOGIN

INTO A WEB PAGE


package webdrivercommand;

import [Link];
import [Link];
import [Link];

public class Loginwebpage {

public static void main(String[] args) throws


InterruptedException {
//To Access ChromeBrowser we need a Chrome Driver
//set System Property
[Link]("[Link]",
"C://Selenium//[Link]");
WebDriver driver=new ChromeDriver();
[Link]("[Link]
//Wait for 5 seconds=5000ms
[Link](5000);
//close the browser
[Link]();
}

PROGRAM 4 : TO CHECK WHETHER A TEST CASE


HAS PASSED OR FAILED
package webdrivercommand;

import [Link];

import [Link];

import [Link];

public class Login {

public static void main(String[] args) throws InterruptedException {

//Launch Chrome Browser

[Link]("[Link]","C://Selenium//[Link]");

WebDriver driver=new ChromeDriver();


//To navigate to URL

[Link]("[Link]

[Link](5000);

[Link]([Link]("mobileNo")).sendKeys("9700339582");

[Link](5000);

[Link]([Link]("password")).sendKeys("hanuman");

[Link](5000);

[Link]([Link]("//b[@id='mainErr']/following::*[5]")).click();

[Link](5000);

//Verification Point

String o="[Link]

String z=[Link]();

if([Link](o))

[Link]("Login Sucessful_Test Passed");

else

[Link]("Login UnSucessful_TestFailed");

//Close Site

[Link]();

}
PROGRAM 5 : TOTAL NUMBER OF OBJECTS ON A
WEB PAGE (HYPERLINK OBJECTS)
package webdrivercommand;
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class Totalnoofobjects {

public static void main(String[] args) throws InterruptedException {


// TODO Auto-generated method stub
[Link]("[Link]","C:\\Selenium\\[Link]");
WebDriver driver = new ChromeDriver();
[Link](5000);
[Link]("[Link]
[Link](5000);
List<WebElement> e = [Link]([Link]("a"));
[Link]([Link]());
for(int i=0; i<[Link](); i=i+1){
[Link]([Link](i).getText());
}
[Link]();
}
}

PROGRAM 6 : TOTAL NUMBER OF OBJECTS


PRESENT IN A COMBO BOX
package webdrivercommand;

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];
public class Totalnoofobjectscombobox {

public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

[Link]("[Link]","C://Selenium//[Link]");

WebDriver driver=new ChromeDriver();

//To navigate to URL

[Link]("[Link]

[Link](5000);

//Syntax:

//[Link]([Link]("value")).OperationMethod();

//Click on Register Link

[Link]([Link]("REGISTER")).click();

[Link](5000);

//Pass text into Textbox for FirstName

[Link]([Link]("firstName")).sendKeys("Madhwaraj");

[Link](5000);

[Link]([Link]("lastName")).sendKeys("Prof");

[Link](5000);

[Link]([Link]("phone")).sendKeys("9703336699");

[Link](5000);

[Link]([Link]("userName")).sendKeys("abcd@[Link]");

[Link](5000);

[Link]([Link]("address1")).sendKeys("chennai fasf");

[Link](5000);

//[Link]([Link]("address2")).sendKeys("chennai fasf");

//[Link](5000);

[Link]([Link]("city")).sendKeys("chennai");
[Link](5000);

[Link]([Link]("state")).sendKeys("TN");

[Link](5000);

[Link]([Link]("postalCode")).sendKeys("5000221");

[Link](5000);

//Select Class in SWD

Select s=new Select(([Link]([Link]("country"))));

//Get all the option from dropdown list and assign into List

List <WebElement> listOptionDropdown = [Link]();

// Count the item dropdown list and assign into integer variable

int dropdownCount = [Link]();

//Print the total count of dropdown list using integer variable

[Link]("Total Number of item count in dropdown list = " + dropdownCount);

//selectByIndex();

//selectByVisibleText("INDIA");

[Link]("INDIA");

[Link](5000);

[Link]([Link]("email")).sendKeys("abcd@[Link]");

[Link](5000);

[Link]([Link]("password")).sendKeys("Madhvaraj");

[Link](5000);

[Link]([Link]("confirmPassword")).sendKeys("Madhvaraj");
[Link](5000);

//Image Button

//[Link]([Link]("Submit")).click();

//[Link](5000);

//close browser

[Link]();

Common questions

Powered by AI

Explicit waits improve the robustness of Selenium scripts by allowing them to wait for specific conditions to occur before proceeding. Unlike `Thread.sleep()`, which pauses execution for a fixed duration regardless of the state of the web page, explicit waits dynamically wait for elements to be present, visible, or interactable. This approach minimizes unnecessary delays and reduces test run time, leading to more efficient and stable test executions. It also enhances script reliability by adapting to varying page load times .

Closing the browser after executing a Selenium WebDriver script is critical to freeing up system resources, preventing memory leaks, and avoiding issues with subsequent test runs. Neglecting this step can lead to an accumulation of open browser instances, consuming excessive CPU and memory, which can slow down or crash the host system. Additionally, it ensures that each test runs in a clean environment without leftover data or settings affecting the results .

Providing login credentials in Selenium scripts poses several security risks, primarily related to hardcoding sensitive information like usernames and passwords. This practice can lead to unauthorized access if the scripts are shared without proper access controls. Maintaining scripts in version control systems without encryptions or employing environment variables to encapsulate credentials are recommended to mitigate these risks. Additionally, storing credentials in plaintext increases the risk of exposure if the scripts get compromised .

The strategy used for verifying a test case involves comparing the current URL after a login attempt to an expected URL. If they match, the test is marked as 'Login Successful_Test Passed'; otherwise, 'Login Unsuccessful_Test Failed'. The strength of this approach is its simplicity and clarity—direct URL verification can immediately confirm a navigation success. However, its weakness lies in the fact that it does not account for intermediary failures (such as incorrect login credentials) that may result in navigation to error pages rather than staying on the same page .

The provided Selenium programs handle synchronization issues by using `Thread.sleep()` to suspend the execution of the program for a specified period. This is used to wait for elements to load or actions to be completed, such as waiting for 5 seconds between actions to ensure that elements are present and interactable .

WebElement lists in Selenium WebDriver are crucial for interacting with multiple elements by allowing batch processing of elements identified by a common attribute, such as tag name. For instance, fetching a list of WebElements with `findElements(By.tagName("a"))` enables scripts to iterate over hyperlinks on a page, facilitating operations like extracting text, verifying attributes, or executing clicks programmatically. This is particularly useful for comprehensive web page verifications and automating repeated interactions across multiple elements .

The Selenium WebDriver program handles dropdown list selections using the Select class, which provides methods like `selectByVisibleText()`. This approach selects a dropdown option based on its visible text. Challenges associated with this technique include incorrect locator strategies that fail to identify the correct dropdowns, or issues arising from dynamic dropdown lists where options may change based on user interactions or page loads .

Setting the system property for the ChromeDriver in Selenium is crucial as it informs the WebDriver about the location of the ChromeDriver executable file. This is necessary for the WebDriver to initialize and control the Chrome browser for automated tasks. Without specifying this path, the WebDriver cannot launch the browser, which would hinder the execution of automation scripts .

Counting and printing the number of hyperlink objects is significant in automation testing as it helps in validating the presence and completeness of links on a web page. This can be crucial for ensuring that all expected hyperlinked elements are available and functional, which is vital for usability and navigation testing. It helps testers verify that no hyperlinks are missing, which could point to incomplete page rendering or incorrect page versions being loaded .

Using XPath for locating elements can lead to issues such as slower performance due to complex path resolutions and maintenance challenges if the page structure changes. Alternatives to XPath include CSS selectors, which are typically faster and can simplify element locators. CSS is often preferred for its simplicity and readability, allowing for straightforward handling of elements through attributes. Another alternative is using ID or className selectors, which are more direct and reliable if they are unique .

You might also like