Easy user-interface testing in Python!

In this blog post, I will explain how to build a small Web UI testing solution using Python and Selenium.

What is UI testing?

UI testing is a software testing type that controls the User Interface of a target application under test. UI testing involves checking forms and UI elements to ensure UI functionality works as per the specification and achieves a high standard of quality.

What is Selenium?

Selenium is an open-source and portable framework tool which is used for automating the tests carried out on web browsers. Basically, Selenium tells a browser to click some element, populate and submit a form, navigate to a page or do any other form of user interaction. It also provides a test domain-specific language to build tests some of the popular programming languages, such as C#, Java, Scala, Perl, and Python.

In the first stage, we have to make an isolated environment, use virtualenv:

virtualenv env --python==python2.7
source env/bin/activate

   		  
For this test we will open "https://www.google.com/" and make sure that the title of the page is "Google".

from selenium import webdriver
 
class AweberTest(unittest.TestCase):
 
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
 
    def test_title(self):
        self.driver.get('https://www.google.com')
        self.assertEqual(
            self.driver.title,
            'Google')
 
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

   		  
Asking the user for input:

from selenium import webdriver 
  
# Taking input from user 
search_string = input("Input the URL or string you want to search for:") 
  
# This is done to structure the string  
# into search url.(This can be ignored) 
search_string = search_string.replace(' ', '+')  
  
# Assigning the browser variable with chromedriver of Chrome. 
# Any other browser and its respective webdriver  
# like geckodriver for Mozilla Firefox can be used 
browser = webdriver.Chrome('chromedriver') 
  
for i in range(1): 
    matched_elements = browser.get("https://www.google.com/search?q =" +
                                     search_string + "&start =" + str(i)) 

   		  
Taking search string in the command line itself:

from selenium import webdriver 
import sys 
  
# function to convert a list into string 
def convert(s):  
    str1 = ""  
    return(str1.join(s))  
        
# Assign the arguments passed to a variable search_string 
search_string = sys.argv[1:]  
  
# The argument passed to the program is accepted 
# as list, it is needed to convert that into string 
search_string = convert(search_string) 
  
# This is done to structure the string  
# into search url.(This can be ignored) 
search_string = search_string.replace(' ', '+')  
  
  
# Assigning the browser variable with chromedriver of Chrome. 
# Any other browser and its respective webdriver  
# like geckodriver for Mozilla Firefox can be used 
browser = webdriver.Chrome('chromedriver') 
  
  
for i in range(1): 
    matched_elements = browser.get("https://www.google.com/search?q =" + 
                                   search_string + "&start =" + str(i)) 

   		  

Posted by Mohammadreza in April 2020

I'm a computer security researcher and a full-stack programmer.