17Apr 2023

Modern Web Automation with Python and Selenium: Everything You Need To Know

These days, Python is gaining a lot of popularity as a programming language owing to the ease of use that it provides. That is not the only reason why people prefer Python, but people love it because of the ease of use that it offers. In the year 1989, Guido van Rossum came up with the idea of Python to overcome the challenges posed by ABC language.

In order to meet the shortcomings of ABC language, Rossum decided to introduce Python integrating the existing features of ABC language while also adding new features, including exception handling and extensibility. In the year 1994, Python 1.0 was released. This version of Python implemented module system from Modula-3 and had the ability to handle interaction with the operating system of Amoeba and came with programming tools that were capable of giving out-of-the-box results.

So now, let us get back to Python and Selenium. Selenium is considered as one of the best tools available for automation testing related to web apps. And Python can be used to write Selenium scripts in a simple way. There are other programming languages that can be used to write Selenium but might be way too complex.

Let us Take a Look at Selenium

Let us take a look at Selenium

Selenium, as we discussed earlier in the article, is generally used for automation or automating test cases for web apps or on web browsers. The web apps that are subjected to automation testing would be tested on web browsers. Also, only web apps can be tested using Selenium. You cannot use Selenium to test desktop software apps or mobile apps. Being an open-source tool, Selenium comes with a lot more features and functionalities that you would love. One of these includes cross-browser compatibility. Also, the best out of all is that it enables automation of web apps.  

One of the other important benefits of Selenium, apart from being an open-source tool, is that there are no licensing costs involved with it. The test cases, support for multiple major browsers and OS platform are the other benefits of Selenium.

So now, you know why to choose Selenium when there might be a lot of other tools that might seem to appease but might not provide as many features as Selenium does. Next, let us take a look at Python.

As discussed, the first and foremost thing about Python is that it can be easily used to write Selenium scripts. Regardless of all other factors, highlights of Python is that it is easy to understand and simple to learn. Moreover, experts often compare its ease of understandability with that of the English language.

There is good support available for Python as it comes with a huge community back up. It can be easily interpreted and is high-level. And the best part of Python is that it is available for free. Now, the most amazing part of Python is that, just like Selenium, it is open-source.

Apart from these interesting factors, Python offers a range of in-built testing frameworks, which help with debugging, offering fast workflows. It also can be integrated with tools, including Splinter and Selenium to make things further easy. Cross-platform and cross-browser testing becomes much easy with Python as it offers frameworks, including PyTest and Robot framework for the same.

Now, let us take a look at how Selenium and Python can be bound together for better.

First, let us start with the setup.

Setup

Before you begin with writing code for Python, you need to install a web driver that supports for whichever web browser you are using – regardless of whether it is Firefox or Chrome. Now, assume that the path ~/.local/bin is in your execution PATH – we will take you through how to install the Firefox web driver, gecko driver for a Linux OS:

$ wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
$ tar xvfz geckodriver-v0.19.1-linux64.tar.gz
$ mv geckodriver ~/.local/bin

In the next step, you need to install the Selenium package – click here for further details. For a virtual environment, all you have to do is to type the below:

$ pip install selenium

Let us find out about Selenium installation.

For a start, you need to have Selenium language bound for Python installation ready. With pip, now run the command below in your terminal:

sudo pip install selenium

Calls to RESTful API requests in Python are recommended. Get this installed with pip, typing the below-given command in your terminal:

sudo pip install requests

Are you new to unit testing? Would you like to know further about Python’s module? Then you can get the full documentation here.

Let us Get Into Running Your First Test

Now, that you are done with the installation process, let us run the first automated test. All you have to do is copy and paste the code given below within your text editor of choice. Once done, try running it from the terminal:

