Skip to content

Innazh/add browser options examples java #1865

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

Merged

Conversation

innazh
Copy link
Contributor

@innazh innazh commented Aug 17, 2024

User description

Add missing Driver Options examples for Java & incorporate those into the docs

Description

  • Added java examples for Driver Options, specifically: browserName, browserVersion, platformName, acceptInsecureCerts, timeouts (Script Timeout, Page Load Timeout, Implicit Wait Timeout), unhandledPromptBehavior, setWindowRect, strictFileInteractability
  • Added the example lines of the code above to the related page in the docs (options*.md files in )

Motivation and Context

The examples were missing in the docs, I'm replacing the "Add example" badges.

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

Tests, Documentation


Description

  • Added comprehensive Java test examples for various ChromeOptions settings in OptionsTest.java.
  • Updated documentation across multiple languages (English, Japanese, Portuguese, Chinese) to include Java code examples with correct line references.
  • Replaced placeholder badges in documentation with actual code references to improve clarity.

Changes walkthrough 📝

Relevant files
Tests
OptionsTest.java
Add Java test examples for ChromeOptions settings               

examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java

  • Added test methods for various ChromeOptions settings.
  • Implemented tests for browserName, browserVersion, and platformName.
  • Included timeout settings and unhandled prompt behavior tests.
  • Added tests for window rect and strict file interactability
    capabilities.
  • +89/-0   
    Documentation
    options.en.md
    Update Java examples in English documentation                       

    website_and_docs/content/documentation/webdriver/drivers/options.en.md

  • Updated Java code examples with line numbers.
  • Replaced placeholder badges with actual code references.
  • +10/-10 
    options.ja.md
    Update Java examples in Japanese documentation                     

    website_and_docs/content/documentation/webdriver/drivers/options.ja.md

  • Updated Java code examples with line numbers.
  • Replaced placeholder badges with actual code references.
  • +10/-10 
    options.pt-br.md
    Update Java examples in Portuguese documentation                 

    website_and_docs/content/documentation/webdriver/drivers/options.pt-br.md

  • Updated Java code examples with line numbers.
  • Replaced placeholder badges with actual code references.
  • +10/-10 
    options.zh-cn.md
    Update Java examples in Chinese documentation                       

    website_and_docs/content/documentation/webdriver/drivers/options.zh-cn.md

  • Updated Java code examples with line numbers.
  • Replaced placeholder badges with actual code references.
  • +10/-10 

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

    innazh and others added 7 commits August 17, 2024 22:32
    added java examples/tests for browserName, browserVersion,platformName,
    scriptTimeout, pageLoadTimeout, implicitWaitTimeout,
    unhandledPromptBehaviour, setWindowRect, strictFileInteractability
    added all mising examples for java for driver options page, except for the ones
    that require moving code
    based on the changes I just made to the example file
    Copy link

    netlify bot commented Aug 17, 2024

    Deploy Preview for selenium-dev ready!

    Name Link
    🔨 Latest commit bce6030
    🔍 Latest deploy log https://app.netlify.com/sites/selenium-dev/deploys/66c3107bc0ee0a000943c1a9
    😎 Deploy Preview https://deploy-preview-1865--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 Aug 17, 2024

    CLA assistant check
    All committers have signed the CLA.

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation tests Review effort [1-5]: 2 labels Aug 17, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Possible Bug
    The assertion in the setBrowserVersion test may not be reliable as it directly compares the set version with the retrieved version, which might not always match exactly.

    Code Smell
    The setPlatformName test uses toString() on a String object, which is unnecessary and may lead to confusion.

    Copy link
    Contributor

    qodo-merge-pro bot commented Aug 17, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Use try-with-resources for automatic resource management

    Use try-with-resources statement for WebDriver instances to ensure proper resource
    management and automatic closing of the driver.

    examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java [93-99]

    -WebDriver driver = new ChromeDriver(chromeOptions);
    -try {
    +try (WebDriver driver = new ChromeDriver(chromeOptions)) {
       Duration timeout = driver.manage().timeouts().getScriptTimeout();
    -  assert timeout.equals(duration);
    -} finally {
    -  driver.quit();
    +  assertEquals(duration, timeout, "Script timeout should match the set duration");
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Using try-with-resources ensures proper resource management and automatic closing of the driver, which is crucial for preventing resource leaks and is a best practice.

    9
    ✅ Use testing framework assertions instead of assert keyword for better error reporting
    Suggestion Impact:The commit replaced the use of the assert keyword with Assertions.assertFalse from JUnit, providing more informative error messages and better test reporting.

    code diff:

    -  	@Test
    -	public void getBrowserName() {
    -		ChromeOptions chromeOptions = new ChromeOptions();
    -		String name = chromeOptions.getBrowserName();
    -		assert !name.isEmpty();
    +  @Test
    +  public void getBrowserName() {
    +	ChromeOptions chromeOptions = new ChromeOptions();
    +	String name = chromeOptions.getBrowserName();
    +	Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");

    Consider using assertions from a testing framework like JUnit or TestNG instead of
    using the assert keyword directly. This provides more informative error messages and
    better test reporting.

    examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java [67]

    -assert !name.isEmpty();
    +assertFalse(name.isEmpty(), "Browser name should not be empty");
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using assertions from a testing framework like JUnit or TestNG provides more informative error messages and better test reporting, which is a best practice in writing test cases.

    8
    ✅ Use specific assertion methods for boolean comparisons
    Suggestion Impact:The commit replaced the generic boolean assertion with a more specific assertion method, Assertions.assertTrue, for the STRICT_FILE_INTERACTABILITY capability, as suggested.

    code diff:

    +  public void setStrictFileInteractability() {
    +    ChromeOptions chromeOptions = new ChromeOptions();
    +    chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
    +	//verify the capability object is not null
    +    Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
    +    Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
     
    -		WebDriver driver = new ChromeDriver(chromeOptions);
    -		try {
    -			Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
    -			assert timeout.equals(duration);
    -		} finally {
    -			driver.quit();
    -		}
    -	}
    -
    -	@Test
    -	public void setImplicitWaitTimeout() {
    -		ChromeOptions chromeOptions = new ChromeOptions();
    -		Duration duration = Duration.of(5, ChronoUnit.SECONDS);
    -		chromeOptions.setImplicitWaitTimeout(duration);
    -
    -		WebDriver driver = new ChromeDriver(chromeOptions);
    -		try {
    -			Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
    -			assert timeout.equals(duration);
    -		} finally {
    -			driver.quit();
    -		}
    -	}
    -
    -	@Test
    -	public void setUnhandledPromptBehaviour() {
    -		ChromeOptions chromeOptions = new ChromeOptions();
    -		chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
    -		assert chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR).equals(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
    -	}
    -
    -	@Test
    -  	public void setWindowRect() {
    -    	ChromeOptions chromeOptions = new ChromeOptions();
    -    	chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
    -    	assert chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT).equals(true);
    -  	}
    -	
    -	@Test
    -	public void setStrictFileInteractability() {
    -	    ChromeOptions chromeOptions = new ChromeOptions();
    -	    chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
    -	    assert chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY).equals(true);
    -	}
    +    Boolean capability = (Boolean) capabilityObject;
    +    Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");

    Consider using a more specific assertion method from a testing framework to compare
    boolean values, which provides more informative error messages.

    examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java [149-150]

     chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
    -assert chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY).equals(true);
    +assertTrue((Boolean) chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY), "Strict file interactability should be enabled");
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using specific assertion methods for boolean comparisons provides more informative error messages, enhancing the clarity and effectiveness of test cases.

    8
    Maintainability
    Use constants for string literals to improve maintainability

    Use a constant for the browser version string to improve maintainability and reduce
    the risk of typos.

    examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java [74-76]

    -String version = "latest";
    -chromeOptions.setBrowserVersion(version);
    -assert version.equals(chromeOptions.getBrowserVersion());
    +private static final String LATEST_VERSION = "latest";
    +chromeOptions.setBrowserVersion(LATEST_VERSION);
    +assertEquals(LATEST_VERSION, chromeOptions.getBrowserVersion(), "Browser version should be set to latest");
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using constants for string literals improves maintainability and reduces the risk of typos, which is a good practice for cleaner and more reliable code.

    7

    innazh and others added 5 commits August 17, 2024 18:54
    I was using toString() on a string variable by accident, removing it here.
    using assertions library, fixed spacing to match the rest of the code,
    added the missing imports. All of this threw off the line numbers for
    the options docs, so will have to edit it in the next commit.
    Since I had added imports, the line numbers fro the examples had to be updated.
    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, @innazh!

    @diemol diemol merged commit 8617cf3 into SeleniumHQ:trunk Aug 19, 2024
    12 checks passed
    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 Review effort [1-5]: 2 tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants