flathub / org.eclipse.Java

https://flathub.org/apps/details/org.eclipse.Java
4 stars 11 forks source link

How can i use Selenium Webdriver inside flatpak Eclipse? #30

Open wolfred24 opened 3 years ago

wolfred24 commented 3 years ago

Hi guys im trying to run chromedriver and geckodriver for firefox inside an Eclipse project, but flatpak Eclipse app cant open Firefox or Chromium because Eclipse does not have access to the binaries of the system, i have tried to give access to the binaries using flatseal but nothing works:

PATH=/app/bin:/app/openjdk-11/bin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:

This is the Eclipse error log: Scenario: Login in to account with correct details # src/test/java/FeatureFiles/Login.feature:47 org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'debian', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.0-11-amd64', java.version: '11.0.8' Driver info: driver.version: FirefoxDriver at org.openqa.selenium.firefox.FirefoxBinary.(FirefoxBinary.java:100) at java.base/java.util.Optional.orElseGet(Optional.java:369) at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:216) at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:187) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:147) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) at StepFiles.Login.setup(Login.java:29)

When i try to execute my code with a local Eclipse installation everything works fine.

Is there a way to give flatpak Eclipse access to the flatpak Firefox or chormium binaries?

akurtakov commented 3 years ago

Just to clarify the issue happens with Firefox/Chromium installed via flatpak ? Have you tried whether it works with system installed firefox via apt-get ? Eclipse has a special process shim to enable running applications available on the host PATH. Unfortunately we haven't look into integration between Eclipse flatpak and Chromium flatpak so any hints or links on the topic are more than welcome.

wolfred24 commented 3 years ago

Yes, i have Firefox flatpak version installed, also i have firefox Debian local installation, when i run my code using Flatpak Eclipse version it does not run at all (because the flatpak Eclipse version cannot execute the firefox binnaries), but when i run my code with loocal debian Eclipse installation it works perfectly fine (because my local installation of Eclipse can execute the firefox binnaries to run the selenium automation test).. this is the minimal code example that i am tryng to run:

System.setProperty("webdriver.gecko.driver", "/home/cesar/workspace/CucumberFramework/src/test/java/Resources/geckodriver"); this.driver = new FirefoxDriver(); this.driver.manage().window().maximize(); this.driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); this.driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);

There is no integration betwen flatpak Eclipse and flatpak Firefox.

ghost commented 2 years ago

So there's still no fix?

mbooth101 commented 2 years ago

But it's the geckodriver that is trying to invoke firefox, and not Eclipse, right? And the geckodriver is not sandbox-aware.

You can probably trick it into launching the firefox process on the host system, outside of the sandbox, like this:

First create a script called "ff-host" with the following content:

#!/bin/bash
flatpak-spawn --directory=. --host firefox "$@"

And make it executable:

$ chmod +x ....../path/to/ff-host

Then tell the geckodriver to use this script as the firefox binary:

System.setProperty("webdriver.gecko.driver", "....../path/to/geckodriver");
System.setProperty("webdriver.firefox.bin", "......./path/to/ff-host");

WebDriver driver = new FirefoxDriver();
driver.get("https://selenium.dev");
driver.quit();

This trivial test seems to work for me, but full disclaimer: I have never used Selenium, or webdrivers, before!

IMHO geckodriver should be enhanced to be sandbox-aware, since Eclipse cannot control what it is doing. Consider filing a ticket against geckodriver.

ghost commented 2 years ago

@mbooth101 Not working for me. I just switched to the Oomph installer, fixed my issue.

mbooth101 commented 2 years ago

@mbooth101 Not working for me.

Which part?

ghost commented 2 years ago

@mbooth101 Not working for me.

Which part?

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:230)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:151)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:198)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:164)
    at MercuryTours.main(MercuryTours.java:14)
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.1.1', revision: 'e8fcc2cecf'
System info: host: 'fedora', ip: '192.168.0.103', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.13-200.fc35.x86_64', java.version: '16.0.2'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:226)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:98)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    ... 6 more
mbooth101 commented 2 years ago

@Ashvith Can you provide a sample project to illustrate?

ghost commented 2 years ago

@mbooth101 I've made another sample program that has the same issue

Code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "/home/ashvith/eclipse-workspace/Selenium/external_module/geckodriver");
        System.setProperty("webdriver.firefox.bin", "/home/ashvith/eclipse-workspace/Selenium/external_module/ffhost");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://selenium.dev");
        driver.quit();
    }
}

Log

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:230)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:151)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:198)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:164)
    at Sample.main(Sample.java:8)
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.1.1', revision: 'e8fcc2cecf'
System info: host: 'fedora', ip: '192.168.0.103', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.13-200.fc35.x86_64', java.version: '16.0.2'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:226)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:98)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    ... 6 more
ssokolow commented 2 years ago

I don't know about Eclipse, but I was having trouble even getting chromedriver to talk to a Flatpak release of Ungoogled Chromium when only the browser was sandboxed, so I decided that, since you connect to it over a TCP socket anyway, why not just run it inside the sandbox?

# Put your chromedriver binary somewhere it won't collide
mv $HOME/bin/chromedriver $HOME/bin/chromedriver.real

# Expose your chromedriver binary to the sandbox:
flatpak override --user com.github.Eloston.UngoogledChromium --filesystem=$HOME/bin/chromedriver.real:ro

# Write a wrapper script in your `PATH` to launch it INSIDE the sandbox
cat <<EOF > $HOME/bin/chromedriver                  
#!/bin/sh
exec flatpak run --command=$HOME/bin/chromedriver.real com.github.Eloston.UngoogledChromium "\$@"
EOF
chmod +x $HOME/bin/chromedriver

Tested working with a simple npm test from a hybrid Rust/TypeScript project, though, at first, it was being temperamental and I had to bypass the auto-start. I'm not sure what I changed to fix that.

chromedriver &
SELENIUM_BROWSER=chrome SELENIUM_REMOTE_URL="http://localhost:9515/" npm test

I also got the same success with geckodriver, though they warn you that they pass custom profiles via temporary directory, so you'll need to follow their instructions to produce something like this instead:

flatpak override --user org.mozilla.firefox --filesystem=~/.cargo/bin/geckodriver:ro
flatpak override --user org.mozilla.firefox --filesystem=~/tmp/geckodriver:create

cat <<EOF >| $HOME/bin/geckodriver                    
#!/bin/sh
exec flatpak run --env=TMPDIR=$HOME/tmp/geckodriver --command=$HOME/.cargo/bin/geckodriver org.mozilla.firefox "\$@"
EOF
chmod +x $HOME/bin/geckodriver

Make sure you rehash if you're running Zsh and are just relying on your PATH order to have the wrapper take precedence or things could get frustrating.

Before I got that working, I also found two other options:

First, you could take advantage of Mozilla's convenient self-contained tarballs to install a special copy of Firefox just for webdriver:

mkdir -p ~/opt
cd ~/opt
wget 'https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US' -Ofirefox.tar.bz2
tar xvaf firefox.tar.bz2

cat <<EOF >| $HOME/bin/geckodriver                    
#!/bin/sh
exec ~/.cargo/bin/geckodriver --binary $HOME/opt/firefox/firefox "\$@"
EOF
chmod +x $HOME/bin/geckodriver

Second, though I couldn't get them to work for unrelated reasons, SeleniumHQ does offer Docker containers for Firefox, Chrome, and Edge with the relevant drivers set up to auto-start.

In theory, any of those should work with a Flatpak'd IDE as long as both Flatpaks have network access, read-write access to the TMPDIR and the variable set in both Flatpak's (only needed for geckodriver), and you either manually start your drivers and use the SELENIUM_* variables for Eclipse's Flatpak or adapt my technique to inject chromedriver and geckodriver wrappers into Eclipse's PATH which hand off to the Chrome/Chromium and Firefox flatpaks. (eg. using flatpak-spawn --host to invoke what I do in the wrappers I wrote.)