import unittest
from selenium import webdriver
import requests
   class SeleniumCBT(unittest.TestCase):
           def setUp(self):
           self.username = “user@email.com”
           self.authkey  = “12345”
           self.api_session = requests.Session()
           self.api_session.auth = (self.username,self.authkey)
           self.test_result = None
           caps = {}
           caps[‘browserName’] = ‘Chrome’
           caps[‘version’] = ’60×64′
           caps[‘platform’] = ‘Windows 10’
           caps[‘screenResolution’] = ‘1366×768’
           self.driver = webdriver.Remote(
           desired_capabilities=caps,
           command_executor=”http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub”%(self.username,self.authkey)
           )
           self.driver.implicitly_wait(20)
           def test_CBT(self):
           self.driver.get(‘http://crossbrowsertesting.github.io/selenium_example_page.html’)
           self.assertEqual(“Selenium Test Example Page”, self.driver.title)
           self.test_result = ‘pass’
           self.driver.quit()
if __name__ == ‘__main__’:
          unittest.main()

Learning About Test Capabilities

Learning about test capabilities

Well, with cross-browser testing, it is possible to add certain capabilities to your test. These capabilities include video recordings, naming structure and others.

What is With the Naming Tests?

It is important to name your tests. This is recommended as it would help you to arrange your automated test cases, which will help with making debugging easier. Also, mark your Build numbers as a representation of the releases.

Naming your tests can help organize your automated test cases for easier debugging. You can also mark Build number to denote releases.

caps[‘name’] = ‘Selenium Test Example’
caps[‘build’] = ‘1.0’

Let us Find More About How to Choose Your Browsers.

For choosing your environment, you need to identify the right type of browsers, operating system, resolutions, and devices as well.

caps[‘browserName’] = ‘Chrome’

How do you Carry Out the Real Device Testing?

How do you carry out the real device testing

Here, we will take you across some of the Selenium tests with real mobile browsers. All you have to do is set up a few unique capabilities as given below:

caps[‘browserName’] = ‘Chrome’
caps[‘deviceName’] = ‘Nexus 6P’
caps[‘platformVersion’] = ‘7.0’
caps[‘platformName’] = ‘Android’
caps[‘deviceOrientation’] = ‘portrait’

How do you Record Videos?

Cross-browser testing provides you with the ease to record a video of your Selenium test session. For more details, click here.

caps[‘record_video’] = ‘true’

How do you Record Network?

How do you record network

For recording network sets while testing for performance debugging, you need to set the below given to ‘true’.

caps[‘record_network’] = ‘true’

How Do You Run Local Tests?

It is possible to run a test on a local URL or behind the firewall of your company.

For more details on setup, initiating and starting the tunnel connection, click here.

It is possible for automated tests to access websites locally, across a proxy or behind a firewall, by opening a tunnel. You need not set a capability during the test.

Can You Run Tests in Parallel?

In order to speed your automated testing, you have the option to automate tests in parallel by making use of various browsers or devices simultaneously. For parallel testing in Python, you need to use the queue dependency.

from queue import Queue
from threading import Thread
from selenium import webdriver
import time
USERNAME = “user@email.com”
API_KEY = “12345”
q = Queue(maxsize=0)
browsers = [
 {“os_api_name”: “Win7x64-C2”, “browser_api_name”: “IE10”, “name”: “Python Parallel”},
 {“os_api_name”: “Win8.1”, “browser_api_name”: “Chrome43x64”, “name”: “Python Parallel”},
] # put all of the browsers into the queue before pooling workers
Browser within browser:
          q.put(browser)
num_threads = 10
def test_runner(q):
          while q.empty() is False:
           try:
           browser = q.get()
           print(“%s: Starting” % browser[“browser_api_name”])
           driver = webdriver.Remote(desired_capabilities=browser, command_executor=“http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub” % (USERNAME, API_KEY) )
           print(“%s: Getting page” % browser[“browser_api_name”])
           driver.get(“http://crossbrowsertesting.com”)
           print(“%s: Quitting browser and ending test” % browser[“browser_api_name”])
           except:
           print(“%s: Error” % browser[“browser_api_name”])
           finally:
           driver.quit()
           time.sleep(15)
           q.task_done()
for i in range(num_threads):
          worker = Thread(target=test_runner, args=(q,))
          worker.setDaemon(True)
          worker.start()
q.join()

How to Take Snapshots?

How to take snapshots

In case you encounter errors while testing, you can take snapshots of these errors for easier debugging and documentation. Then, you can share these via email, Jira or Slack. All you have to do is invoke an API as given below in order to take snapshots while you test run.

snapshot_hash = self.api_session.post(‘https://crossbrowsertesting.com/api/v3/selenium/’ + self.driver.session_id + ‘/snapshots’).json()[‘hash’]

Check the example below for more details:

import unittest
from selenium import webdriver
import requests
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
class LoginForm(unittest.TestCase):
          def setUp(self):
           # Insert your username and authkey here
           # Find your authkey at crossbrowsertesting.com/account
           self.username = “user@email.com”
           self.authkey  = “12345”
           self.api_session = requests.Session()
           self.api_session.auth = (self.username,self.authkey)
        self.test_result = None
           caps = {}
           caps[‘name’] = ‘Screenshot Example’
           caps[‘browser_api_name’] = ‘Chrome53’
           caps[‘os_api_name’] = ‘Win10’
           caps[‘screen_resolution’] = ‘1024×768’
           self.driver = webdriver.Remote(
           desired_capabilities=caps,
           command_executor=“http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub”%(self.username,self.authkey)
           )
           self.driver.implicitly_wait(20)
           def test_CBT(self):        

       try:
           self.driver.get(‘http://crossbrowsertesting.github.io/login-form.html’)
           self.driver.maximize_window()
           self.driver.find_element_by_name(‘username’).send_keys(‘tester@crossbrowsertesting.com’)
           self.driver.find_element_by_name(‘password’).send_keys(‘test123’)
           self.driver.find_element_by_css_selector(‘body > div > div > div > div > form > div.form-actions > button’).click()
           elem = WebDriverWait(self.driver, 10).until(
           EC.presence_of_element_located((By.XPATH, ‘//*[@id=\”logged-in-message\”]/h2’))
           )
           welcomeText = elem.text
           self.assertEqual(“Welcome tester@crossbrowsertesting.com”, welcomeText)
           print(“Taking snapshot”)
           snapshot_hash = self.api_session.post(‘https://crossbrowsertesting.com/api/v3/selenium/’ + self.driver.session_id + ‘/snapshots’).json()[‘hash’]            self.test_result = ‘pass’
           except AssertionError as e:
           # log the error message, and set the score to “during tearDown()”.
           self.api_session.put(‘https://crossbrowsertesting.com/api/v3/selenium/’ + self.driver.session_id + ‘/snapshots/’ + snapshot_hash,
           data={‘description’:“AssertionError: “ + str(e)})
           self.test_result = ‘fail’
           raise
           def tearDown(self):
           print(“Done with session %s” % self.driver.session_id)
           self.driver.quit()
           # Here we make the api call to set the test’s score.
           # Pass it it passes, fail if an assertion fails, unset if the test didn’t finish
           if self.test_result is not None:
           self.api_session.put(‘https://crossbrowsertesting.com/api/v3/selenium/’ + self.driver.session_id,
            data={‘action’:‘set_score’, ‘score’:self.test_result})
if __name__ == ‘__main__’:
          unittest.main()

Now, you have the complete details on test running with Python and Selenium.

Acodez IT Solutions is a web design and web development company in India. We provide web-related services to our clients across the globe. We are also an SEO agency offering inbound marketing solutions at affordable prices. For more information, contact us today.

Looking for a good team
for your next project?

Contact us and we'll give you a preliminary free consultation
on the web & mobile strategy that'd suit your needs best.

Contact Us Now!
Jamsheer K

Jamsheer K

Jamsheer K, is the Tech Lead at Acodez. With his rich and hands-on experience in various technologies, his writing normally comes from his research and experience in mobile & web application development niche.

Get a free quote!

Brief us your requirements & let's connect

Leave a Comment

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