Best Python code snippet using playwright-python
testroute.py
Source:testroute.py
...20 """21 rule = '/' + self.rand_str(20)22 # Add route23 @self.app.route(rule)24 def handle_route():25 TestRoute.cache['timeline'].append('handle_route')26 return ''27 # Call route28 with self.app.test_client() as c:29 c.get(rule)30 self.assert_equal(1, len(TestRoute.cache['timeline']))31 self.assert_in('handle_route', TestRoute.cache['timeline'])32 self.assert_equal(0, TestRoute.cache['timeline'].index('handle_route'))33 def test_original_routing_with_parameter(self):34 """35 Test original routing with parameter36 """37 rule = '/' + self.rand_str(20)38 rule_with_param = rule + '/<param>'39 param = 'myparam'40 # Add route41 @self.app.route(rule_with_param)42 def handle_route(param=None):43 TestRoute.cache['timeline'].append('handle_route')44 TestRoute.cache['param'] = param45 return ''46 # Call route47 with self.app.test_client() as c:48 c.get(rule + '/' + param)49 self.assert_equal(1, len(TestRoute.cache['timeline']))50 self.assert_in('handle_route', TestRoute.cache['timeline'])51 self.assert_equal(0, TestRoute.cache['timeline'].index('handle_route'))52 self.assert_in('param', TestRoute.cache)53 self.assert_equal(param, TestRoute.cache['param'])54 def test_new_routing(self):55 """56 Test new routing57 """58 rule = '/' + self.rand_str(20)59 # Add route60 self.app.route(rule, uses=(MyController, 'get'))61 # Call route62 with self.app.test_client() as c:63 c.get(rule)64 self.assert_equal(1, len(TestRoute.cache['timeline']))65 self.assert_in('handle_route', TestRoute.cache['timeline'])66 self.assert_equal(0, TestRoute.cache['timeline'].index('handle_route'))67 def test_new_routing_with_parameter(self):68 """69 Test new routing with parameter70 """71 rule = '/' + self.rand_str(20)72 rule_with_param = rule + '/<param>'73 param = 'myparam'74 # Add route75 self.app.route(rule_with_param, uses=(MyController, 'get_with_param'))76 # Call route77 with self.app.test_client() as c:78 c.get(rule + '/' + param)79 self.assert_equal(1, len(TestRoute.cache['timeline']))80 self.assert_in('handle_route', TestRoute.cache['timeline'])81 self.assert_equal(0, TestRoute.cache['timeline'].index('handle_route'))82 self.assert_in('param', TestRoute.cache)83 self.assert_equal(param, TestRoute.cache['param'])84 def test_initialize(self):85 """86 Test initialize87 """88 rule = '/' + self.rand_str(20)89 # Add route90 self.app.route(rule, uses=(MyController, 'get'))91 # Call route92 with self.app.test_client() as c:93 c.get(rule)94 self.assert_in('init_params', TestRoute.cache)95 self.assert_equal(0, len(TestRoute.cache['init_params']))96 def test_initialize_with_parameter(self):97 """98 Test initialize with parameter99 """100 rule = '/' + self.rand_str(20)101 rule_with_param = rule + '/<param>'102 param = 'myparam'103 # Add route104 self.app.route(rule_with_param, uses=(MyController, 'get_with_param'))105 # Call route106 with self.app.test_client() as c:107 c.get(rule + '/' + param)108 self.assert_in('init_params', TestRoute.cache)109 self.assert_equal(1, len(TestRoute.cache['init_params']))110 self.assert_in('param', TestRoute.cache['init_params'])111 self.assert_equal(param, TestRoute.cache['init_params']['param'])112 def test_faulty_routing(self):113 """114 Test faulty routing115 """116 rule = '/' + self.rand_str(20)117 # Add route with both uses and handler118 with self.assert_raises_regexp(TypeError, "'Route' object is not callable"):119 @self.app.route(rule, uses=(MyController, 'get'))120 def handle_route():121 pass122 def test_middleware_no_middleware(self):123 """124 Test route with no middleware125 """126 rule = '/' + self.rand_str(20)127 # Add route128 @self.app.route(rule)129 def handle_route():130 TestRoute.cache['timeline'].append('handle_route')131 return ''132 # Call route133 with self.app.test_client() as c:134 c.get(rule)135 self.assert_equal(1, len(TestRoute.cache['timeline']))136 self.assert_in('handle_route', TestRoute.cache['timeline'])137 self.assert_equal(0, TestRoute.cache['timeline'].index('handle_route'))138 def test_middleware_registering(self):139 """140 Test registering the request middleware141 """142 rule = '/' + self.rand_str(20)143 rule2 = '/' + self.rand_str(20)144 self.assert_not_equal(rule, rule2)145 # Add route146 @self.app.route(rule, middleware=[MyRequestMiddleware])147 def handle_route():148 TestRoute.cache['timeline'].append('handle_route')149 return ''150 # Call route151 with self.app.test_request_context(rule):152 self.app.preprocess_request()153 rv = self.app.dispatch_request()154 response = self.app.make_response(rv)155 response = self.app.process_response(response)156 self.assert_equal(3, len(TestRoute.cache['timeline']))157 self.assert_in(MyRequestMiddleware.__name__ + '.before', TestRoute.cache['timeline'])158 self.assert_equal(0, TestRoute.cache['timeline'].index(MyRequestMiddleware.__name__ + '.before'))159 self.assert_in('handle_route', TestRoute.cache['timeline'])160 self.assert_equal(1, TestRoute.cache['timeline'].index('handle_route'))161 self.assert_in(MyRequestMiddleware.__name__ + '.after', TestRoute.cache['timeline'])162 self.assert_equal(2, TestRoute.cache['timeline'].index(MyRequestMiddleware.__name__ + '.after'))163 # Add second route164 @self.app.route(rule2, middleware=[MyRequestMiddleware, (MySecondRequestMiddleware, 'arg1')])165 def handleecond_route():166 TestRoute.cache['timeline'].append('handle_route')167 return ''168 # Call route169 TestRoute.cache = dict()170 TestRoute.cache['timeline'] = []171 with self.app.test_request_context(rule2):172 self.app.preprocess_request()173 rv = self.app.dispatch_request()174 response = self.app.make_response(rv)175 response = self.app.process_response(response)176 self.assert_equal(5, len(TestRoute.cache['timeline']))177 self.assert_in(MyRequestMiddleware.__name__ + '.before', TestRoute.cache['timeline'])178 self.assert_equal(0, TestRoute.cache['timeline'].index(MyRequestMiddleware.__name__ + '.before'))179 self.assert_in(MySecondRequestMiddleware.__name__ + '.before', TestRoute.cache['timeline'])180 self.assert_equal(1, TestRoute.cache['timeline'].index(MySecondRequestMiddleware.__name__ + '.before'))181 self.assert_in('handle_route', TestRoute.cache['timeline'])182 self.assert_equal(2, TestRoute.cache['timeline'].index('handle_route'))183 self.assert_in(MySecondRequestMiddleware.__name__ + '.after', TestRoute.cache['timeline'])184 self.assert_equal(3, TestRoute.cache['timeline'].index(MySecondRequestMiddleware.__name__ + '.after'))185 self.assert_in(MyRequestMiddleware.__name__ + '.after', TestRoute.cache['timeline'])186 self.assert_equal(4, TestRoute.cache['timeline'].index(MyRequestMiddleware.__name__ + '.after'))187 def test_middleware_overwriting(self):188 """189 Test overwriting of middleware190 """191 rule = '/' + self.rand_str(20)192 rule2 = '/' + self.rand_str(20)193 # Add route194 @self.app.route(rule, middleware=[MyRequestMiddleware])195 def handle_route():196 pass197 # Overwrite route198 @self.app.route(rule2, middleware=[MyRequestMiddleware, (MySecondRequestMiddleware, 'arg1')])199 def handleOverwrittenRoute():200 pass201 def test_middleware_before_returning_none_null(self):202 """203 Test before returning none null204 :return: void205 """206 rule = '/' + self.rand_str(20)207 # Overwrite route208 @self.app.route(rule, middleware=[(MyThirdRequestMiddleware, 'arg1'), MyRequestMiddleware])209 def handle_route():210 TestRoute.cache['timeline'].append('handle_route')211 return ''212 # Call route213 with self.app.test_request_context(rule):214 self.app.preprocess_request()215 rv = self.app.dispatch_request()216 response = self.app.make_response(rv)217 self.app.process_response(response)218 self.assert_equal(1, len(TestRoute.cache['timeline']))219 self.assert_equal(MyThirdRequestMiddleware.__name__ + '.before', TestRoute.cache['timeline'][0])220 def test_middleware_with_uses(self):221 """222 Test middleware with uses223 :return: void...
testhandler.py
Source:testhandler.py
...31 ])32 app = self.create_application()33 # Add route34 @app.route(rule)35 def handle_route():36 TestHandler.cache['timeline'].append('handle_route')37 abort(abort_exception)38 return 'handled route'39 # Check current handler40 self.assert_equal(MyHandler, app.config('app.exceptions.handler', None))41 # Call route42 with app.test_client() as c:43 # Loop http exceptions44 for http_exception in default_exceptions.values():45 abort_exception = http_exception.code46 TestHandler.cache = dict()47 TestHandler.cache['timeline'] = []48 rv = c.get(rule)49 # THIS IS NOT OK! See https://github.com/pallets/werkzeug/issues/123150 # Waiting for fix to be released.51 if http_exception.code != 412:52 self.assert_equal('rendered', rv.get_data(True))53 self.assert_equal(http_exception.code, rv.status_code)54 self.assert_equal(4, len(TestHandler.cache['timeline']))55 self.assert_in('handle_route', TestHandler.cache['timeline'])56 self.assert_equal(0, TestHandler.cache['timeline'].index('handle_route'))57 self.assert_in(MyHandler.__name__ + '.report', TestHandler.cache['timeline'])58 self.assert_equal(1, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.report'))59 self.assert_in(MyHandler.__name__ + '.render', TestHandler.cache['timeline'])60 self.assert_equal(2, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.render'))61 self.assert_true(isinstance(TestHandler.cache['timeline'][3], HTTPException))62 self.assert_equal(http_exception.code, TestHandler.cache['timeline'][3].code)63 def test_404(self):64 """65 Test 40466 """67 rule = '/' + self.rand_str(20)68 self.write_config([69 "from tests.exceptions.testhandler import MyHandler \n",70 "APP = { \n",71 " 'debug': False, \n",72 " 'exceptions': { \n",73 " 'handler': MyHandler \n",74 " } \n",75 "} \n",76 ])77 app = self.create_application()78 # Check current handler79 self.assert_equal(MyHandler, app.config('app.exceptions.handler', None))80 # Call route81 with app.test_client() as c:82 rv = c.get(rule)83 self.assert_equal('rendered', rv.get_data(True))84 self.assert_equal(404, rv.status_code)85 self.assert_equal(3, len(TestHandler.cache['timeline']))86 self.assert_in(MyHandler.__name__ + '.report', TestHandler.cache['timeline'])87 self.assert_equal(0, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.report'))88 self.assert_in(MyHandler.__name__ + '.render', TestHandler.cache['timeline'])89 self.assert_equal(1, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.render'))90 self.assert_true(isinstance(TestHandler.cache['timeline'][2], HTTPException))91 self.assert_equal(404, TestHandler.cache['timeline'][2].code)92 def test_exception(self):93 """94 Test generic exception95 """96 rule = '/' + self.rand_str(20)97 self.write_config([98 "from tests.exceptions.testhandler import MyHandler \n",99 "APP = { \n",100 " 'debug': False, \n",101 " 'exceptions': { \n",102 " 'handler': MyHandler \n",103 " } \n",104 "} \n",105 ])106 app = self.create_application()107 # Add route108 @app.route(rule)109 def handle_route():110 TestHandler.cache['timeline'].append('handle_route')111 raise RuntimeError('MyRuntimeError')112 # Check current handler113 self.assert_equal(MyHandler, app.config('app.exceptions.handler', None))114 # Call route115 with app.test_client() as c:116 rv = c.get(rule)117 self.assert_equal('rendered', rv.get_data(True))118 self.assert_equal(500, rv.status_code)119 self.assert_equal(4, len(TestHandler.cache['timeline']))120 self.assert_in('handle_route', TestHandler.cache['timeline'])121 self.assert_equal(0, TestHandler.cache['timeline'].index('handle_route'))122 self.assert_in(MyHandler.__name__ + '.report', TestHandler.cache['timeline'])123 self.assert_equal(1, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.report'))124 self.assert_in(MyHandler.__name__ + '.render', TestHandler.cache['timeline'])125 self.assert_equal(2, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.render'))126 self.assert_true(isinstance(TestHandler.cache['timeline'][3], RuntimeError))127 self.assert_equal('MyRuntimeError', '%s' % TestHandler.cache['timeline'][3])128 def test_dont_report(self):129 """130 Test dont_report131 :return: void132 """133 rule1 = '/' + self.rand_str(20)134 rule2 = '/' + self.rand_str(20)135 self.write_config([136 "from tests.exceptions.testhandler import MyHandler \n",137 "APP = { \n",138 " 'debug': False, \n",139 " 'exceptions': { \n",140 " 'handler': MyHandler \n",141 " } \n",142 "} \n",143 ])144 app = self.create_application()145 # Add route146 @app.route(rule1)147 def handle_route1():148 TestHandler.cache['timeline'].append('handle_route1')149 raise SystemError()150 @app.route(rule2)151 def handle_route2():152 TestHandler.cache['timeline'].append('handle_route2')153 raise OSError()154 # Call route155 with app.test_client() as c:156 c.get(rule1)157 with app.test_client() as c:158 c.get(rule2)159 self.assert_equal(7, len(TestHandler.cache['timeline']))160 self.assert_equal(0, TestHandler.cache['timeline'].index('handle_route1'))161 self.assert_equal('handle_route1', TestHandler.cache['timeline'][0])162 self.assert_equal(MyHandler.__name__ + '.report', TestHandler.cache['timeline'][1])163 self.assert_equal(MyHandler.__name__ + '.render', TestHandler.cache['timeline'][2])164 self.assert_true(isinstance(TestHandler.cache['timeline'][3], SystemError))165 self.assert_equal('handle_route2', TestHandler.cache['timeline'][4])166 self.assert_equal(MyHandler.__name__ + '.render', TestHandler.cache['timeline'][5])167 self.assert_true(isinstance(TestHandler.cache['timeline'][6], OSError))168 def test_raise_exception_debug(self):169 """170 Test raise exception in debug environment171 :return: void172 """173 rule = '/' + self.rand_str(20)174 self.write_config([175 "from tests.exceptions.testhandler import MyHandler \n",176 "APP = { \n",177 " 'debug': True, \n",178 " 'exceptions': { \n",179 " 'handler': MyHandler \n",180 " } \n",181 "} \n",182 ])183 app = self.create_application()184 # Add route185 @app.route(rule)186 def handle_route():187 raise RuntimeError(rule)188 # Call route189 with self.assert_raises_regexp(RuntimeError, rule):190 with app.test_client() as c:191 c.get(rule)192class MyHandler(EdmundsHandler):193 """194 Exception Handler class195 """196 dont_report = [197 OSError198 ]199 def report(self, exception):200 """...
testprofilermiddleware.py
Source:testprofilermiddleware.py
...33 stream = app.config('app.profiler.instances')[0]['stream']34 # Add route35 rule = '/' + self.rand_str(20)36 @app.route(rule)37 def handle_route():38 return ''39 with app.test_client() as c:40 # Check stream41 self.assert_equal('', stream.getvalue())42 # Call route43 c.get(rule)44 # Check stream45 self.assert_equal('', stream.getvalue())46 def test_app_no_debug(self):47 """48 Test app no debug49 """50 # Write config51 self.write_config([52 "from edmunds.profiler.drivers.stream import Stream \n",53 "try: \n",54 " from cStringIO import StringIO \n",55 "except ImportError: \n",56 " from io import StringIO \n",57 "APP = { \n",58 " 'debug': False, \n",59 " 'profiler': { \n",60 " 'enabled': True, \n",61 " 'instances': [ \n",62 " { \n",63 " 'name': 'stream',\n",64 " 'driver': Stream,\n",65 " 'stream': StringIO(),\n",66 " }, \n",67 " ], \n",68 " }, \n",69 "} \n",70 ])71 # Create app and fetch stream72 app = self.create_application()73 stream = app.config('app.profiler.instances')[0]['stream']74 # Add route75 rule = '/' + self.rand_str(20)76 @app.route(rule)77 def handle_route():78 return ''79 with app.test_client() as c:80 # Check stream81 self.assert_equal('', stream.getvalue())82 # Call route83 c.get(rule)84 # Check stream85 self.assert_equal('', stream.getvalue())86 def test_app_no_debug_profiler_disabled(self):87 """88 Test app no debug and profiler disabled89 """90 # Write config91 self.write_config([92 "from edmunds.profiler.drivers.stream import Stream \n",93 "try: \n",94 " from cStringIO import StringIO \n",95 "except ImportError: \n",96 " from io import StringIO \n",97 "APP = { \n",98 " 'debug': False, \n",99 " 'profiler': { \n",100 " 'enabled': False, \n",101 " 'instances': [ \n",102 " { \n",103 " 'name': 'stream',\n",104 " 'driver': Stream,\n",105 " 'stream': StringIO(),\n",106 " }, \n",107 " ], \n",108 " }, \n",109 "} \n",110 ])111 # Create app and fetch stream112 app = self.create_application()113 stream = app.config('app.profiler.instances')[0]['stream']114 # Add route115 rule = '/' + self.rand_str(20)116 @app.route(rule)117 def handle_route():118 return ''119 with app.test_client() as c:120 # Check stream121 self.assert_equal('', stream.getvalue())122 # Call route123 c.get(rule)124 # Check stream125 self.assert_equal('', stream.getvalue())126 def test_enabled(self):127 """128 Test enabled129 """130 # Write config131 self.write_config([132 "from edmunds.profiler.drivers.stream import Stream \n",133 "try: \n",134 " from cStringIO import StringIO \n",135 "except ImportError: \n",136 " from io import StringIO \n",137 "APP = { \n",138 " 'debug': True, \n",139 " 'profiler': { \n",140 " 'enabled': True, \n",141 " 'instances': [ \n",142 " { \n",143 " 'name': 'stream',\n",144 " 'driver': Stream,\n",145 " 'stream': StringIO(),\n",146 " }, \n",147 " ], \n",148 " }, \n",149 "} \n",150 ])151 # Create app and fetch stream152 app = self.create_application()153 stream = app.config('app.profiler.instances')[0]['stream']154 # Add route155 rule = '/' + self.rand_str(20)156 @app.route(rule)157 def handle_route():158 return ''159 with app.test_client() as c:160 # Check stream161 self.assert_equal('', stream.getvalue())162 # Call route163 c.get(rule)164 # Check stream165 self.assert_not_equal('', stream.getvalue())166 def test_multiple_profilers(self):167 """168 Test multiple profilers169 """170 # Write config171 self.write_config([172 "from edmunds.profiler.drivers.stream import Stream \n",173 "try: \n",174 " from cStringIO import StringIO \n",175 "except ImportError: \n",176 " from io import StringIO \n",177 "APP = { \n",178 " 'debug': True, \n",179 " 'profiler': { \n",180 " 'enabled': True, \n",181 " 'instances': [ \n",182 " { \n",183 " 'name': 'stream',\n",184 " 'driver': Stream,\n",185 " 'stream': StringIO(),\n",186 " }, \n",187 " { \n",188 " 'name': 'stream2',\n",189 " 'driver': Stream,\n",190 " 'stream': StringIO(),\n",191 " }, \n",192 " ], \n",193 " }, \n",194 "} \n",195 ])196 # Create app and fetch stream197 app = self.create_application()198 stream = app.config('app.profiler.instances')[0]['stream']199 stream2 = app.config('app.profiler.instances')[1]['stream']200 # Add route201 rule = '/' + self.rand_str(20)202 @app.route(rule)203 def handle_route():204 return ''205 with app.test_client() as c:206 # Check stream207 self.assert_equal('', stream.getvalue())208 self.assert_equal('', stream2.getvalue())209 self.assert_equal(stream.getvalue(), stream2.getvalue())210 # Call route211 c.get(rule)212 # Check stream213 self.assert_not_equal('', stream.getvalue())214 self.assert_not_equal('', stream2.getvalue())...
testmiddleware.py
Source:testmiddleware.py
...48 self.app.middleware(MyApplicationMiddleware)49 # Add route50 rule = '/' + self.rand_str(20)51 @self.app.route(rule)52 def handle_route():53 TestMiddleware.cache['timeline'].append('handle_route')54 return ''55 # Call route56 with self.app.test_client() as c:57 rv = c.get(rule)58 self.assert_equal(2, len(TestMiddleware.cache['timeline']))59 self.assert_in(MyApplicationMiddleware.__name__, TestMiddleware.cache['timeline'])60 self.assert_equal(0, TestMiddleware.cache['timeline'].index(MyApplicationMiddleware.__name__))61 self.assert_in('handle_route', TestMiddleware.cache['timeline'])62 self.assert_equal(1, TestMiddleware.cache['timeline'].index('handle_route'))63 # Add second middleware64 self.app.middleware(MySecondApplicationMiddleware)65 # Call route66 TestMiddleware.cache = dict()...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!