The Talent500 Blog
TestProject Python SDK Tutorial For Selenium & Appium Tests 1

TestProject Python SDK Tutorial For Selenium & Appium Tests

For many years, Selenium and Appium have been among the most popular open-source testing frameworks for desktop browsers and mobile applications.

What Is TestProject Python SDK?

The TestProject Python SDK enhances these tools, providing you access to the full capabilities of the TestProject platform, including elegant HTML and PDF reports, automated browser identification, driver setup, and much more.

Installation and Configuration

Python SDK may be found on PyPI, the Python package index. If you already have a functional Python installation, all you need to do now is install the SDK by running the following command:

pip install testproject-python-sdk

This will install the SDK as well as its dependencies, which include Selenium and the Python Appium client.

We need to execute two more things before we can start using the SDK.

1) Installation and configuration of the TestProject Agent on your machine:

The TestProject Agent is responsible for installing and configuring browser drivers, as well as delivering reports generated by the SDK to the TestProject platform.

You may download your Agent after creating an account on the TestProject platform as it is free.

TestProject Python SDK Tutorial For Selenium & Appium Tests 2

After downloading, you must install and run it. The Agent’s default URL is http://localhost:8585. It is not an issue to run the Agent on a different port, or even on a separate computer. In this situation, all you have to do is enter the right Agent address in an environment variable called TP AGENT URL to tell the SDK where it’s operating.

2) Get and configure a developer token:

A developer token is also required to connect with the Agent. You may obtain your development token from the TestProject platform site after installing the Agent, as indicated in the figure below.

TestProject Python SDK Tutorial For Selenium & Appium Tests 3

To make the SDK aware of your developer token, save it in the environment variable TP DEV TOKEN. You may also specify it as an argument when creating a new driver session, as we’ll see in a moment.

You’re ready to proceed once you’ve downloaded the SDK, installed, configured, and launched the Agent, and got and configured your developer token.

  • Creating the First TestProject-powered Selenium Test

Assume we have a Selenium-based test that calls the TestProject example web app. It offers login credentials and validates that we are welcomed to signal that the login activity was successful. Assume we run this test and execute the assertions using the Pytest unit testing framework.

Such type of test might look like this:

from selenium import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome()

    driver.get(‘https://example.testproject.io/web/’)

    driver.find_element_by_id(‘name’).send_keys(‘John Smith’)

    driver.find_element_by_id(‘password’).send_keys(‘12345’)

    driver.find_element_by_id(‘login’).click()

    assert driver.find_element_by_id(‘greetings’).is_displayed() is True

    driver.quit()

Chrome is the browser used in the preceding example. The SDK supports the following desktop browsers in addition to Chrome:

Firefox
Edge
Internet Explorer
Safari

We haven’t utilised any of the abstraction patterns that are prevalent in Selenium-based testing, such as Page Objects, in this example, but if you do, that’s OK. In fact, we strongly advise against it since it establishes a clear distinction between your test flow (actions, test data) and the implementation specifics of your web pages (element locators).

After completing all of the preceding installation and configuration processes, all that remains is to modify the import line as indicated below to convert this test into a TestProject-powered test.

From selenium import webdriver

Replace with this one:

from src.testproject.sdk.drivers import webdriver

That’s all! When you run the test now, the SDK will obtain a driver instance from the TestProject Agent and use it to conduct the test. It will also transmit reporting instructions to the TestProject platform, which will be utilised to generate HTML reports. Let us have a look at them!

  • Reports Inspection On The TestProject Platform

By going to the TestProject and choosing the ‘Reports’ choices from the menu, you can see that a new report for the test we just ran has been produced. Please see the image below.

TestProject Python SDK Tutorial For Selenium & Appium Tests 4

As you can see, the SDK inferred a project name (software testing help), a job name (examples), and a test name (test login to testproject example app) and utilised these to generate the report. This is supported for Pytest and Unittest, as well as tests that do not use a particular unit testing framework.

In the next section, we’ll look at how to define customised project, task, and test names, as well as a variety of other useful reporting settings.

Any driver commands executed during the test are automatically included in the report, along with their results. TestProject automatically provides overviews and dashboards.

  • Reporting Customization Options with TestProject

As seen in the above example, TestProject can infer project, job, and test names for the most popular Python unit testing frameworks. But, if you wish to utilise custom names in your reports, there are two ways to do it.

1) Using a decorator

The TestProject SDK additionally has a @report decorator for decorating your test methods and specifying custom project, job, and test names, as illustrated below:

from src.testproject.sdk.drivers import webdriver

from src.testproject.decorator import report 

@report(project=”Software Testing Help”, job=”SDK Examples”, test=”Login Test”)

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome()

When we run this decorated test method and review the results, we can see that the provided names, rather than the automatically inferred ones, were utilised in the resulting report.

TestProject Python SDK Tutorial For Selenium & Appium Tests 5

 

2) Specifying project and job names in the driver function and manually submitting a test:

Overriding project and job names is also possible by defining them in the driver object’s function. This may be accomplished by doing the following:

from src.testproject.sdk.drivers import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome(projectname=’Software Testing Help’,

 jobname=’SDK Examples’)

If you want to alter the automatically inferred test name, you may manually report a test at the conclusion of the test, as seen below:

from src.testproject.sdk.drivers import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome(projectname=’Software Testing Help’,

 jobname=’SDK Examples’)

    driver.get(‘https://example.testproject.io/web/’)

    driver.find_element_by_id(‘name’).send_keys(‘John Smith’)

    driver.find_element_by_id(‘password’).send_keys(‘12345’)

    driver.find_element_by_id(‘login’).click()

    assert driver.find_element_by_id(‘greetings’).is_displayed() is True

    driver.report().test(name=’Login Test’, passed=True)

    driver.quit()

If you choose manual reporting, you should stop automated testing reporting (which is set by default) to prevent tests from being reported twice, which would distort your report and dashboards.

You may prevent automatic reporting using the command:

from src.testproject.sdk.drivers import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome()

    driver.report().disable_auto_test_reports(disabled=True)

This will result in the report looking precisely like the last screenshot above.

3) Disabling Reporting of Driver commands

If you don’t want your report to include every single driver command that was executed during the test, deactivate automatic reporting of them as follows:

from src.testproject.sdk.drivers import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome()

    driver.report().disable_command_reports(disabled=True)

Later in your tests, you can re-enable driver command reporting by executing the same method again but with the option disabled=False.

You may still report certain intermediate stages throughout your test if you want to:

from src.testproject.sdk.drivers import webdriver

def test_login_to_testproject_example_app():

    driver = webdriver.Chrome()

    driver.report().disable_command_reports(disabled=True)

    driver.report().step(description=”An intermediate step”, message=”A

custom message”, passed=True, screenshot=True)

 

You may even include screenshots to your own report steps, as you can see. This will be automatically included into the HTML report on the TestProject platform.

TestProject Python SDK Tutorial For Selenium & Appium Tests 6

  • Running Appium-based Tests Using TestProject

In addition to Selenium-based tests, the TestProject SDK can execute Appium tests on mobile devices. Take the following example, which is being executed in an emulator versus a native Android app:

from appium import webdriver

def test_native_android_app():

         desired_capabilities = {

        “appActivity”: “io.testproject.demo.MainActivity”,

        “appPackage”: “io.testproject.demo”,

        “udid”: “<your emulator name goes here>”,

        “browserName”: “”,

        “platformName”: “Android”,

    }

        driver = webdriver.Remote(desired_capabilities=desired_capabilities)

    driver.find_element_by_id(‘name’).send_keys(‘John Smith’)

    driver.find_element_by_id(‘password’).send_keys(‘12345’)

    driver.find_element_by_id(‘login’).click()

    assert driver.find_element_by_id(‘greetings’).is_displayed()

    driver.quit()

Again, we only need to alter one thing to harness the potential of the TestProject platform.

from appium import webdriver

To:

from src.testproject.sdk.drivers import webdriver

The TestProject Agent also serves as the Appium server, so you don’t have to run it on the computer where your tests are executing.

All of the above-mentioned reporting tools are also accessible for Appium-based tests.

Conclusion

As you can see from this blog, the TestProject Python SDK can help you supercharge your existing Selenium and Appium-based tests by automating the configuration of your browser drivers and Appium server, as well as generating fantastic HTML results and dashboards for you on the TestProject platform.

0
Afreen Khalfe

Afreen Khalfe

A professional writer and graphic design expert. She loves writing about technology trends, web development, coding, and much more. A strong lady who loves to sit around nature and hear nature’s sound.

Add comment