Kirill Kuprienko is  Automation Engineer at BlazeMeter. He specializes in automation testing of Blazemeter products with Selenium and  Python.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW!

arrow Please enter a URL with http(s)
Dec 23 2019

Getting started with Selenium, Python, and BlazeMeter GUI Functional Testing

In this blog post I will explain how to run GUI Functional Testing with Selenium and Python. The advantages of running GUI Functional Tests with Python is that if you’ve already got a project in Python that you can modify this project directly in BlazeMeter without needing to greatly modify the code.

 

What is Selenium?

 

Selenium is a portable framework for testing web applications. Selenium provides a playback tool for authoring functional tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can run on most modern web browsers. Selenium runs on Windows, Linux, and macOS. Supported python versions - Python 2.7, 3.4+ I will use Python 3.8

 

What is Python?

 

Python is an object-oriented programming language. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries and is extensible to C or C++. Many large companies use the Python programming language include NASA, Google, YouTube, etc. With new Blazemeter Continuous Testing functionality i can run my Selenium tests locally but requested browsers will be run in Blazemeter’s resources. This means I’ll get reports with video of my tests in the Blazemeter UI. I Have a Python Selenium test and run it locally. Now let's look at how i can run it with Blazemeter.

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

 

 

At first i need to set my credentials in Blazemeter.

 

API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
API_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

 

Now let’s set the base Blazemeter URL as base = 'a.blazemeter.com' And add your browser.

 

 

desired_capabilities = {
   'browserName': 'firefox',
   # 'browserVersion': 70,
}

 

Now I can update my webdriver with the new parameters.

 

blazegrid_url = 'https://{}:{}@{}/api/v4/grid/wd/hub'.format(API_KEY, API_SECRET, base)
driver = webdriver.Remote(command_executor=blazegrid_url, desired_capabilities=desired_capabilities)

 

My final script :

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# creds
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
API_SECRET ='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
base = 'a.blazemeter.com'

### BlazeGrid capabilites
desired_capabilities = {
	'browserName': 'chrome'}


blazegrid_url = 'https://{}:{}@{}/api/v4/grid/wd/hub'.format(API_KEY, API_SECRET, base)
driver = webdriver.Remote(command_executor=blazegrid_url, desired_capabilities=desired_capabilities)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

 

Now let’s run it.

And look into my account  on a.blazemeter.com

New GUI Functional Test started
Open report
Now the test has ended and i see my commands and the video of my scenario.
 

Getting started with Selenium, Python, and BlazeMeter GUI Functional Test

But there is no any test statuses and test case name.
Let’s divide my test by test cases.
 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
 

 

# creds
API_KEY = ''
API_SECRET = ''
base = 'a.blazemeter.com'

### BlazeGrid capabilites
desired_capabilities = {
	'browserName': 'chrome'}


blazegrid_url = 'https://{}:{}@{}/api/v4/grid/wd/hub'.format(API_KEY, API_SECRET, base)
driver = webdriver.Remote(command_executor=blazegrid_url, desired_capabilities=desired_capabilities)

# open url
driver.get("http://www.python.org")

# check title
assert "Python" in driver.title

# fill search filed
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")

# check is search not empty
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

 

Great, now I am going to configure the steps for my GUI Functional test in BlazeMeter.

Let's look at 1st test case # open url

To set up the name i will use the commands below:

args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Fill search filed', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)

For set status :
args = {'status': 'passed', 'message': '',}  

 

And then

 

driver.execute_script("/* FLOW_MARKER test-case-stop */", args)

 

Ok now i can configure all my test like the example below:

 

# open url
args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Open url', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
try:
   driver.get("http://www.python.org")
   args = {
       'status': 'passed',
       'message': '',
   }
except AssertionError as exc:
   args = {
       'status': 'failed',
       'message': str(exc),
   }
except BaseException as exc:
   args = {
       'status': 'broken',
       'message': str(exc),
   }
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)

 

Status should be changed if we have an exception.

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# creds
API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
API_SECRET ='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

base = 'a.blazemeter.com'

### BlazeGrid capabilites
desired_capabilities = {
   'browserName': 'chrome'}

blazegrid_url = 'https://{}:{}@{}/api/v4/grid/wd/hub'.format(API_KEY, API_SECRET, base)
driver = webdriver.Remote(command_executor=blazegrid_url, desired_capabilities=desired_capabilities)

# open url
args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Open url', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
try:
   driver.get("http://www.python.org")
   args = {
       'status': 'passed',
       'message': '',
   }
except AssertionError as exc:
   args = {
       'status': 'failed',
       'message': str(exc),
   }
except BaseException as exc:
   args = {
       'status': 'broken',
       'message': str(exc),
   }
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)

# check title
args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Check title', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
try:
   assert "Python2" in driver.title
   args = {
       'status': 'passed',
       'message': '',
   }
except AssertionError as exc:
   args = {
   'status': 'failed',
   'message': str(exc),
}
except BaseException as exc:
   args = {
   'status': 'broken',
   'message': str(exc),
}
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)


# fill search filed
args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Fill search filed', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
try:
   elem = driver.find_element_by_name("q")
   elem.send_keys("pycon")
   args = {
       'status': 'passed',
       'message': '',
   }
except AssertionError as exc:
   args = {
   'status': 'failed',
   'message': str(exc),
}
except BaseException as exc:
   args = {
   'status': 'broken',
   'message': str(exc),
}
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)

# check is search not empty
args = {'testSuiteName': 'Simple Gui Test', 'testCaseName': 'Check is search not empty', }
driver.execute_script("/* FLOW_MARKER test-case-start */", args)
try:
   elem.send_keys(Keys.RETURN)
   assert "No results found." not in driver.page_source
   args = {
       'status': 'passed',
       'message': '',
   }
except AssertionError as exc:
   args = {
   'status': 'failed',
   'message': str(exc),
}
except BaseException as exc:
   args = {
   'status': 'broken',
   'message': str(exc),
}
driver.execute_script("/* FLOW_MARKER test-case-stop */", args)
driver.close()

 

Let’s try to run it in BlazeMeter and check the report.

 

Getting started with Selenium, Python, and BlazeMeter GUI Functional Test

 

Now i see my test cases and the statuses.

 

Getting started with Selenium, Python, and BlazeMeter GUI Functional Test

Amazing! 

And finally I change the assertion  in the 2nd test case:

assert "Python2" in driver.title and run it again:

Getting started with Selenium, Python, and BlazeMeter GUI Functional Test

 

With BlazeMeter’s new Continuous Testing platform you can run all your tests in one easy platform, including performance testing, GUI Functional and API Monitoring, as well as Mock Services capabilities.

 

To run your own GUI Functional Tests in BlazeMeter simply log in here, or put your url in the box below.

     
arrow Please enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!