selenium – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 17 Dec 2022 10:54:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Setup Selenium with Python and Chrome Driver on Ubuntu & Debian https://tecadmin.net/setup-selenium-with-python-on-ubuntu-debian/ https://tecadmin.net/setup-selenium-with-python-on-ubuntu-debian/#comments Sun, 19 Jun 2022 04:12:49 +0000 https://tecadmin.net/?p=30109 Selenium is a versatile tool that can be used for automating browser-based tests. It has a wide range of features that make it an ideal choice for automating tests. Selenium can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby. [...]

The post Setup Selenium with Python and Chrome Driver on Ubuntu & Debian appeared first on TecAdmin.

]]>
Selenium is a versatile tool that can be used for automating browser-based tests. It has a wide range of features that make it an ideal choice for automating tests. Selenium can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby.

This makes it possible to write tests in the language that you are most comfortable with. In addition, Selenium has a large user community that provides support and help when needed.

In this blog post, you will learn to set up a Selenium environment on an Ubuntu system. Also provides you with a few examples of Selenium scripts written in Python.

Prerequisites

You must have Sudo privileged account access to the Ubuntu system.

One of the examples also required a desktop environment to be installed.

Step 1: Installing Google Chrome

Use the below steps to install the latest Google Chrome browser on Ubuntu and Debian systems.

  1. First of all, download the latest Gooogle Chrome Debian package on your system.
    wget -nc https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 
    
  2. Now, execute the following commands to install Google Chrome from the locally downloaded file.
    sudo apt update 
    sudo apt install -f ./google-chrome-stable_current_amd64.deb 
    

    Press ‘y’ for all the confirmations asked by the installer.

This will complete the Google Chrome on your Ubuntu or Debian system. This will also create an Apt PPA file for further upgrades.

Step 2: Installing Selenium and Webdriver for Python

We will use a virtual environment for running Python scripts. Follow the below steps to create Python virtual environment and install the required python modules.

  1. Create a directory to store Python scripts. Then switch to the newly-created directory.
    mkdir tests && cd tests 
    
  2. Set up the Python virtual environment and activate it.
    python3 -m venv venv 
    source venv/bin/activate 
    

    Once the environment is activated, You will find the updated prompt as shown below screenshot:

    Create Python Environment for Selenium on Ubuntu
    Create Python Environment for Selenium on Ubuntu
  3. Now use PIP to install the selenium and webdriver-manager Python modules under the virtual environment.
    pip install selenium webdriver-manager 
    

    Installing Selenium and Webdriver Python Module on Ubuntu & Debian
    Installing Selenium and Webdriver Python Module on Ubuntu & Debian

Example 1: Selenium Python Script with Headless Chrome

Your system is ready to run Selenium scripts written in Python. Now, create a sample selenium script in Python that fetches the title of a website.

This script will run headless, So you can run it without an X desktop environment. You can simply SSH to your system and run the below example:

  1. Create a Python script and edit it in your favorite text editor:
    nano test.py 
    
  2. Copy-paste the following Selenium Python script to the file.
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    driver.get("https://python.org")
    print(driver.title)
    driver.close()

    Press CTRL + O to save content to the file and press CTRL + X to close the editor.

  3. Now, run this Python script in a shell.
    python test.py 
    

    You will see the output something like the below:

    Running Selenium Python Script in Ubuntu & Debian
    Running the Selenium Python Script

Example 2: Selenium Python Script with Chrome GUI

In order to run this example, the Ubuntu system must have installed a Desktop environment. If the desktop is not installed, use another tutorial to install the Desktop environment on Ubuntu systems.

Now, log in to the desktop interface and try to run the below example.

  1. Open a command prompt, then create a new Python script and edit it in your favorite text editor.
    nano test.py 
    
  2. Copy-paste the below snippet in the file:

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from webdriver_manager.chrome import ChromeDriverManager
    
    options = Options()
    # options.add_argument('--headless')
    # options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    driver.get('http://www.google.com')
    search = driver.find_element(by=By.NAME, value="q")
    search.send_keys("Hey, Tecadmin")
    search.send_keys(Keys.RETURN)
    
    time.sleep(5)
    driver.close()

    Write the changes to file with CTRL + O and close this with keyboard shortcut CTRL + X

  3. This is a Selenium script written in Python, that will launch the Google Chrome web browser and search for a defined string. then close the browser.
  4. Run the Python script in the terminal:
    python test2.py 
    

    You will see that a Browser window will open and perform the defined tasks in the script. See the below screencast of the run:

