Skip to content

Selenium java project in IDEA #17765

Description

@asolntsev

Bazel plugin in IDEA doesn't work for me.
And even when it worked, it didn't allow many Java features, e.g. profiling.

I created a much simpler setup.
Just wanted to share it with other Selenium maintainers.

  1. Created script scripts/prepare_idea_java_project.sh that copies all Java dependencies to a folder:
  2. Run this script
  3. Open the project in IDEA as a simple Java project
  4. Bingo! All Java features work: running, debugging, profiling; running multiple selected tests etc.

Script scripts/prepare_idea_java_project.sh

(written with the help of ChatGPT, of cause)

#!/usr/bin/env bash
# Prepare an IntelliJ IDEA java project by copying Bazel-built dependency jars
# into ./java-libs so the IDE can resolve them.
#
# Prerequisites:
#   - Run anywhere inside the Selenium repo (script cd's to the repo root).

set -euo pipefail
shopt -s nullglob

cd "$(git rev-parse --show-toplevel)"

bazel build //java/... --pin_browsers=true

LIB="$PWD/java-libs"
rm -rf "$LIB"
mkdir -p "$LIB"

EXECROOT=$(bazel info execution_root)

# To exclude sources:
#       and not f.path.endswith("-sources.jar")
# JUnit jars are excluded because IntelliJ bundles its own copy.
bazel cquery \
  'deps(kind("java_test", //java/...))' \
  --output=starlark \
  --starlark:expr='"\n".join([
    f.path
    for f in target.files.to_list()
    if f.path.endswith(".jar")
       and (f.path.startswith("external/") or "/genfiles/" in f.path or ("/devtools/v" in f.path and f.path.endswith("-project.jar")))
       and (not "junit-jupiter-engine" in f.path)
       and (not "junit-platform-engine" in f.path)
       and (not "junit-platform-launcher" in f.path)
       and (not "junit-platform-reporting" in f.path)
  ])' \
| sort -u \
| while IFS= read -r rel; do
    [[ -z "$rel" ]] && continue
    abs="$EXECROOT/$rel"
    dest="$LIB/$(basename "$abs")"
    if [[ -e "$dest" ]]; then
      echo "warn: overwriting $dest (basename collision)" >&2
    fi
    cp "$abs" "$dest"
    chmod u+w "$dest"
  done

# Bazel's own runfiles library used by Selenium code
runfiles=(.ijwb/.blaze/libraries/librunfiles-hjar*.jar)
if (( ${#runfiles[@]} == 0 )); then
  echo "warn: no librunfiles-hjar*.jar found — sync the IntelliJ Bazel plugin first" >&2
else
  cp "${runfiles[@]}" "$LIB/"
fi

echo "✔ Dependency jars only: $(ls -1 "$LIB" | wc -l)"

IDEA project setup

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions