Skip to content
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

Modified these files to ensure that proper error is displayed while r… #4271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ritankarsaha
Copy link

FIXES #3105
Running selenium server locally without installing java first in the system

###Usage Examples

// example-usage.js
const SeleniumServiceBuilder = require('./services/selenium-service-builder.js');

const settings = {
  selenium: {
    port: 4444,
    loopback: '127.0.0.1',
    env: {}, // Add any environment variables if needed
    stdio: 'inherit', // Adjust as needed
    cli_args: {
      // Example JVM arguments
      // 'webdriver.chrome.driver': '/path/to/chromedriver',
      // Add other CLI arguments as needed
    },
    jvmArgs: [
      // Example JVM arguments
      '-Xmx2g',
      '-Xms1g',
    ],
  },
  webdriver: {
    server_path: '/path/to/selenium-server-4.x.x.jar', // Update this path
    command: 'standalone', // or any other command as per Selenium 4
  },
};

(async () => {
  const seleniumService = new SeleniumServiceBuilder(settings);
  seleniumService.setCliArgs();
  try {
    await seleniumService.createService({
      showSpinner: (message) => {
        // Implement spinner display if desired
        console.log(message);
      },
    });
    // Selenium Server is now running
  } catch (error) {
    console.error('Failed to start Selenium Server:', error);
    process.exit(1);
  }
})();

Changes Made

1. Import Required Modules

The following modules were imported to enhance the functionality of the service builder:

  • which: Used to locate the Java executable in the system's PATH.
  • fs.promises: Provides asynchronous file system operations.
  • Logger: Assumed to be exported from a utility module for logging purposes.

2. Modified SeleniumServer4 Constructor

  • Parameter Addition: The constructor now accepts javaPath as the first parameter, decoupling Java detection from the constructor logic.
  • Usage of javaPath: The detected Java executable path is passed to the parent DriverService class.

3. Added findJavaExecutable Method

  • Purpose: This method asynchronously checks for the presence of Java by first looking at the JAVA_HOME environment variable and then falling back to searching the system's PATH.
  • Error Handling: Descriptive errors are thrown if Java is not found or is not executable.

4. Added checkExecutable Method

  • Purpose: Verifies that the Java executable exists and is executable.
  • Usage: This method is called within findJavaExecutable to ensure the Java binary is usable.

5. Modified createService Method

  • Java Detection: The method now calls findJavaExecutable to retrieve the Java executable path before proceeding.
  • Error Handling: It catches and logs errors related to Java detection or Selenium Server startup.
  • Service Initialization: The detected javaPath is passed to the SeleniumServer4 constructor.

6. Caching Java Path

  • Variable: Introduced _javaPath to cache the detected Java path, avoiding redundant checks in subsequent service starts.

###################################################

Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

  • [x ] Before marking your PR for review, please test and verify your changes by making appropriate modifications to any of the Nightwatch example tests (present in examples/tests directory of the project) and running them. ecosia.js and duckDuckGo.js are good examples to work with.
  • [x ] Create a new branch from master (e.g. features/my-new-feature or issue/123-my-bugfix);
  • [x ] If you're fixing a bug also create an issue if one doesn't exist yet;
  • [ x] If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
  • [ x] Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
  • [x ] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
  • [x ] Do not include changes that are not related to the issue at hand;
  • [x ] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
  • [x ] Always add unit tests - PRs without tests are most of the times ignored.

…unning selenium server locally without installing java first in the system
@CLAassistant
Copy link

CLAassistant commented Oct 9, 2024

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No error while running selenium-server locally without installing Java first.
2 participants