Conclusion

In this tutorial, you have learned about the configuration of Selenium for Python on Ubuntu and Debian Linux systems. Also provides you with two Selenium examples. Hope this tutorial helps you to understand to run Selenium with Python.

The post Setup Selenium with Python and Chrome Driver on Ubuntu & Debian appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-selenium-with-python-on-ubuntu-debian/feed/ 1 selenium Archives – TecAdmin nonadult
Setup Selenium with Python and Chrome on Fedora https://tecadmin.net/configuring-the-selenium-with-python-on-fedora/ https://tecadmin.net/configuring-the-selenium-with-python-on-fedora/#respond Sat, 18 Jun 2022 04:40:38 +0000 https://tecadmin.net/?p=30102 Selenium is a versatile tool, which is widely used for automating browser-based tests. It can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby. This makes it possible to write tests in the language that you are most comfortable with. [...]

The post Setup Selenium with Python and Chrome on Fedora appeared first on TecAdmin.

]]>
Selenium is a versatile tool, which is widely used for automating browser-based tests. It can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby.

This makes it possible to write tests in the language that you are most comfortable with. In addition, Selenium has a large user community that provides support and help when needed.

This tutorial will help you to configure the environment for Selenium with Python and Chrome on Fedora. We will discuss an example written in Python.

Prerequisites

Assuming you have access to a Fedora system with a Sudo privileged account.

This tutorial can be run with GUI access or shell access only.

Step 1 – Installing Google Chrome

You can use firefox or Google Chrome web browser to run your selenium test cases. In this article, we will discuss examples with the Google Chrome web browser.

So, Let’s install Google chrome first. Enable the google-chome repository with the below-mentioned commands:

sudo dnf install fedora-workstation-repositories 
sudo dnf config-manager --set-enabled google-chrome 

Now, install the latest google chrome stable web browser:

sudo dnf install google-chrome-stable 

Google Chrome will be installed on your Fedora system.

Step 2 – Setup Python Environment

We will create a virtual environment for running our Python test cases. Follow the below steps to create Python virtual environment, and install the required modules.

  1. Installing Python and its virutal environent module.
    sudo dnf install python3 python3-virtualenv 
    
  2. Create a directory for contianing python environment and scripts..
    mkdir tests && cd tests 
    
  3. Create virutal environment
    python3 -m venv venv 
    source venv/bin/activate 
    
    Creating Python Virtual Environment for Selenium on Fedora
    Creating Python Virtual Environment for Selenium on Fedora
  4. Installing selenium and webdriver manager using PIP under the virtual environemnt.
    pip install selenium webdriver-manager 
    

    Installing selenium for Python on Fedora
    Installing selenium for Python on Fedora

Step 3 – Running an Example with Selenium Python

The Python virtual environment is ready to run Selenium scripts. Let’s run an example script, that opens a website in a headless (Useful for remote ssh access) google chrome browser and prints the title of the website.

Make sure the Python virtual environment is active. you can identify that using the terminal prompt. Now create a Python script and edit it in a text editor.

nano test1.py 

Copy-paste the below snippet to file:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.get("https://python.org")
print(driver.title)
driver.close()

Press CTRL +O to write changes and then press CTRL + X to exit from the editor.

Now, run your Python script:

python test1.py 

You will see the output something like below:

Running Selenium Python Script on Fedora
Running Selenium Python Script on Fedora

At the first run, the script will download the latest chromedriver and place it in your system to use for the next executions.

In the output, you can see the title of the given website is printed on the screen.

Conclusion

Selenium is a popular tool among website testers for running automatic test cases. In this tutorial, we have discussed configuring the Selenium environment with Python scripts.

The post Setup Selenium with Python and Chrome on Fedora appeared first on TecAdmin.

]]>
https://tecadmin.net/configuring-the-selenium-with-python-on-fedora/feed/ 0
How to Setup Selenium with Chrome Driver on Fedora https://tecadmin.net/setup-selenium-with-chrome-driver-on-fedora/ https://tecadmin.net/setup-selenium-with-chrome-driver-on-fedora/#comments Mon, 17 Feb 2020 10:26:18 +0000 https://tecadmin.net/?p=20173 This tutorial will help you to set up Selenium with ChromeDriver on Fedora systems. This tutorial also includes an example of a Java program that uses the Selenium standalone server and ChromeDriver and runs a sample test case. This tutorial described how to set up a selenium server with a chrome driver on a Fedora [...]

The post How to Setup Selenium with Chrome Driver on Fedora appeared first on TecAdmin.

]]>
This tutorial will help you to set up Selenium with ChromeDriver on Fedora systems. This tutorial also includes an example of a Java program that uses the Selenium standalone server and ChromeDriver and runs a sample test case.

This tutorial described how to set up a selenium server with a chrome driver on a Fedora system. Also, you will get a sample Java program to run a small test over selenium with a headless chrome driver.

Prerequisites

Login to your Fedora system with Sudo privileged account. Launch a terminal and execute the following commands to install the required packages on your system.

sudo dnf install unzip wget java-11-openjdk java-11-openjdk-devel 

Step 1 – Installing Google Chrome

Enable the Google chrome repository for installing latest versions. Execute the following commands, this will enable google-chrome repo on your Fedora system:

sudo dnf install fedora-workstation-repositories 
sudo dnf config-manager --set-enabled google-chrome 

Next install the Google chrome web browser:

sudo dnf install google-chrome-stable  

Step 2 – Install ChromeDriver

You are also required to set up ChromeDriver on your system. ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium. The WebDriver is an open-source tool for automated testing of web apps across multiple browsers.

wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip 
unzip chromedriver_linux64.zip 

You can find the latest ChromeDriver on its official download page. Now execute below commands to configure ChromeDriver on your system.

sudo mv chromedriver /usr/bin/chromedriver 
sudo chown root:root /usr/bin/chromedriver 
sudo chmod +x /usr/bin/chromedriver 

Step 3 – Download Required Jar Files

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.

wget https://selenium-release.storage.googleapis.com/3.13/selenium-server-standalone-3.13.0.jar 

Also, download the TestNG jar file on your system.

wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip 
unzip testng-6.8.7.jar.zip 

Your Selenium server is now running with Chrome. Use this server to run your test cases written in Selenium using the Google Chrome web browser. The next step is an optional step and doesn’t depend on Step 5.

Step 4 – Testing with Sample Java Application

This is an optional step. It describes running a single test case using the Selenium standalone server and ChromeDriver. Let’s create a Java program using the Selenium server and Chrome Driver. This Java program will open a specified website URL and check if the defined string is present on the webpage or not.

Create a Java program by editing a file in text editor.

vim TecAdminSeleniumTest.java 

