Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/neteditTestFunctions/setup.py
169673 views
1
# -*- coding: utf-8 -*-
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file setup.py
15
# @author Pablo Alvarez Lopez
16
# @date 28-05-25
17
18
# imports
19
import os
20
import sys
21
import time
22
import subprocess
23
import pyautogui
24
import pyperclip
25
26
from .input.keyboard import typeKey, keyRelease
27
from .constants import NETEDIT_APP, TEXTTEST_SANDBOX, REFERENCE_PNG
28
from .constants import DELAY_REFERENCE, DELAY_MOUSE_MOVE, DELAY_MOUSE_CLICK
29
30
31
def Popen(extraParameters):
32
"""
33
@brief open netedit
34
"""
35
# set the default parameters of Netedit
36
neteditCall = [NETEDIT_APP]
37
38
# check if a netedit config must be loaded
39
if os.path.exists(os.path.join(TEXTTEST_SANDBOX, "netedit.netecfg")):
40
neteditCall += ['-c netedit.netecfg']
41
42
# add extra parameters
43
neteditCall += extraParameters
44
45
# return a subprocess with Netedit
46
return subprocess.Popen(neteditCall, env=os.environ, stdout=sys.stdout, stderr=sys.stderr, cwd=TEXTTEST_SANDBOX)
47
48
49
def getReferenceMatch(neProcess, makeScrenshot):
50
"""
51
@brief obtain reference referencePosition (pink square)
52
"""
53
# show information
54
print("Finding reference")
55
# make a screenshot
56
errorScreenshot = pyautogui.screenshot()
57
try:
58
# wait for reference
59
time.sleep(DELAY_REFERENCE)
60
# capture screen and search reference
61
positionOnScreen = pyautogui.locateOnScreen(REFERENCE_PNG, minSearchTime=3, confidence=0.95)
62
except Exception as e:
63
# we cannot specify the exception here because some versions of pyautogui use one and some don't
64
print(e)
65
positionOnScreen = None
66
# make a screenshot
67
errorScreenshot = pyautogui.screenshot()
68
# check if pos was found
69
if positionOnScreen:
70
# adjust position to center
71
referencePosition = (positionOnScreen[0] + 16, positionOnScreen[1] + 16)
72
# break loop
73
print("TestFunctions: 'reference.png' found. Position: " +
74
str(referencePosition[0]) + " - " + str(referencePosition[1]))
75
# check that position is consistent (due scaling)
76
if referencePosition != (304, 168):
77
print("TestFunctions: Position of 'reference.png' isn't consistent")
78
# click over position
79
pyautogui.moveTo(referencePosition)
80
# wait
81
time.sleep(DELAY_MOUSE_MOVE)
82
# press i for inspect mode
83
typeKey("i")
84
# click over position (used to center view in window)
85
pyautogui.click(button='left')
86
# wait after every operation
87
time.sleep(DELAY_MOUSE_CLICK)
88
# return reference position
89
return referencePosition
90
# referente not found, then write screenshot
91
if (makeScrenshot):
92
errorScreenshot.saveExistent("errorScreenshot.png")
93
# kill netedit process
94
neProcess.kill()
95
# print debug information
96
sys.exit("TestFunctions: Killed Netedit process. 'reference.png' not found")
97
98
99
def setupAndStart(extraParameters=[], makeScrenshot=True):
100
"""
101
@brief setup and start netedit
102
"""
103
if os.name == "posix":
104
# to work around non working gtk clipboard
105
pyperclip.set_clipboard("xclip")
106
# Open Netedit
107
neteditProcess = Popen(extraParameters)
108
# atexit.register(quit, neteditProcess, False, False)
109
# print debug information
110
print("TestFunctions: Netedit opened successfully")
111
# Release all keys
112
keyRelease("shift")
113
keyRelease("control")
114
keyRelease("alt")
115
# Wait for Netedit reference
116
return neteditProcess, getReferenceMatch(neteditProcess, makeScrenshot)
117
118