How to Run Selenium Tests on Chrome?

We offer you a brighter future with FREE online courses - Start Now!!

A well-liked open-source framework for automating web browsers is called Selenium. It allows developers and testers to automate web applications and perform functional and regression testing of web applications. Selenium supports multiple browsers like Chrome, Firefox, Edge, and Safari. In this article, we will focus on how to run selenium tests on Chrome browser.

ChromeDriver

The WebDriver protocol is implemented for Chrome by ChromeDriver, a standalone server. It is maintained by the Chromium team and is available for download on the Chromium website. ChromeDriver starts the Chrome browser instance and sends commands to it via the WebDriver interface.

Setting up ChromeDriver

Before we can run tests on Chrome, we need to download and set up ChromeDriver. We need to ensure that the ChromeDriver version matches the Chrome browser version installed on our system. We can check the Chrome browser version by going to the “Chrome Menu” -> “Help” -> “About Google Chrome”. Once we have the Chrome browser version, we can download the appropriate version of ChromeDriver from the Chromium website.

Here’s a step-by-step guide to setting up Selenium Chromedriver:

1. First, download the latest version of Chromedriver from the official website. Make sure you download the appropriate version for your operating system and Chrome browser version.

2. Extract the Chromedriver executable file from the downloaded zip file to a directory on your computer.

3. Next, install the Selenium library for Python by running the command: pip install selenium. This will install the Selenium Python package, which includes the necessary modules for interacting with the Chromedriver.

4. Now you’re ready to write your first Selenium test script. Here’s an example script that navigates to Google.com and searches for a query:

from selenium import webdriver

chromedriver_path = '/path/to/chromedriver'
driver = webdriver.Chrome(chromedriver_path)
driver.get('https://www.google.com')
search_box = driver.find_element_by_name('q')
search_box.send_keys('Selenium Chromedriver')
search_box.submit()
driver.implicitly_wait(10)
driver.quit()

5. Replace the ‘/path/to/chromedriver’ in the script above with the actual path to the Chromedriver executable file on your computer.

6. Save the script to a file with a .py extension, such as selenium_test.py.

7. Run the script from the command line by running the command python selenium_test.py. This will open a new Chrome window and execute the test script.

After downloading ChromeDriver, we need to add the location of the ChromeDriver executable to the PATH environment variable. This ensures that the system can locate ChromeDriver when we run our tests.

Writing Tests in Selenium 

Selenium provides a rich set of APIs to interact with web pages. We can use these APIs to simulate user interactions with the web page, verify page elements, and perform various other tasks. Here is a basic test that opens the Chrome browser, navigates to a web page, enters text into a search box, and clicks on the search button:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('https://www.google.com')

search_box = browser.find_element_by_name('q')

search_box.send_keys('Selenium')

search_button = browser.find_element_by_name('btnK')

search_button.click()

browser.quit()

Running Selenium Tests on Chrome Browser

To run the test, we need to save the test script to a file with the .py extension and execute the file using Python. The following command will execute the test script:

‘py test_script.py’

When we run the test, we should see a new Chrome browser window open, navigate to the Google homepage. Then it will enter the search query, click the search button, and close the browser.

Conclusion

In this article, we learned how to set up ChromeDriver and run tests on Chrome using Selenium. We also saw a simple example of how to write a test script in Selenium. Selenium provides a powerful toolset for automating web applications, and Chrome is just one of the many browsers it supports. With the help of Selenium, we can automate repetitive tasks, perform regression testing, and ensure the quality of our web applications.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *