Skip to content

added locator code file for java for locator strategies Section and added content in diff languages files#2583

Open
rpallavisharma wants to merge 1 commit intoSeleniumHQ:trunkfrom
rpallavisharma:pal-fixLocatorsJava
Open

added locator code file for java for locator strategies Section and added content in diff languages files#2583
rpallavisharma wants to merge 1 commit intoSeleniumHQ:trunkfrom
rpallavisharma:pal-fixLocatorsJava

Conversation

@rpallavisharma
Copy link
Member

@rpallavisharma rpallavisharma commented Feb 6, 2026

User description

added locator code file for java for locator strategies Section and added content in diff languages files

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Added java program for locators and added the code block in diff files

Motivation and Context

it wasn't there.

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Enhancement, Tests


Description

  • Created comprehensive Java test class for locator strategies

  • Replaced inline code snippets with references to actual test methods

  • Updated documentation across multiple language files (English, Japanese, Portuguese, Chinese)

  • Removed old LocatorsTest.java file with ByAll and ByChained examples


Diagram Walkthrough

flowchart LR
  A["Old LocatorsTest.java<br/>ByAll/ByChained examples"] -->|Replaced| B["New LocatorTest.java<br/>8 locator strategy tests"]
  B -->|Referenced in| C["Documentation Files<br/>en/ja/pt-br/zh-cn"]
  C -->|Links to| D["Specific test methods<br/>via gh-codeblock"]
Loading

File Walkthrough

Relevant files
Tests
LocatorTest.java
New comprehensive Java locator test class                               

examples/java/src/test/java/dev/selenium/elements/LocatorTest.java

  • Created new test class with 8 test methods covering all locator
    strategies
  • Implements setUp/tearDown with JUnit 5 annotations for driver
    lifecycle management
  • Tests className, cssSelector, id, name, linkText, partialLinkText,
    tagName, and xpath locators
  • Each test validates element retrieval and assertions on element
    properties
+84/-0   
LocatorsTest.java
Removed deprecated LocatorsTest file                                         

examples/java/src/test/java/dev/selenium/elements/LocatorsTest.java

  • Removed old test file containing ByAll and ByChained locator examples
  • File was replaced with new LocatorTest.java containing individual
    locator strategy tests
+0/-44   
Documentation
locators.en.md
Updated Java examples with test file references                   

website_and_docs/content/documentation/webdriver/elements/locators.en.md

  • Replaced 8 inline Java code snippets with gh-codeblock references to
    LocatorTest.java
  • Each locator strategy (className, cssSelector, id, name, linkText,
    partialLinkText, tagName, xpath) now links to corresponding test
    method
  • Maintains consistent documentation structure with other language
    examples
+12/-20 
locators.ja.md
Updated Japanese locator documentation references               

website_and_docs/content/documentation/webdriver/elements/locators.ja.md

  • Replaced 8 inline Java code snippets with gh-codeblock references to
    LocatorTest.java
  • Updated all locator strategy examples to reference actual test methods
  • Aligned Japanese documentation with English version structure
+12/-20 
locators.pt-br.md
Updated Portuguese-Brazilian locator documentation references

website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md

  • Replaced 8 inline Java code snippets with gh-codeblock references to
    LocatorTest.java
  • Updated all locator strategy examples to reference actual test methods
  • Aligned Portuguese-Brazilian documentation with English version
    structure
+12/-20 
locators.zh-cn.md
Updated Chinese locator documentation references                 

website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md

  • Replaced 8 inline Java code snippets with gh-codeblock references to
    LocatorTest.java
  • Updated all locator strategy examples to reference actual test methods
  • Aligned Simplified Chinese documentation with English version
    structure
+12/-20 

@netlify
Copy link

netlify bot commented Feb 6, 2026

👷 Deploy request for selenium-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 95ed7cf

@qodo-code-review
Copy link
Contributor

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Incorporate removed examples into new tests

The PR removes LocatorsTest.java, which has examples for ByAll and ByChained
locators. These examples should be added to the new LocatorTest.java and linked
in the documentation to prevent content loss.

Examples:

examples/java/src/test/java/dev/selenium/elements/LocatorsTest.java [1-44]

Solution Walkthrough:

Before:

// examples/java/src/test/java/dev/selenium/elements/LocatorTest.java
public class LocatorTest {
    // ... tests for className, cssSelector, id, name, etc.
    // No tests for ByAll or ByChained
}

