Last week TestProject became the first free test automation platform to support for Java, C# & Python, following the launch of their new Open SDK, Wohoo! As a huge fan of TestProject & Python I think this is a really great thing.
In this article we’re going look at all the pieces which make these such an awesome pairing.
The popularity of Python as a test automation scripting language has been growing rapidly over the past few years and it is easy to see why. It’s quick to get started, has a beginner friendly syntax and some incredibly useful libraries. With less ‘boiler plate’ code than some of its counterparts Python has become a language which you can pick up and run with from the offset. This makes it an excellent choice for those starting out or those who just want to write a quick script, without getting bogged down in classes & functions.
Despite its relative simplicity, Python is no less capable in the test automation space than other languages. It of course supports Selenium WebDriver & Appium but it also has a very capable test framework and a brilliant assertion library built in, that’s before we mention some of the available libraries including; Axe (Usability Testing), requests (HTTP requests), Great Expectations (Data Pipeline testing), Hypothesis(Property Based Testing). Oh yeah and let’s not forget pytest, an extremely popular and very powerful test framework.
TestProject is a free automation platform for web, Mobile and API testing which supports completely codeless automation test scripting. Built on top of Selenium & Appium TestProject enables teams to test web, Android & IOS apps with ease. Additionally, TestProject provides excellent reporting capabilities via it’s cloud based app, as well as some great integration with qtest, for test management and Sauce Labs & BrowserStack cross browser/cross device testing in the cloud.
TestProject also contains a host of internal & community built addons to further enhance capabilities and of course, C#, Java and Python SDKs so teams can create TestProject compatible Selenium & Appium tests in code, outside of TestProject. This seems like a good time to go ahead and look at this in a bit more detail.
TestProject Python SDK
Now, here it is. Introducing the TestProject Python SDK
What is an SDK
So first of all, lets start with the basics. What is an SDK? Well, SDK stands for Software Development Kit and in essence, it’s a set of libraries, scripts & code all nicely packaged up to provide all the tools a developer needs to build what they need. TestProject’s Python SDK is exactly that. It’s everything a tester needs to write TestProject compatible Python Selenium tests outside of TestProject.
Why use it?
Ok, so TestProject is pretty good but you may find example where it is not quite capable of doing what you need. For example, TestProject does not support iframes easily (I should mention iframe support is on its way very soon). In this scenario you may find it better to write tests using selenium directly. Also, there mat be times where you want to make use of an external python package you would also need to write your tests outside of TestProject.
The Python SDK enables you to write Selenium tests just as you would normally, whilst allowing you to take full advantage of some of TestProject’s core feature. Using the SDK means your test results will be reported in the TestProject app, just as if they were recorded tests. It also means that you no longer have to worry about managing drivers for each browser as, yep you’ve guessed it, TestProject takes care of this for you.
If you’re on board so far, I guess you’re wondering what the complicated process you’ll need to go through to get your existing python tests up and running in TestProject, well the answer is none.
If you already have a TestProject account and a local agent, then you can run your existing Selenium Tests with TestProject in three simple steps
Running my python Tests with TestProject
Ok, right so I have this very straightforward Selenium test which registers a user using the TestProject example site and checks the greeting message returned, simple right?
from selenium import webdriver def test_register_user(): url = 'https://example.testproject.io/web/' full_name = 'Ryan Howard' password = '12345' # set the driver instance driver = webdriver.Chrome() # browse to the endpoint driver.get(url) # enter full name driver.find_element_by_id("name").send_keys(full_name) # enter a password driver.find_element_by_id("password").send_keys(password) # enter a password driver.find_element_by_id("login").click() # get name text from greeting message name = driver.find_element_by_xpath('/html/body/div/div/p/b').text # confirm name in greeting message matches full name assert name == full_name # quit the driver driver.quit()
So, now we have a passing test, lets look at what we need to do to make our test run using the TestProject SDK. As I mentioned earlier, we’re just three simple steps away
#1 Pip install
To get started we first need to install the SDK. So from your terminal just run
pip install testproject-python-sdk
Nice! Step one done, I assure you it doesn’t get much more complicated
#2 Set your TestProject Developer Token as an Environment Variable in you Pyton Environment.
The Developer Token can be retrieved via the integrations tab in TestProject app
Once you’ve got your token create an environment variable called TP_DEV_TOKEN in you Python environment.
Awesome, 2 steps down. One to go
#3 Lastly, lets return to our script and change one thing. We’re going to replace our Selenium Webdriver import statement
from selenium import webdriver
with the import statement from the installed TestProject SDK.
from src.testproject.sdk.drivers import webdriver
Our script now looks like this.
from src.testproject.sdk.drivers import webdriver def test_register_user(): url = 'https://example.testproject.io/web/' full_name = 'Ryan Howard' password = '12345' # set the driver instance driver = webdriver.Chrome() # browse to the endpoint driver.get(url) # enter full name driver.find_element_by_id("name").send_keys(full_name) # enter a password driver.find_element_by_id("password").send_keys(password) # enter a password driver.find_element_by_id("login").click() # get name text from greeting message name = driver.find_element_by_xpath('/html/body/div/div/p/b').text # confirm name in greeting message matches full name assert name == full_name # quit the driver driver.quit()
If we run it again using pytest our test still passes.
The big difference this time though is that if we go to our TestProject Application we can see that the test is displayed in our reports.
And that’s it! You can now integrate you python selenium tests with TestProject. Just one word of caution though be sure to use driver.quit() at the end of each of your tests, this is how the SDK knows when to report the test.
For full details take a look at the TestProject docs
Other than that, away you go and of course Have fun!
I’ve also created a short demo video taking you through each of these steps if you want to see it in action.