Skip to content

Add example for asserting with java on getting_started/using_selenium #1672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

harshitBhardwaj97
Copy link
Contributor

@harshitBhardwaj97 harshitBhardwaj97 commented Apr 15, 2024

User description

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

Example for Asserting added for java language, by creating AssertionsTest.java file and
adding relevant code lines reference in the markdown file.

Motivation and Context

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.

Type

enhancement, documentation


Description

  • Added AssertionsTest.java demonstrating how to use assertions in Selenium tests with Java.
  • Updated English and Japanese versions of the "Using Selenium" documentation to reference the new assertions example.
  • Made minor formatting adjustments to the documentation for better readability and consistency.

Changes walkthrough

Relevant files
Enhancement
AssertionsTest.java
Add Java Selenium Assertions Example                                         

examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java

  • Added a new Java test class AssertionsTest with a test method
    assertionTest demonstrating how to perform assertions in Selenium
    tests.
  • Demonstrates assertions on URL, page title, and visibility of an
    element.
  • +34/-0   
    Documentation
    using_selenium.en.md
    Update Selenium Getting Started Guide with Assertions Example

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.en.md

  • Updated the documentation to include a reference to the newly added
    AssertionsTest.java.
  • Minor formatting adjustments for consistency.
  • +27/-14 
    using_selenium.ja.md
    Update Japanese Selenium Documentation with Assertions Example

    website_and_docs/content/documentation/webdriver/getting_started/using_selenium.ja.md

  • Included reference to AssertionsTest.java in the Japanese version of
    the documentation.
  • Made similar formatting improvements as in the English version.
  • +27/-14 

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    …enium/
    
    Example for Asserting added for java language, by creating AssertionsTest.java
    file and adding relevant code lines reference in the markdown file.
    Copy link

    netlify bot commented Apr 15, 2024

    Deploy Preview for selenium-dev ready!

    Name Link
    🔨 Latest commit 340ef4d
    🔍 Latest deploy log https://app.netlify.com/sites/selenium-dev/deploys/661e5058efe98d00082d09e7
    😎 Deploy Preview https://deploy-preview-1672--selenium-dev.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    @CLAassistant
    Copy link

    CLAassistant commented Apr 15, 2024

    CLA assistant check
    All committers have signed the CLA.

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 15, 2024
    Copy link
    Contributor

    PR Description updated to latest commit (9f9d814)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the PR mainly involves adding a new Java test file with a basic Selenium test and updates to documentation. The Java test file is straightforward and follows common Selenium testing patterns, making it relatively easy to review for experienced developers familiar with Selenium and Java. The documentation changes are minor and mainly involve formatting adjustments and adding references to the new Java example, which should not require significant effort to review.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Hardcoded URL and Title: The test case in AssertionsTest.java uses hardcoded URLs and expected title values, which might make the test fragile if the webpage content changes. Consider fetching expected values from a configuration file or environment variables to make tests more robust.

    Browser Dependency: The test explicitly instantiates a ChromeDriver, making it dependent on Chrome and the ChromeDriver being available and correctly set up on the machine running the test. It's recommended to abstract the WebDriver creation process to support different browsers more flexibly and to handle driver setup more gracefully.

    Missing Cleanup in Case of Assertions Failure: The driver.quit() call is at the end of the test method, which might not be executed if an assertion fails earlier in the test. It's better to use a try-finally block or a test framework feature (like @after or @AfterEach annotations) to ensure resources are always freed up.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Ensure resources are cleaned up properly by using a try-finally block or test teardown methods.

    Consider using a try-finally block or a testing framework's @after or @AfterEach
    annotation to ensure that the WebDriver is quit even if an assertion fails. This prevents
    the browser from remaining open if a test fails.

    examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java [15-31]

     WebDriver driver = new ChromeDriver();
    -driver.manage().window().maximize();
    -...
    -driver.quit();
    +try {
    +    driver.manage().window().maximize();
    +    ...
    +} finally {
    +    driver.quit();
    +}
     
    Separate test setup from test logic for better readability and maintenance.

    For better code readability and maintenance, consider separating the test setup (e.g.,
    opening a browser, navigating to a page) from the actual test logic into different methods
    or using a setup method annotated with @before or @beforeeach.

    examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java [15-18]

    -WebDriver driver = new ChromeDriver();
    -driver.manage().window().maximize();
    -...
    -driver.get("https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/");
    +WebDriver driver;
     
    +@BeforeEach
    +public void setUp() {
    +    driver = new ChromeDriver();
    +    driver.manage().window().maximize();
    +    driver.get("https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/");
    +}
    +
    +@Test
    +public void assertionTest() {
    +    ...
    +}
    +
    Enhancement
    Improve test reliability by using explicit waits before assertions.

    To improve test reliability, consider adding explicit waits (e.g., WebDriverWait) before
    assertions that depend on the state of the web page, ensuring elements are loaded before
    interacting with them or checking their state.

    examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java [28-29]

    -boolean isLinkDisplayed = driver.findElement(By.xpath("//a/span[text()='Using Selenium']")).isDisplayed();
    +WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    +boolean isLinkDisplayed = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a/span[text()='Using Selenium']"))).isDisplayed();
     assertTrue(isLinkDisplayed);
     
    Enhance test comprehensiveness by checking that elements are not only visible but also clickable.

    To make the test more comprehensive, consider checking not only the visibility of the link
    but also that it is clickable, ensuring that the link is not only displayed but also
    functional.

    examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java [28-29]

    -boolean isLinkDisplayed = driver.findElement(By.xpath("//a/span[text()='Using Selenium']")).isDisplayed();
    -assertTrue(isLinkDisplayed);
    +WebElement link = driver.findElement(By.xpath("//a/span[text()='Using Selenium']"));
    +WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    +wait.until(ExpectedConditions.elementToBeClickable(link));
    +assertTrue(link.isDisplayed());
     
    Maintainability
    Use a configuration file or constants class for managing test data.

    Instead of hardcoding the URL in the test method, consider using a configuration file or
    constants class to manage URLs and other test data. This makes the tests easier to
    maintain and run against different environments.

    examples/java/src/test/java/dev/selenium/getting_started/AssertionsTest.java [18-20]

    -driver.get("https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/");
    +driver.get(AppConfig.BASE_URL);
     ...
    -String expectedUrl = "https://www.selenium.dev/documentation/webdriver/getting_started/using_selenium/";
    +String expectedUrl = AppConfig.BASE_URL;
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    Example for Setting up and Tearing down added
    for java language, by creating SetupAndTeardownTest.java
    file, and adding relevant code lines reference in the markdown file(s).
    Assertion and Setup/Teardown examples linked with
    existing UsingSeleniumTest.java, removed AssertionsTest.java
    and SetupAndTeardownTest.java
    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Could you please also make changes to the Portuguese and Chinese translations?

    1. BeforeAll and AfterAll functions removed.
    2. Print statements removed.
    3. driver.manage().window().maximize() removed.
    4. Driver navigation and implicit wait code moved into test body.
    5. Markdown changes done in all the 4 languages.
    @harshitBhardwaj97
    Copy link
    Contributor Author

    As asked, the following changes have been done -

    1. BeforeAll and AfterAll functions removed.
    2. Print statements removed.
    3. driver.manage().window().maximize() removed.
    4. Driver navigation and implicit wait code moved into test body.
    5. Markdown changes done in all the 4 languages.

    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thank you, @harshitBhardwaj97!

    @diemol diemol merged commit cf913c3 into SeleniumHQ:trunk Apr 16, 2024
    @harshitBhardwaj97
    Copy link
    Contributor Author

    Thank you, @harshitBhardwaj97!
    Welcome Sir, I will try to contribute more. Have a nice day.

    selenium-ci added a commit that referenced this pull request Apr 16, 2024
    …#1672)
    
    * Add example for asserting with java on page getting_started/using_selenium/
    
    Example for Asserting added for java language, by creating AssertionsTest.java
    file and adding relevant code lines reference in the markdown file.
    
    * Documentation updated for using_selenium.ja.md
    
    * Add example of Setting up and Tear down with java
    
    Example for Setting up and Tearing down added
    for java language, by creating SetupAndTeardownTest.java
    file, and adding relevant code lines reference in the markdown file(s).
    
    * Assertion and Setup/Teardown examples linked
    
    Assertion and Setup/Teardown examples linked with
    existing UsingSeleniumTest.java, removed AssertionsTest.java
    and SetupAndTeardownTest.java
    
    * Do the requested changes
    
    1. BeforeAll and AfterAll functions removed.
    2. Print statements removed.
    3. driver.manage().window().maximize() removed.
    4. Driver navigation and implicit wait code moved into test body.
    5. Markdown changes done in all the 4 languages.
    
    [deploy site] cf913c3
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    documentation Improvements or additions to documentation enhancement New feature or request Review effort [1-5]: 2
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants