Selenium is an automated web testing framework. Using this we can automate the browser functioning for testing any web application. Using selenium you can run predefined code to navigate between multiple pages and test application with predefined rules. This tutorial will help you to setup Selenium with Firefox on Ubuntu, Debian and LinuxMint systems.
Read This: Setup Selenium with ChromeDriver on Ubuntu
Step 1 – Prerequisites
Execute the following commands to install required packages on your system. Here Xvfb (X virtual framebuffer) is an in-memory display server for a UNIX-like operating system (e.g., Linux). It implements the X11 display server protocol without any display. This is helpful for CLI applications like CI service.
sudo apt-get update sudo apt-get install -y unzip xvfb libxi6 libgconf-2-4
Also, install Java on your system. Use the below command to install the latest available java version.
sudo apt-get install default-jdk
Step 2 – Install Firefox with Driver
Firefox is available under default apt repositories. You can simply install it by running the following command from the command prompt.
sudo apt-get -y install firefox
Also, download the geckodriver for the firefox.
wget https://github.com/mozilla/geckodriver/releases/download/v0.25.0/geckodriver-v0.25.0-linux64.tar.gz tar xzf geckodriver-v0.25.0-linux64.tar.gz sudo mv geckodriver /usr/bin/geckodriver
Step 3 – Download Selenium Server Jar
The Selenium Server is required to run Remote Selenium WebDrivers. You need to download the Selenium standalone server jar file using the below commands or visit here to find the latest version of Jar file.
mkdir ~/selenium && cd ~/selenium wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
Also download the testng-6.5.1.jar file to your system.
wget http://www.java2s.com/Code/JarDownload/testng/testng-6.5.1.jar.zip unzip testng-6.5.1.jar.zip
Step 4 – Start Selenium Server
Your server setup is ready. Start the standalone selenium server using Xvfb utility.
Run Selenium Server
DISPLAY=:1 xvfb-run java -jar ~/selenium/selenium-server-standalone-3.13.0.jar
Your Selenium server is now running with firefox. Use this server to run your test cases written in Selenium using the Firefox web browser.
Step 5 – Sample Java Program (Optional)
This is an optional step. It describes running a single test case using Selenium standalone server and FirefoxDriver. This Java program will open a specified website URL and check if defined string presents on the webpage or not.
Create a Java program by editing a file in a text editor.
vim TecAdminSeleniumTest.java
Add the below content to file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.testng.annotations.Test; public class TecAdminSeleniumTest { public static void main(String[] args) throws IOException, InterruptedException { FirefoxBinary firefoxBinary = new FirefoxBinary(); firefoxBinary.addCommandLineOptions("--headless"); firefoxBinary.addCommandLineOptions("--no-sandbox"); System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver"); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setBinary(firefoxBinary); FirefoxDriver driver = new FirefoxDriver(firefoxOptions); driver.get("https://google.com"); Thread.sleep(1000); if (driver.getPageSource().contains("kkkI'm Feeling Lucky")) { System.out.println("Pass"); } else { System.out.println("Fail"); } driver.quit(); } } |
You can change the URL “https://google.com” with any other URL of your choice, Then also change the search string like “I’m Feeling Lucky” used in the above Java program. Save your java program and execute it. First, you need to set the Java CLASSPATH environment variable including the selenium-server-standalone-3.141.59.jar and testng-6.5.1.jar. Then compile the java program and run it.
export CLASSPATH=".:selenium-server-standalone-3.141.59.jar:testng-6.5.1.jar"
Now, compile your Java program and run it.
javac TecAdminSeleniumTest.java java TecAdminSeleniumTest
If the defined search string found, You will get message “Pass” and if string not found on the webpage, you will get the “Fail” message on the screen.
6 Comments
java TecAdminSeleniumTest
Error: Could not find or load main class TecAdminSeleniumTest
Caused by: java.lang.ClassNotFoundException: TecAdminSeleniumTest
How to solve the expection
At the end you write: “Start the Chrome via standalone selenium server using Xvfb utility.”
isn’t it for firefox?!
Please provide the complete content as you provided for the chrome browser on the below URL:
https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/
Need to provide the complete program example for the firefox also
Thanks for your great article
I’m getting this error
xvfb-run: error: Xvfb failed to start
while running following command
DISPLAY=:1 xvfb-run java -jar ~/selenium/selenium-server-standalone-3.13.0.jar
Maybe it’s because you need to find the directory where selenium-server-standalone-3.13.0.jar downloaded. In my directory I found that file in /home/selenium-server-standalone-3.13.0.jar.