Add the below content to the file.

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TecAdminSeleniumTest {

        public static void main(String[] args) throws IOException, InterruptedException {
                System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.addArguments("--headless");
                chromeOptions.addArguments("--no-sandbox");

                WebDriver driver = new ChromeDriver(chromeOptions);

                driver.get("https://google.com");

                Thread.sleep(1000);

                if (driver.getPageSource().contains("I'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.jar and testng-6.8.7.jar. Then compile the java program and run it.

export CLASSPATH=".:selenium-server-standalone.jar:testng-6.8.7.jar" 
javac TecAdminSeleniumTest.java 

Then run command:

java TecAdminSeleniumTest 
Output
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 10968 Only local connections are allowed. Feb 01, 2020 10:51:40 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS Pass

You will see the results below. If the defined search string is found, You will get the message “Pass” and if the string is not found on the webpage, you will get the “Fail” message on the screen.

Selenium test case results

Conclusion

You have successfully configured Selenium with ChromeDrive on your Fedora system. Now you can automate your test cases and run them periodically. I hope this tutorial contributes a little help to you with the automation testing. Please do not forget to share this tutorial.

The post How to Setup Selenium with Chrome Driver on Fedora appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-selenium-with-chrome-driver-on-fedora/feed/ 3
How to Setup Selenium with ChromeDriver on Debian Linux https://tecadmin.net/setup-selenium-with-chromedriver-on-debian/ https://tecadmin.net/setup-selenium-with-chromedriver-on-debian/#comments Sun, 12 Aug 2018 14:09:17 +0000 https://tecadmin.net/?p=16698 This tutorial will help you to set up Selenium with ChromeDriver on Debian 9 and Debian 8. This tutorial also includes an example of a Java program that uses Selenium standalone server and ChromeDriver and runs a sample test case. Xvfb (X virtual framebuffer) is an in-memory display server for a UNIX-like operating system (e.g., [...]

The post How to Setup Selenium with ChromeDriver on Debian Linux appeared first on TecAdmin.

]]>
This tutorial will help you to set up Selenium with ChromeDriver on Debian 9 and Debian 8. This tutorial also includes an example of a Java program that uses Selenium standalone server and ChromeDriver and runs a sample test case.

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 services.

Step 1 – Prerequisites

Login to your Debian system as sudo privileged user and execute the following commands to install the required packages on your system.

sudo apt-get update
sudo apt-get install -y curl unzip xvfb libxi6 libgconf-2-4

Also, install Java on your system. Use below command to install OpenJDK on your system, If you like install Oracle Java 8 on your Debian system.

sudo apt-get install default-jdk 

Step 2 – Install Google Chrome

Now install Latest Google chrome on your Debian system using commands below. Google chrome headless feature opens multipe doors for the automation.

sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
sudo echo "deb [arch=amd64]  http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable

Step 3 – Install ChromeDriver

ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium. The WebDriver is an open source tool for automated testing of web apps across multiple browsers.

wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

You can find the latest ChromeDriver on its official download page. Now execute below commands to configure ChromeDriver on your system.

sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver

Step 4 – Download Required Jar Files

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.

wget https://selenium-release.storage.googleapis.com/3.13/selenium-server-standalone-3.13.0.jar

Also, download the testng-6.8.7.jar file to your system.

wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip
unzip testng-6.8.7.jar.zip

Step 5 – Start Chrome via Selenium Server

Your server setup is ready. Start the Chrome via standalone selenium server using Xvfb utility.

Run Chrome via Selenium Server

xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar

Use -debug option at end of command to start server in debug mode.

You can also Start Headless ChromeDriver by typing the below command on terminal.

chromedriver --url-base=/wd/hub

Your Selenium server is now running with Chrome. Use this server to run your test cases written in Selenium using the Google Chrome web browser. The next step is an optional step and doesn’t depend on Step 5.

Step 6 – Sample Java Program (Optional)

This is an optional step. It describes running a single test case using Selenium standalone server and ChromeDriver. Let’s create a Java program using the Selenium server and Chrome Driver. This Java program will open a specified website URL and check if the defined string is present on the webpage or not.

Create a Java program by editing a file in text editor.

vim TecAdminSeleniumTest.java

Add the below content to the file.

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TecAdminSeleniumTest {

        public static void main(String[] args) throws IOException, InterruptedException {
                System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.addArguments("--headless");
                chromeOptions.addArguments("--no-sandbox");

                WebDriver driver = new ChromeDriver(chromeOptions);
                driver.get("https://google.com");
                Thread.sleep(1000);

                if (driver.getPageSource().contains("I'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.jar and testng-6.8.7.jar. Then compile the java program and run it.

export CLASSPATH=".:selenium-server-standalone.jar:testng-6.8.7.jar"
javac TecAdminSeleniumTest.java
java TecAdminSeleniumTest

You will see the results below. If the defined search string is found, You will get the message “Pass” and if the string is not found on the webpage, you will get the “Fail” message on the screen.

Selenium test case results

The post How to Setup Selenium with ChromeDriver on Debian Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-selenium-with-chromedriver-on-debian/feed/ 2
How to Setup Selenium with Firefox on Ubuntu https://tecadmin.net/setup-selenium-with-firefox-on-ubuntu/ https://tecadmin.net/setup-selenium-with-firefox-on-ubuntu/#comments Sat, 28 Jul 2018 17:05:13 +0000 https://tecadmin.net/?p=16687 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 [...]

The post How to Setup Selenium with Firefox on Ubuntu appeared first on TecAdmin.

]]>
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.

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.

The post How to Setup Selenium with Firefox on Ubuntu appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-selenium-with-firefox-on-ubuntu/feed/ 6
How to Setup Selenium with ChromeDriver on Ubuntu 22.04, 20.04 & 18.04 https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/ https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/#comments Mon, 19 Feb 2018 16:17:20 +0000 https://tecadmin.net/?p=15095 This tutorial will help you to set up Selenium with ChromeDriver on Ubuntu, and LinuxMint systems. This tutorial also includes an example of a Java program that uses a Selenium standalone server and ChromeDriver and runs a sample test case. Read This: Setup Selenium with Firefox on Ubuntu Step 1 – Prerequisites Execute the following [...]

The post How to Setup Selenium with ChromeDriver on Ubuntu 22.04, 20.04 & 18.04 appeared first on TecAdmin.

]]>
This tutorial will help you to set up Selenium with ChromeDriver on Ubuntu, and LinuxMint systems. This tutorial also includes an example of a Java program that uses a Selenium standalone server and ChromeDriver and runs a sample test case.

Read This: Setup Selenium with Firefox on Ubuntu

Step 1 – Prerequisites

Execute the following commands to install the 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 services.

sudo apt update 
sudo apt install -y unzip xvfb libxi6 libgconf-2-4 

Also, install Java on your system. Let’s install Oracle Java 8 on your system or use the below command to install OpenJDK.

sudo apt install default-jdk 

Step 2 – Install Google Chrome

Now install Latest Google chrome package on your system using the below list commands. Google chrome headless feature opens multipe doors for the automation.

sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add 
sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list" 
sudo apt -y update 
sudo apt -y install google-chrome-stable 

Step 3 – Installing ChromeDriver

You are also required to set up ChromeDriver on your system. ChromeDriver is a standalone server that implements WebDriver’s wire protocol for Chromium. The WebDriver is an open-source tool for the automated testing of web apps across multiple browsers.

Find out the Google chrome version installed on your system.

google-chrome --version 
Output
Google Chrome 94.0.4606.71

Next, visit the Chromedriver download page and download the matching version of chromedriver on your system.

In my case, Google Chrome 94 is running on my system. So download the following file. You must make sure to download the correct version of a file:

wget https://chromedriver.storage.googleapis.com/94.0.4606.61/chromedriver_linux64.zip 
unzip chromedriver_linux64.zip 

You can find the latest ChromeDriver on its official download page. Now execute below commands to configure ChromeDriver on your system.

sudo mv chromedriver /usr/bin/chromedriver 
sudo chown root:root /usr/bin/chromedriver 
sudo chmod +x /usr/bin/chromedriver 

Step 4 – Download Required Jar Files

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.

wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar 
mv selenium-server-standalone-3.141.59.jar selenium-server-standalone.jar 

Also, download the testng-6.8.7.jar file to your system.

wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip 
unzip testng-6.8.7.jar.zip 

Step 5 – Start Chrome via Selenium Server

Your server setup is ready. Start Chrome via a standalone selenium server using the Xvfb utility.

Run Chrome via Selenium Server

xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar 

Use -debug option at end of the command to start the server in debug mode.

You can also Start Headless ChromeDriver by typing the below command on the terminal.

chromedriver --url-base=/wd/hub 

Your Selenium server is now running with Chrome. Use this server to run your test cases written in Selenium using the Google Chrome web browser. The next step is an optional step and doesn’t depend on Step 5.

Step 6 – Sample Java Program (Optional)

This is an optional step. It describes running a single test case using a Selenium standalone server and ChromeDriver. Let’s create a Java program using the Selenium server and Chrome Driver. This Java program will open a specified website URL and check if a defined string is present 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 the file.

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TecAdminSeleniumTest {

        public static void main(String[] args) throws IOException, InterruptedException {
                System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.addArguments("--headless");
                chromeOptions.addArguments("--no-sandbox");

                WebDriver driver = new ChromeDriver(chromeOptions);

                driver.get("https://google.com");

                Thread.sleep(1000);

                if (driver.getPageSource().contains("I'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.jar and testng-6.8.7.jar. Then compile the java program and run it.

export CLASSPATH=".:selenium-server-standalone.jar:testng-6.8.7.jar" 
javac TecAdminSeleniumTest.java 
java TecAdminSeleniumTest 

You will see the results below. If the defined search string is found, You will get the message “Pass” and if the string is not found on the webpage, you will get the “Fail” message on the screen.

Selenium test case results

The post How to Setup Selenium with ChromeDriver on Ubuntu 22.04, 20.04 & 18.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/feed/ 42