Best Python code snippet using SeleniumBase
main.py
Source:main.py
1from numpy import Infinity2import helper_functions as helpers3import pandas as pd4import time5# take the time for benchmarking6start = time.time()7iter_times = []8# algorithm parameters9# if budget < len(start_tour) not all possibile changes will be tested10budget = 5011# the length the solution tour should have12target_length = 213# Choose between random or static start tour. Using the static tour makes results more comparable and is much faster.14# generating random start tours does not work without brute forcing yet. Using the static starting tours is recommended.15random_start_tour = False16# The tour to use:17# 0 = 9 ways and ~270m length18# 1 = 19 ways and ~310m length19# 2 = 41 ways and ~1.48km length.20tour_id = 121# If true -> go through algorithm step by step with more debugging information22verbose = False23# load data set (execute preprocessing.py previously)24ways = pd.read_json("data/preprocessed_ways.json")25# inital values and start solution/tour26num_of_iterations = 027if(random_start_tour):28 start_tour = helpers.getRandomTour(ways, n_of_ways=25, budget=500000)29else:30 start_tour = helpers.getStaticExampleTour(ways, tour_id=tour_id)31helpers.prepareOverpassPlotScriptForTour(start_tour, "plot-nodes-init-solution")32print(start_tour)33print("Initial tour length:")34print(str(helpers.getTourLengthDF(start_tour)) + " km")35start_tour.to_csv("data/start_tour.csv")36# at first, the tour used by the algorithm is the start solution37tour = start_tour38# remove duplicate index column created by pandas39tour = tour.drop(['index'], axis=1)40curr_diff = target_length-helpers.getTourLengthDF(start_tour)41prev_diff = Infinity # Infinity to make first improvement positive42improvement = prev_diff-curr_diff43no_improvement_counter = 044changes_made = False45reached_tour_end = False46# main algorithm loop. Find better solutions from initial solution until budget is reached47# Based on 2-opt but implementation differs in several regards:48# - OSM data is not a fully connected graph. So reconnecting the two removed edges (case 4 below) is very rarley possible.49# - Instead, 2 additional 2-opt moves are introduced (case 2 and 3 below) too increase the likelihood of finding an improvement50# - Additionally, the algorithm aims for a target length instead of minimizing the length51while num_of_iterations < budget:52 iter_start_time = time.time()53 print("------ITERATION START--------")54 print("Iteration: " + str(num_of_iterations))55 # iterate over every way in the tour56 for i, way_i in enumerate(tour.itertuples()):57 # nested loop to get second edge/way to be replaced for every "i"58 for k, way_k in enumerate(tour.itertuples()):59 # dont loop over ways with index smaller than outer index i60 if k <= i or k >= len(tour) - 1:61 continue62 #get alternative ways that improve the difference the most, later its checked if they are longer than the sum of ways they replace63 replacement_way_i = helpers.getAlternativeWay(way_i.start_node, way_k.start_node, way_i.length, tour, ways, curr_diff)64 replacement_way_k = helpers.getAlternativeWay(way_i.end_node, way_k.end_node, way_k.length, tour, ways, curr_diff, blacklist=replacement_way_i)65 # Now four possible cases can occur: (in standard 2-opt, only 1. and 4. are handled/needed)66 # 1. no replacement found at all -> do nothing and go to next iteration67 if(len(replacement_way_i)==0 and len(replacement_way_k)==0):68 continue69 # 2. a replacement was found only for edge/way i70 if(len(replacement_way_i)>0 and len(replacement_way_k)==0 and not helpers.isWayInTour(replacement_way_i, tour)):71 # check if replacing ways would decrease difference to target length, skip iteration if not72 if( abs(curr_diff-helpers.getTourLengthDF(tour[i:k])) > abs(curr_diff-helpers.getTourLengthDF(replacement_way_i)) ):73 # debugging output if verbose = True (see at the top)74 if(verbose):75 helpers.printDebugInformation(tour, i, k, way_i, way_k, replacement_way_i, replacement_way_k, "Case 2")76 tour = tour.drop(tour.index[i:k]) #note: slicing is [inclusive:exclusive]77 first_section = tour.loc[:i].append(replacement_way_i, ignore_index=True)78 second_section = tour.loc[k:]79 tour = pd.concat([first_section, second_section]).reset_index(drop=True)80 print(tour)81 # indicate that a change was made, used to restart outer for loop to keep loop and dataframe indices in sync82 changes_made = True83 # 3. a replacement was found only for edge/way k84 if(len(replacement_way_i)==0 and len(replacement_way_k)>0 and not helpers.isWayInTour(replacement_way_k, tour)):85 # check if replacing ways would decrease difference to target length, skip iteration if not86 if( abs(curr_diff-helpers.getTourLengthDF(tour[(i+1):(k+1)])) > abs(curr_diff-helpers.getTourLengthDF(replacement_way_k)) ):87 # debugging output if verbose = True (see at the top)88 if(verbose):89 helpers.printDebugInformation(tour, i, k, way_i, way_k, replacement_way_i, replacement_way_k, "Case 3") 90 tour = tour.drop(tour.index[(i+1):(k+1)]) #note: slicing is [inclusive:exclusive]91 first_section = tour.loc[:i+1].append(replacement_way_k, ignore_index=True)92 second_section = tour.loc[k+1:]93 tour = pd.concat([first_section, second_section]).reset_index(drop=True)94 print(tour)95 # indicate that a change was made, used to restart outer for loop to keep loop and dataframe indices in sync96 changes_made = True97 98 # 4. a replacement was found for both, i and k, reconnecting their start/end nodes (standard 2-opt case)99 if(len(replacement_way_i)>0 and len(replacement_way_k)>0 and not helpers.isWayInTour(replacement_way_i, tour) and not helpers.isWayInTour(replacement_way_k, tour)):100 # check if replacing ways would decrease difference to target length, skip iteration if not101 if( abs(curr_diff-helpers.getTourLengthDF(tour[i])-helpers.getTourLengthDF(tour[k])) > abs(curr_diff-helpers.getTourLengthDF(replacement_way_k)) ):102 # debugging output if verbose = True (see at the top)103 if(verbose):104 helpers.printDebugInformation(tour, i, k, way_i, way_k, replacement_way_i, replacement_way_k, "Case 4")105 # add both replacements to tour and remove previous edges/ways i and k106 tour = tour.drop([i])107 tour = tour.drop([k])108 # first part of tour before i, unchanged, with new replacement for i109 first_section = tour.loc[:i].append(replacement_way_i, ignore_index=True) 110 # all edges/ways between i and k in reversed order, appended k at the end111 middle_section = tour.loc[i+1:k]112 middle_section = tour.loc[::-1].append(replacement_way_k, ignore_index=True) 113 # last part of tour after k, unchanged114 last_section = tour.loc[k+1:] 115 tour = pd.concat([116 first_section,117 middle_section,118 last_section 119 ], axis=0).reset_index(drop=True)120 # indicate that a change was made, used to restart outer for loop to keep loop and dataframe indices in sync121 changes_made = True122 123 # break the i loop and restart from the beginning if the tour was changed 124 # this resets the indices after a change to the itertuples125 if changes_made:126 break127 elif i==len(tour)-1:128 reached_tour_end = True129 # meassure time of iteration130 iter_end_time = time.time()131 iter_times.append(iter_end_time-iter_start_time)132 # count the number of times no improvement was made133 # break the loop if it matches the tour length (everything was checked without improvement)134 if(improvement==0):135 no_improvement_counter = no_improvement_counter + 1136 if(no_improvement_counter>=len(tour) or reached_tour_end):137 reached_tour_end = True138 break139 140 # set differences for stopping criterion and increase budget counter141 prev_diff = curr_diff142 curr_diff = target_length-helpers.getTourLengthDF(tour)143 improvement = prev_diff-curr_diff144 num_of_iterations = num_of_iterations + 1145 changes_made = False146 print("-------ITERATION END-------")147 print("Iteration: " + str(num_of_iterations-1))148 print("Improvement: " + str(improvement))149 print("Difference to target: " + str(curr_diff))150print("---------------------------TERMINATED------------------------------------")151if(num_of_iterations==budget):152 print("Budget reached. Try increasing the budget to find a better solution.")153if(reached_tour_end):154 print("All possibilities compared. No further improvements can be made based on the given starting tour.")155print("Result tour:")156print(tour)157print("Target length: "+str(target_length)+" km")158print("Result length: "+str(helpers.getTourLengthDF(tour))+" km")159end = time.time()160helpers.prepareOverpassPlotScriptForTour(tour, "plot-nodes-algo-output")161print("Total run-time: " + str(end-start) +"s")...
test_survey_ui.py
Source:test_survey_ui.py
...3@odoo.tests.common.tagged('post_install','-at_install')4class TestUi(odoo.tests.HttpCase):5 def test_01_admin_survey_tour(self):6 access_token = self.env.ref('survey.survey_feedback').access_token7 self.start_tour("/survey/start/%s" % access_token, 'test_survey', login="admin")8 def test_02_demo_survey_tour(self):9 access_token = self.env.ref('survey.survey_feedback').access_token10 self.start_tour("/survey/start/%s" % access_token, 'test_survey', login="demo")11 def test_03_public_survey_tour(self):12 access_token = self.env.ref('survey.survey_feedback').access_token13 self.start_tour("/survey/start/%s" % access_token, 'test_survey')14 def test_04_certification_success_tour(self):15 access_token = self.env.ref('survey.vendor_certification').access_token16 self.start_tour("/survey/start/%s" % access_token, 'test_certification_success', login="demo")17 def test_05_certification_failure_tour(self):18 access_token = self.env.ref('survey.vendor_certification').access_token19 self.start_tour("/survey/start/%s" % access_token, 'test_certification_failure', login="demo")20 def test_06_survey_prefill(self):21 access_token = self.env.ref('survey.survey_feedback').access_token...
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!!