# -*- coding: utf-8 -*-1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file setup.py14# @author Pablo Alvarez Lopez15# @date 28-05-251617# imports18import os19import sys20import time21import subprocess22import pyautogui23import pyperclip2425from .input.keyboard import typeKey, keyRelease26from .constants import NETEDIT_APP, TEXTTEST_SANDBOX, REFERENCE_PNG27from .constants import DELAY_REFERENCE, DELAY_MOUSE_MOVE, DELAY_MOUSE_CLICK282930def Popen(extraParameters):31"""32@brief open netedit33"""34# set the default parameters of Netedit35neteditCall = [NETEDIT_APP]3637# check if a netedit config must be loaded38if os.path.exists(os.path.join(TEXTTEST_SANDBOX, "netedit.netecfg")):39neteditCall += ['-c netedit.netecfg']4041# add extra parameters42neteditCall += extraParameters4344# return a subprocess with Netedit45return subprocess.Popen(neteditCall, env=os.environ, stdout=sys.stdout, stderr=sys.stderr, cwd=TEXTTEST_SANDBOX)464748def getReferenceMatch(neProcess, makeScrenshot):49"""50@brief obtain reference referencePosition (pink square)51"""52# show information53print("Finding reference")54# make a screenshot55errorScreenshot = pyautogui.screenshot()56try:57# wait for reference58time.sleep(DELAY_REFERENCE)59# capture screen and search reference60positionOnScreen = pyautogui.locateOnScreen(REFERENCE_PNG, minSearchTime=3, confidence=0.95)61except Exception as e:62# we cannot specify the exception here because some versions of pyautogui use one and some don't63print(e)64positionOnScreen = None65# make a screenshot66errorScreenshot = pyautogui.screenshot()67# check if pos was found68if positionOnScreen:69# adjust position to center70referencePosition = (positionOnScreen[0] + 16, positionOnScreen[1] + 16)71# break loop72print("TestFunctions: 'reference.png' found. Position: " +73str(referencePosition[0]) + " - " + str(referencePosition[1]))74# check that position is consistent (due scaling)75if referencePosition != (304, 168):76print("TestFunctions: Position of 'reference.png' isn't consistent")77# click over position78pyautogui.moveTo(referencePosition)79# wait80time.sleep(DELAY_MOUSE_MOVE)81# press i for inspect mode82typeKey("i")83# click over position (used to center view in window)84pyautogui.click(button='left')85# wait after every operation86time.sleep(DELAY_MOUSE_CLICK)87# return reference position88return referencePosition89# referente not found, then write screenshot90if (makeScrenshot):91errorScreenshot.saveExistent("errorScreenshot.png")92# kill netedit process93neProcess.kill()94# print debug information95sys.exit("TestFunctions: Killed Netedit process. 'reference.png' not found")969798def setupAndStart(extraParameters=[], makeScrenshot=True):99"""100@brief setup and start netedit101"""102if os.name == "posix":103# to work around non working gtk clipboard104pyperclip.set_clipboard("xclip")105# Open Netedit106neteditProcess = Popen(extraParameters)107# atexit.register(quit, neteditProcess, False, False)108# print debug information109print("TestFunctions: Netedit opened successfully")110# Release all keys111keyRelease("shift")112keyRelease("control")113keyRelease("alt")114# Wait for Netedit reference115return neteditProcess, getReferenceMatch(neteditProcess, makeScrenshot)116117118