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! |

arrowPlease 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 a GUI Functional Test with Selenium and Python. The advantages of running GUI Functional Tests with Python is that you can modify them directly in BlazeMeter without having to modify the code significantly.

 

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 for writing 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. It supports the python versions - Python 2.7, 3.4+. I will use Python 3.8 in this example.

 

What is Python?

 

Python is an object-oriented programming language that is ideally designed for rapid prototyping of complex applications. Python has interfaces for many OS system calls and libraries and it is extensible to C or C++. Many large companies use Python as their programming language, include NASA, Google, YouTube, and more. The new BlazeMeter Continuous Testing functionality enables running Selenium tests locally  while the requested browsers are run on BlazeMeter’s resources. This means you can get the video test reports in the Blazemeter UI.

Let's say I have a Python Selenium testing I'm running locally.

 

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()

 

To run it in BlazeMeter, I need to set my credentials:

 

API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
API_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

 

Now, set the base Blazemeter URL as base = 'a.blazemeter.com' and add your browser.

 

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

 

Update the 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()

 

We can now run the test.

When looking at the test in BlazeMeter, we can see the commands and the video of the scenario.

 

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

 

However, we're missing the test statuses and test case name.


Let’s divide the test into 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()

 

Next, I will 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)

 

The complete configured test:

 

# 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)

 

In the case of an exception the status should be changed:

 

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 agan BlazeMeter and check the report.

 

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

 

We did it! We can see the test cases and the statuses.

 

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

 

Finally, I changed the assertion in the second 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.

   
arrowPlease enter a URL with http(s)

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