Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

Locating Strategies in Selenium WebDriver

Introduction 
In Selenium WebDriver, locators are used to identify elements on a webpage. The findElement() to find a single element and findElements() to find multiple elements. 
There are multiple ways to uniquely identify a web element within the web page such as ID, 
CSS SelectorXPATH, Name, Class Name, Tag Name, Link Text, And Partial Link Text.

FindElement(): 
method returns a single element or throws an exception if none is found. 

Syntax 
WebElement elementName = driver.findElement(By.LocatorStrategy("ValueOfLocators"));

Below are some examples of Locator Strategy
1. Id: Finds elements with an Id attribute that includes the specified value.

WebElement elementId = driver.findElement(By.id("header_block")); 

2. CSS Selector:
Locates elements matching a CSS selector.

WebElement elementSelector =driver.findElement(By.cssSelector("input[type='submit']")); 

3. Xpath: Locates elements matching an Xpath expression.

WebElement elementXpath =driver.findElement(By.xpath("//input[@type='submit']")); 

4. Name: Finds elements with a name attribute that includes the specified value.

WebElement elementName =driver.findElement(By.name("nameValue")); 

5. ClassName: Finds elements with a class name attribute that includes the specified value.

WebElement elementClassName =driver.findElement(By.className ("classValue")); 

6. TagName: Select elements by their HTML tag.

WebElement elementTagName =driver.findElement(By.tagName ("html tagName"));

7. LinkText: Finds links by their exact text.

WebElement elementLinkText =driver.findElement(By.linkText ("Text in the Link")); 

8. Partial LinkText: Locates anchor elements whose visible text contains the search values. If multiple elements are matching, only the first one will be selected.

WebElement elementPartialLinkText = driver.findElement(By.partialLinkText ("Partial Text in the Link"));

findElements(): method returns a list of matching elements, or an empty list if none are found.

Syntax
List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));

Below are some examples of ID

List<WebElement> elements = driver.findElements(By.id("globalContainer"))

Comments

Popular posts from this blog

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath