SeleniumHQ / seleniumhq.github.io

Official Selenium website and documentation
https://selenium.dev
Apache License 2.0
1.09k stars 1.28k forks source link

added code and content for frames #1915

Closed pallavigitwork closed 1 week ago

pallavigitwork commented 2 weeks ago

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.

added content and code for frames example

Description

added content and code for frames example

Motivation and Context

added content and code for frames example as it wasn't there

Types of changes

Checklist


PR Type

enhancement, documentation


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
FramesTest.java
Add Java test for iframe interactions using Selenium         

examples/java/src/test/java/dev/selenium/interactions/FramesTest.java
  • Added a new test class FramesTest for handling iframes.
  • Implemented a test method informationWithElements to demonstrate
    switching between iframes.
  • Utilized different methods for switching to iframes: by WebElement,
    name, and index.
  • Included assertions to verify iframe content and interactions.
  • +70/-3   
    Documentation
    frames.en.md
    Update English documentation with iframe example references

    website_and_docs/content/documentation/webdriver/interactions/frames.en.md
  • Updated documentation to include example code for switching iframes.
  • Replaced inline Java code with references to the new Java test file.
  • +15/-27 
    frames.ja.md
    Update Japanese documentation with iframe example references

    website_and_docs/content/documentation/webdriver/interactions/frames.ja.md
  • Updated Japanese documentation to include example code for switching
    iframes.
  • Replaced inline Java code with references to the new Java test file.
  • +9/-25   
    frames.pt-br.md
    Update Portuguese documentation with iframe example references

    website_and_docs/content/documentation/webdriver/interactions/frames.pt-br.md
  • Updated Portuguese documentation to include example code for switching
    iframes.
  • Replaced inline Java code with references to the new Java test file.
  • +9/-25   
    frames.zh-cn.md
    Update Chinese documentation with iframe example references

    website_and_docs/content/documentation/webdriver/interactions/frames.zh-cn.md
  • Updated Chinese documentation to include example code for switching
    iframes.
  • Replaced inline Java code with references to the new Java test file.
  • +12/-26 

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

    netlify[bot] commented 2 weeks ago

    Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    Latest commit ab5111cebad3ebe8abdb3bb302440ff6a0c67a32
    codiumai-pr-agent-pro[bot] commented 2 weeks ago

    PR Reviewer Guide ๐Ÿ”

    โฑ๏ธ Estimated effort to review: 3 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšชโšช
    ๐Ÿงช PR contains tests
    ๐Ÿ”’ No security concerns identified
    โšก Key issues to review

    Resource Management
    The WebDriver instance is not properly closed using try-with-resources or a finally block, which may lead to resource leaks. Test Structure
    The test method contains multiple assertions and operations, which violates the single responsibility principle for unit tests. Consider splitting into multiple test methods. Hardcoded Wait
    The use of implicitlyWait with a fixed duration may lead to unreliable tests. Consider using explicit waits for better test stability.
    codiumai-pr-agent-pro[bot] commented 2 weeks ago

    PR Code Suggestions โœจ

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Use try-with-resources to automatically close the WebDriver ___ **Use a try-with-resources statement to ensure the WebDriver is closed properly, even
    if an exception occurs.** [examples/java/src/test/java/dev/selenium/interactions/FramesTest.java [31-71]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1915/files#diff-c32bcc5edb4bcccf7c3694e3a6103af9a1bb389f8a7b6afee1821662f87df0baR31-R71) ```diff -WebDriver driver = new ChromeDriver(); -driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); +try (WebDriver driver = new ChromeDriver()) { + driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); + + // Navigate to Url + driver.get("https://www.selenium.dev/selenium/web/iframes.html"); + + ... + + // No need for explicit quit() call +} -// Navigate to Url -driver.get("https://www.selenium.dev/selenium/web/iframes.html"); - -... - -//quit the browser -driver.quit(); - ```
    Suggestion importance[1-10]: 9 Why: The suggestion to use try-with-resources ensures that the WebDriver is closed properly, even if an exception occurs, which is a best practice for resource management in Java.
    9
    Replace implicit wait with explicit wait for better control and reliability ___ **Use a WebDriverWait instead of implicit wait to improve test reliability and
    performance.** [examples/java/src/test/java/dev/selenium/interactions/FramesTest.java [32]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1915/files#diff-c32bcc5edb4bcccf7c3694e3a6103af9a1bb389f8a7b6afee1821662f87df0baR32-R32) ```diff -driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); +WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); +wait.until(ExpectedConditions.presenceOfElementLocated(By.id("iframe1"))); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Using WebDriverWait instead of an implicit wait provides more precise control over wait conditions, improving test reliability and performance.
    8
    Enhancement
    Use a more specific assertion for checking text content ___ **Replace the boolean assertion with a more specific assertion that checks for the
    exact text content.** [examples/java/src/test/java/dev/selenium/interactions/FramesTest.java [42]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1915/files#diff-c32bcc5edb4bcccf7c3694e3a6103af9a1bb389f8a7b6afee1821662f87df0baR42-R42) ```diff -assertEquals(true, driver.getPageSource().contains("We Leave From Here")); +assertTrue(driver.getPageSource().contains("We Leave From Here"), "Expected text 'We Leave From Here' not found in page source"); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: The suggestion improves the clarity of the test by using assertTrue with a descriptive message, making it easier to understand test failures.
    7
    Combine redundant frame switching tests into a single, more efficient test method ___ **Combine the two separate frame switching tests into a single test method to reduce
    redundancy and improve test efficiency.** [examples/java/src/test/java/dev/selenium/interactions/FramesTest.java [38-59]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1915/files#diff-c32bcc5edb4bcccf7c3694e3a6103af9a1bb389f8a7b6afee1821662f87df0baR38-R59) ```diff -//switch To IFrame using Web Element +// Test frame switching using both WebElement and name/id WebElement iframe = driver.findElement(By.id("iframe1")); -//Switch to the frame + +// Switch to frame using WebElement driver.switchTo().frame(iframe); -assertEquals(true, driver.getPageSource().contains("We Leave From Here")); -//Now we can type text into email field -WebElement emailE= driver.findElement(By.id("email")); -emailE.sendKeys("admin@selenium.dev"); -emailE.clear(); +assertTrue(driver.getPageSource().contains("We Leave From Here"), "Expected text not found after switching by WebElement"); +WebElement emailElement = driver.findElement(By.id("email")); +emailElement.sendKeys("admin@selenium.dev"); +emailElement.clear(); driver.switchTo().defaultContent(); - -//switch To IFrame using name or id -driver.findElement(By.name("iframe1-name")); -//Switch to the frame -driver.switchTo().frame(iframe); -assertEquals(true, driver.getPageSource().contains("We Leave From Here")); -WebElement email=driver.findElement(By.id("email")); -//Now we can type text into email field -email.sendKeys("admin@selenium.dev"); -email.clear(); +// Switch to frame using name +driver.switchTo().frame("iframe1-name"); +assertTrue(driver.getPageSource().contains("We Leave From Here"), "Expected text not found after switching by name"); +emailElement = driver.findElement(By.id("email")); +emailElement.sendKeys("admin@selenium.dev"); +emailElement.clear(); driver.switchTo().defaultContent(); ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: The suggestion reduces redundancy and improves test efficiency by combining similar frame switching tests, although it may slightly reduce test granularity.
    6
    pallavigitwork commented 1 week ago

    Thank you @harsha509 :)