Package capture for robot framework with pyshark

94 views Asked by At

I would like to create two keyword for Robot Framework to capture network traffic. Start Package Capture Stop Package Capture

This is the content of networkcapturelibrary.py

import pyshark, threading

class NetworkCaptureLibrary:
    def __init__(self):
        self.capture = None
        self.stop_execution = False

    def _start_package_capture_in_background(self, interface, file_name):
        self.capture = pyshark.LiveCapture(interface=interface, output_file=file_name)
        self.capture.sniff()
        for _ in self.capture:
            if self.stop_execution:
                break

    def start_package_capture(self, interface, file_name):
        self.capture = pyshark.LiveCapture(interface=interface, output_file=file_name)
        self.background_thread = threading.Thread(target=self._start_package_capture_in_background, args=(interface, file_name))
        self.background_thread.start()

    def stop_package_capture(self):
        if self.capture:
            self.stop_execution = True
            self.background_thread.join()

This is the content of test.robot

*** Settings ***
Library    NetworkCaptureLibrary.py

*** Test Cases ***
Capture Traffic Test
    Start Package Capture    Ethernet   captured_traffic.pcap
    Sleep    10s   # Capture traffic for 10 seconds (optional)
    Stop Package Capture

I use self.stop_execution to flag when to finish background process.

The script fails with following error at execution: File "C:\Python311\Lib\threading.py", line 1038, in _bootstrap_inner

Could you advise, what could be the problem.

0

There are 0 answers