Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»How to Setup Selenium with Firefox on Ubuntu

    How to Setup Selenium with Firefox on Ubuntu

    RahulBy RahulJuly 28, 20183 Mins ReadUpdated:September 21, 2019

    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.

    Automation selenium testing
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install ownCloud 10 on Debian 9 (Stretch)
    Next Article How to Install Nextcloud on Debian 10/9/8

    Related Posts

    What is CPU? – Definition, Types and Parts

    3 Mins Read

    How to Install Ionic Framework on Ubuntu 22.04

    3 Mins Read

    What is the /etc/hosts file in Linux

    Updated:June 27, 20222 Mins Read

    Creating DMARC Record for Your Domain

    Updated:June 29, 20223 Mins Read

    What is Computer Hardware?

    4 Mins Read

    What is Information Technology (IT)?

    Updated:June 29, 20223 Mins Read

    6 Comments

    1. Kamalakkannan on June 27, 2022 10:02 am

      java TecAdminSeleniumTest
      Error: Could not find or load main class TecAdminSeleniumTest
      Caused by: java.lang.ClassNotFoundException: TecAdminSeleniumTest

      Reply
      • Kamalakkannan on June 27, 2022 10:03 am

        How to solve the expection

        Reply
    2. sarah on July 16, 2019 5:04 am

      At the end you write: “Start the Chrome via standalone selenium server using Xvfb utility.”
      isn’t it for firefox?!

      Reply
    3. Saurabh saini on April 23, 2019 12:11 pm

      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

      Reply
    4. Muhammad hafeez on August 31, 2018 5:49 am

      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

      Reply
      • WB on September 24, 2018 4:55 pm

        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.

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.