Best Python code snippet using robotframework-pageobjects_python
test_system_node.py
Source:test_system_node.py
1#! /usr/bin/env python32# Copyright 2018 Intel Corporation.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15import math16import sys17import time18from geometry_msgs.msg import Pose19from geometry_msgs.msg import PoseStamped20from geometry_msgs.msg import PoseWithCovarianceStamped21import rclpy22from rclpy.node import Node23class NavTester(Node):24 def __init__(self):25 super().__init__('nav2_tester')26 self.initial_pose_pub = self.create_publisher(PoseWithCovarianceStamped,27 'initialpose')28 self.goal_pub = self.create_publisher(PoseStamped, 'move_base_simple/goal')29 self.model_pose_sub = self.create_subscription(PoseWithCovarianceStamped,30 '/amcl_pose', self.poseCallback)31 self.initial_pose_received = False32 def setInitialPose(self, pose):33 self.initial_pose = pose34 self.currentPose = pose35 self.setNavstackInitialPose(pose)36 def setNavstackInitialPose(self, pose):37 msg = PoseWithCovarianceStamped()38 msg.pose.pose = pose39 msg.header.frame_id = 'map'40 self.get_logger().info('Publishing Initial Pose')41 self.initial_pose_pub.publish(msg)42 def setGoalPose(self, pose):43 self.goal_pose = pose44 msg = PoseStamped()45 msg.header.frame_id = 'map'46 msg.pose = pose47 self.goal_pub.publish(msg)48 def poseCallback(self, msg):49 self.get_logger().info('Received amcl_pose')50 self.current_pose = msg.pose.pose51 self.initial_pose_received = True52 def reachesGoal(self, timeout):53 goalReached = False54 start_time = time.time()55 while not goalReached:56 rclpy.spin_once(self, timeout_sec=1)57 if self.distanceFromGoal() < 0.50: # get within 50cm of goal58 goalReached = True59 self.get_logger().info('*** GOAL REACHED ***')60 return True61 elif timeout is not None:62 if (time.time() - start_time) > timeout:63 self.get_logger().error('Robot timed out reaching its goal!')64 return False65 def distanceFromGoal(self):66 d_x = self.current_pose.position.x - self.goal_pose.position.x67 d_y = self.current_pose.position.y - self.goal_pose.position.y68 distance = math.sqrt(d_x * d_x + d_y * d_y)69 self.get_logger().info('Distance from goal is: ' + str(distance))70 return distance71 def setSimTime(self):72 self.get_logger().info('Setting transforms to use sim time from gazebo')73 from subprocess import call74 # loop through the problematic nodes75 for nav2_node in ('/static_transform_publisher', '/map_server',76 '/global_costmap/global_costmap', '/local_costmap/local_costmap'):77 while (call(['ros2', 'param', 'set', nav2_node, 'use_sim_time', 'True'])):78 self.get_logger().error("Error couldn't set use_sim_time param on: " +79 nav2_node + ' retrying...')80def test_InitialPose(test_robot, timeout):81 # Set initial pose to the Turtlebot3 starting position -2, 0, 0, facing towards positive X82 initial_pose = Pose()83 initial_pose.position.x = -2.084 initial_pose.position.y = -0.585 initial_pose.position.z = 0.0186 initial_pose.orientation.x = 0.087 initial_pose.orientation.y = 0.088 initial_pose.orientation.z = 0.089 initial_pose.orientation.w = 1.090 test_robot.get_logger().info('Setting initial pose')91 test_robot.initial_pose_received = False92 test_robot.setInitialPose(initial_pose)93 quit_time = time.time() + timeout94 test_robot.get_logger().info('Waiting for initial pose to be received')95 while not test_robot.initial_pose_received and time.time() < quit_time:96 rclpy.spin_once(test_robot) # wait for poseCallback97 if (test_robot.initial_pose_received):98 test_robot.get_logger().info('test_InitialPose PASSED')99 else:100 test_robot.get_logger().info('test_InitialPose FAILED')101 return test_robot.initial_pose_received102def test_RobotMovesToGoal(test_robot):103 goal_pose = Pose()104 goal_pose.position.x = 0.0105 goal_pose.position.y = 2.0106 goal_pose.position.z = 0.01107 goal_pose.orientation.x = 0.0108 goal_pose.orientation.y = 0.0109 goal_pose.orientation.z = 0.0110 goal_pose.orientation.w = 1.0111 test_robot.get_logger().info('Setting goal pose')112 test_robot.setGoalPose(goal_pose)113 test_robot.get_logger().info('Waiting 30 seconds for robot to reach goal')114 return test_robot.reachesGoal(timeout=30)115def test_all(test_robot):116 # set transforms to use_sim_time117 test_robot.setSimTime()118 result = True119 if (result):120 result = test_InitialPose(test_robot, 10)121 if (result):122 result = test_RobotMovesToGoal(test_robot)123 if (not result):124 # retry the test one more time125 test_robot.get_logger().info('Test failed, retrying...')126 result = test_RobotMovesToGoal(test_robot)127 # Add more tests here if desired128 return result129def main(argv=sys.argv[1:]):130 rclpy.init()131 test_robot = NavTester()132 test_robot.get_logger().info('Starting test_system_node')133 # wait a few seconds to make sure entire stack is up and running134 test_robot.get_logger().info('Waiting for a few seconds for all nodes to initialize')135 time.sleep(5)136 # run tests137 result = test_all(test_robot)138 if (result):139 test_robot.get_logger().info('Test PASSED')140 return141 else:142 test_robot.get_logger().error('Test FAILED')143 exit(1)144if __name__ == '__main__':...
test_robot.py
Source:test_robot.py
1from robot import ToyRobot2from ToyRobotError import ToyRobotError3import unittest4from face import Face5class TestRobot(unittest.TestCase):6 def setUp(self) -> None:7 self.test_robot = ToyRobot()8 def test_initialise(self):9 self.test_robot = ToyRobot()10 # Test that the robot doesn't have any coordinates11 self.assertIsNone(self.test_robot.get_x_value())12 self.assertIsNone(self.test_robot.get_y_value())13 # Test that the robot is not facing any direction14 self.assertIsNone(self.test_robot.get_face_value())15 def test_is_placed(self):16 self.test_robot = ToyRobot()17 # Test that the robot is not placed when created18 self.assertFalse(self.test_robot.is_placed())19 # Place the robot20 self.test_robot.place(0, 0, Face.NORTH)21 # Test that the robot is placed22 self.assertTrue(self.test_robot.is_placed())23 def test_place(self):24 # Initialise a robot25 self.test_robot = ToyRobot()26 # Place the robot with valid values27 self.test_robot.place(0, 0, Face.NORTH)28 # Test the coordinates and direction of the robot29 self.assertEqual(self.test_robot.get_x_value(), 0)30 self.assertEqual(self.test_robot.get_y_value(), 0)31 self.assertEqual(self.test_robot.get_face_value(), Face.NORTH)32 # Test negative values for position raises ValueError33 with self.assertRaises(ToyRobotError):34 self.test_robot.place(-1, 0, Face.NORTH)35 with self.assertRaises(ToyRobotError):36 self.test_robot.place(0, -1, Face.NORTH)37 # Test non-enum values of Face raises TypeError38 with self.assertRaises(ToyRobotError):39 self.test_robot.place(0, 0, "NORTH")40 def test_left(self):41 # Initialise a robot42 self.test_robot = ToyRobot()43 # Place the robot44 self.test_robot.place(0, 0, Face.SOUTH)45 # Test left movements46 self.test_robot.left()47 self.assertEqual(self.test_robot.get_face_value(), Face.EAST)48 self.test_robot.left()49 self.assertEqual(self.test_robot.get_face_value(), Face.NORTH)50 self.test_robot.left()51 self.assertEqual(self.test_robot.get_face_value(), Face.WEST)52 self.test_robot.left()53 self.assertEqual(self.test_robot.get_face_value(), Face.SOUTH)54 def test_right(self):55 # Initialise a robot56 self.test_robot = ToyRobot()57 # Place the robot58 self.test_robot.place(0, 0, Face.SOUTH)59 # Test right movements60 self.test_robot.right()61 self.assertEqual(self.test_robot.get_face_value(), Face.WEST)62 self.test_robot.right()63 self.assertEqual(self.test_robot.get_face_value(), Face.NORTH)64 self.test_robot.right()65 self.assertEqual(self.test_robot.get_face_value(), Face.EAST)66 self.test_robot.right()67 self.assertEqual(self.test_robot.get_face_value(), Face.SOUTH)68 def test_move(self):69 # Initialise a robot70 self.test_robot = ToyRobot()71 # Place the robot72 self.test_robot.place(0, 0, Face.NORTH)73 # Move the robot one step north and check y coordinate74 self.test_robot.move()75 self.assertEqual(self.test_robot.get_y_value(), 1)76 # Turn right, move one step and check x coordinate77 self.test_robot.right()78 self.test_robot.move()79 self.assertEqual(self.test_robot.get_x_value(), 1)80 # Place robot and check movement backwards on grid81 self.test_robot.place(1, 1, Face.NORTH)82 self.test_robot.left()83 self.test_robot.move()84 self.assertEqual(self.test_robot.get_x_value(), 0)85 self.test_robot.place(1, 1, Face.WEST)86 self.test_robot.left()87 self.test_robot.move()88 self.assertEqual(self.test_robot.get_y_value(), 0)89 def test_str(self):90 # Initialise and place the robot91 self.test_robot = ToyRobot()92 self.test_robot.place(0, 0, Face.NORTH)93 # Check if the str returns the correct string94 self.assertEqual(str(self.test_robot), "0,0,NORTH")95 # Rotate right and test the correct string96 self.test_robot.right()97 self.assertEqual(str(self.test_robot), "0,0,EAST")98 # Move and test the correct string99 self.test_robot.move()100 self.assertEqual(str(self.test_robot), "1,0,EAST")101if __name__ == '__main__':...
main.py
Source:main.py
...5from Utilities import *6from Experiments import *7if __name__ == "__main__":8 # Part 49 # test_robot(BreadthFirstSearchRobot, [0, 1, 2, 3, 4, 5])10 # Part 511 # test_robot(UniformCostSearchRobot, [0, 1, 2, 3, 4, 5])12 # Part 713 # test_robot(WAStartRobot, [0, 1, 2, 3, 4, 5], heuristic=tail_manhattan_heuristic)14 # Part 815 # test_robot(WAStartRobot, [99], heuristic=tail_manhattan_heuristic)16 # a = solve_and_display(WAStartRobot, 99, blit=True, heuristic=tail_manhattan_heuristic)17 # a = solve_and_display(WAStartRobot, 5, blit=True, heuristic=center_manhattan_heuristic)18 # b = solve_and_display(BreadthFirstSearchRobot, 3)19 for i in range(3):20 w_experiment(i)21 # w_values = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]22 # for w in w_values:23 # a = solve_and_display(WAStartRobot, 2, heuristic=center_manhattan_heuristic, w=w, blit=False)24 # for k in [2, 4, 6, 8]:25 # test_robot(WAStartRobot, [3, 4], heuristic=ShorterRobotHeuristic, k=k)26 # test_robot(WAStartRobot, [3, 4], heuristic=center_manhattan_heuristic)27 # test_robot(WAStartRobot, [0, 1, 2, 3, 4, 5], heuristic=center_manhattan_heuristic)28 # for k in [2, 4, 6, 8]:29 # test_robot(WAStartRobot, [0, 1, 2, 3, 4, 5], heuristic=ShorterRobotHeuristic, k=k)30 for i in range(2,6):31 shorter_robot_heuristic_experiment(i)32 # a = solve_and_display(WAStartRobot, 50, heuristic=center_manhattan_heuristic, w=0.5, blit=False)33 # b = solve_and_display(WAStartRobot, 50, heuristic=ShorterRobotHeuristic, k=2, blit=False)34 # a = solve_and_display(WAStartRobot, 95, heuristic=center_manhattan_heuristic, w=0.5, blit=False)35 # b = solve_and_display(WAStartRobot, 95, heuristic=ShorterRobotHeuristic, k=2, blit=False)36 # w_experiment(50)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!