test_rwt_plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2021, Kei Okada
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of the Copyright holder. nor the
20 # names of its contributors may be used to endorse or promote products
21 # derived from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 #
36 
37 import argparse
38 import sys
39 import time
40 import rospy
41 import rostest
42 import unittest
43 
44 from selenium import webdriver
45 from selenium.webdriver.common.keys import Keys
46 from selenium.webdriver.common.by import By
47 from selenium.webdriver.support import expected_conditions as EC
48 from selenium.webdriver.support.ui import Select
49 
50 import pkg_resources
51 selenium_version = pkg_resources.get_distribution("selenium").version
52 # Check if selenium version is greater than 4.3.0
53 if pkg_resources.parse_version(selenium_version) >= pkg_resources.parse_version("4.3.0"):
54  from selenium.webdriver.support.ui import WebDriverWait
55  from selenium.webdriver.common.by import By
56 
57 from std_msgs.msg import Float64
58 
59 CLASSNAME = 'rwt_plot'
60 
61 class TestRwtPlot(unittest.TestCase):
62 
63  def sin_cb(self, msg):
64  self.sin_msg = msg
66 
67  def __init__(self, *args):
68  super(TestRwtPlot, self).__init__(*args)
69  rospy.init_node('test_rwt_plot')
70 
71  def setUp(self):
72  parser = argparse.ArgumentParser()
73  parser.add_argument('--no-headless', action='store_true',
74  help='start webdriver with headless mode')
75  args, unknown = parser.parse_known_args()
76 
77  self.sin_msg = None
78  self.sin_msg_received = 0
79 
80  rospy.Subscriber('/sin', Float64, self.sin_cb)
81  self.url_base = rospy.get_param("url_roswww_testserver")
82 
83  opts = webdriver.firefox.options.Options()
84  if not args.no_headless:
85  opts.add_argument('-headless')
86  self.browser = webdriver.Firefox(options=opts)
87 
88  self.wait = webdriver.support.ui.WebDriverWait(self.browser, 10)
89  # maximize screen
90  if pkg_resources.parse_version(selenium_version) >= pkg_resources.parse_version("4.3.0"):
91  self.browser.fullscreen_window()
92  else:
93  self.browser.find_element_by_tag_name("html").send_keys(Keys.F11)
94 
95  def tearDown(self):
96  try:
97  self.browser.close()
98  self.browser.quit()
99  except:
100  pass
101 
102  def test_rwt_plot(self):
103  url = '%s/rwt_plot' % (self.url_base)
104  rospy.logwarn("Accessing to %s" % url)
105 
106  self.browser.get(url)
107 
108  # check settings
109  self.wait.until(EC.presence_of_element_located((By.ID, "button-ros-master-settings")))
110  settings = self.find_element_by_id("button-ros-master-settings")
111  self.assertIsNotNone(settings, "Object id=button-ros-master-settings not found")
112  settings.click()
113 
114  self.wait.until(EC.presence_of_element_located((By.ID, "input-ros-master-uri")))
115  uri = self.find_element_by_id("input-ros-master-uri")
116  self.assertIsNotNone(uri, "Object id=input-ros-master-uri not found")
117  uri.clear();
118  uri.send_keys('ws://localhost:9090/')
119 
120  self.wait.until(EC.presence_of_element_located((By.ID, "button-ros-master-connect")))
121  connect = self.find_element_by_id("button-ros-master-connect")
122  self.assertIsNotNone(connect, "Object id=button-ros-master-connect")
123  connect.click()
124 
125  # wait for /sin topic
126  topic_text = ''
127  while topic_text == '':
128  time.sleep(1)
129  self.wait.until(EC.presence_of_element_located((By.ID, "topic-select")))
130  topic = self.find_element_by_id("topic-select")
131  self.assertIsNotNone(topic, "Object id=topic-select not found")
132  topic_text = topic.text
133  self.assertTrue(u'/sin' in topic_text)
134 
135  Select(topic).select_by_value('/sin')
136 
137  # select field
138  self.wait.until(EC.presence_of_element_located((By.ID, "field-accessor")))
139  field = self.find_element_by_id("field-accessor")
140  self.assertIsNotNone(field, "Object id=field-accessor not found")
141  field.clear()
142  field.send_keys('data')
143 
144  self.wait.until(EC.presence_of_element_located((By.ID, "topic-field-button")))
145  plot = self.find_element_by_id("topic-field-button")
146  self.assertIsNotNone(plot, "Object id=topic-field-button")
147  plot.click()
148 
149  # check topic type
150  self.wait.until(EC.presence_of_element_located((By.ID, "message-detail")))
151  topic_type = self.find_element_by_id("message-detail")
152  self.assertIsNotNone(topic_type, "Object id=message-detail")
153  # self.assertTrue(u'"data": "float64"' in topic_type.text) ## it sometimes fails
154 
155  # check plot is updated
156  self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "g.x")))
157  x_axis = self.find_element_by_css_selector("g.x")
158  self.assertIsNotNone(x_axis, "Object id=x_axis")
159  x_axis_value = x_axis.text
160  loop = 0
161  x_axis_value_updated = 0
162  while loop < 20:
163  loop = loop + 1
164  time.sleep(1)
165  x_axis = self.find_element_by_css_selector("g.x")
166  rospy.logwarn("check if tick updated {} < {} ({})".format(x_axis_value, x_axis.text, x_axis_value_updated))
167  if x_axis_value != x_axis.text:
168  x_axis_value_updated = x_axis_value_updated + 1
169  if x_axis_value_updated >= 4:
170  break
171  x_axis_value = x_axis.text
172 
173  self.assertNotEqual(x_axis_value, x_axis.text)
174 
175  def find_element_by_id(self, name):
176  if pkg_resources.parse_version(selenium_version) >= pkg_resources.parse_version("4.3.0"):
177  return self.browser.find_element(By.ID, name)
178  else:
179  return self.browser.find_element_by_id(name)
180 
182  if pkg_resources.parse_version(selenium_version) >= pkg_resources.parse_version("4.3.0"):
183  return self.browser.find_element(By.CSS_SELECTOR, name)
184  else:
185  return self.browser.find_element_by_css_selector(name)
186 
187 
188 if __name__ == '__main__':
189  try:
190  rostest.run('rwt_plot', CLASSNAME, TestRwtPlot, sys.argv)
191  except KeyboardInterrupt:
192  pass
193  print("{} exiting".format(CLASSNAME))
def find_element_by_css_selector(self, name)
def __init__(self, args)
def find_element_by_id(self, name)


rwt_plot
Author(s): Ryohei Ueda
autogenerated on Fri Jun 2 2023 02:53:35