// examples/java/src/test/java/dev/selenium/elements/LocatorsTest.java
// This file is deleted. It contained:
public class LocatorsTest {
    public void ByAllTest() {
        By example = new ByAll(By.id("password-field"), By.id("username-field"));
        // ...
    }

    public String ByChainedTest() {
        By example = new ByChained(By.id("login-form"), By.id("username-field"));
        // ...
    }
}

After:

// examples/java/src/test/java/dev/selenium/elements/LocatorTest.java
public class LocatorTest {
    // ... existing tests for className, cssSelector, id, name, etc.

    @Test
    public void testByAll() {
        driver.get(".../login.html");
        By all = new ByAll(By.id("username-field"), By.id("password-field"));
        List<WebElement> inputs = driver.findElements(all);
        Assertions.assertEquals(2, inputs.size());
    }

    @Test
    public void testByChained() {
        driver.get(".../login.html");
        By chained = new ByChained(By.id("login-form"), By.id("username-field"));
        WebElement username = driver.findElement(chained);
        Assertions.assertNotNull(username);
    }
}
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies that the PR removes valuable examples for ByAll and ByChained locators without replacing them, which is a significant content regression for a PR focused on improving examples.

Medium
General
Improve test robustness for tag name

In testTagName, use findElements to get all tags and assert that at least one
has the correct href, making the test more robust against DOM changes.

examples/java/src/test/java/dev/selenium/elements/LocatorTest.java [71-76]

 @Test
 public void testTagName() {
-    WebElement element = driver.findElement(By.tagName("a"));
-    Assertions.assertNotNull(element);
-    Assertions.assertEquals("https://www.selenium.dev/", element.getAttribute("href"));
+    java.util.List<WebElement> elements = driver.findElements(By.tagName("a"));
+    Assertions.assertTrue(elements.stream()
+            .anyMatch(e -> "https://www.selenium.dev/".equals(e.getAttribute("href"))));
 }
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: This is a good suggestion that improves the robustness of the testTagName test by using findElements and checking for any match, which prevents the test from breaking if the element order changes.

Low
Improve test robustness for class name

In testClassName, use findElements to retrieve all elements with the class
"information" and verify at least one is an tag to improve test reliability.

examples/java/src/test/java/dev/selenium/elements/LocatorTest.java [29-34]

 @Test
 public void testClassName() {
-    WebElement element = driver.findElement(By.className("information"));
-    Assertions.assertNotNull(element);
-    Assertions.assertEquals("input", element.getTagName());
+    java.util.List<WebElement> elements = driver.findElements(By.className("information"));
+    Assertions.assertTrue(elements.stream()
+            .anyMatch(e -> "input".equals(e.getTagName())));
 }
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: This suggestion correctly identifies that using findElement with a non-unique class name is brittle. The proposed change to use findElements and check for any match makes the test more robust and reliable.

Low
Refine XPath locator specificity

Refine the XPath locator in testXpath to be more specific by including the
type='radio' attribute, preventing potential mismatches.

examples/java/src/test/java/dev/selenium/elements/LocatorTest.java [80]

-WebElement element = driver.findElement(By.xpath("//input[@value='f']"));
+WebElement element = driver.findElement(By.xpath("//input[@value='f' and @type='radio']"));
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: This is a valid suggestion that improves the specificity and robustness of the XPath locator, which is a good practice for writing stable tests.

Low
Extract URL to constant

Extract the hard-coded URL in setUp to a private static final constant to
improve maintainability.

examples/java/src/test/java/dev/selenium/elements/LocatorTest.java [18]

-driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html");
+private static final String BASE_URL = "https://www.selenium.dev/selenium/web/locators_tests/locators.html";
+...
+driver.get(BASE_URL);

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 3

__

Why: This is a valid code style and maintainability suggestion. However, since the URL is used only once in the file, its impact is minor.

Low
  • More

@rpallavisharma rpallavisharma changed the title added locator file for java and added content in diff languages files added locator code file for java for Locator Strategies Section and added content in diff languages files Feb 6, 2026
@rpallavisharma rpallavisharma changed the title added locator code file for java for Locator Strategies Section and added content in diff languages files added locator code file for java for locator strategies Section and added content in diff languages files Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant