How to use intercept method of Selenium.WebDriver.DriverExtensions.HasNetworkInterception Package

Best Selenium code snippet using Selenium.WebDriver.DriverExtensions.HasNetworkInterception.intercept


Source:has_network_interception.rb Github


Full Screen

...24 # to either pass them through like proxy or provide25 # a stubbed response instead.26 #27 # @example Log requests and pass through28 # driver.intercept do |request, &continue|29 # puts "#{request.method} #{request.url}"30 # # end32 #33 # @example Stub requests for images34 # driver.intercept do |request, &continue|35 # if request.url.match?(/\.png$/)36 # request.url = ''37 # end38 # # end40 #41 # @example Log responses and pass through42 # driver.intercept do |request, &continue|43 # do |response|44 # puts "#{response.code} #{response.body}"45 # end46 # end47 #48 # @example Mutate specific response49 # driver.intercept do |request, &continue|50 # do |response|51 # response.body << 'Added by Selenium!' if request.url.include?('/myurl')52 # end53 # end54 #55 # @param [Proc] block which is called when request is intercepted56 # @yieldparam [DevTools::Request] request57 # @yieldparam [Proc] continue block which proceeds with the request and optionally yields response58 #59 def intercept(&block)60 true)61 devtools.fetch.on(:request_paused) do |params|62 id = params['requestId']63 if params.key?('responseStatusCode') || params.key?('responseErrorReason')64 intercept_response(id, params, &pending_response_requests.delete(id))65 else66 intercept_request(id, params, &block)67 end68 end69 devtools.fetch.enable(patterns: [{requestStage: 'Request'}, {requestStage: 'Response'}])70 end71 private72 def pending_response_requests73 @pending_response_requests ||= {}74 end75 def intercept_request(id, params, &block)76 original = DevTools::Request.from(id, params)77 mutable = DevTools::Request.from(id, params)78 do |&continue| # rubocop:disable Performance/RedundantBlockCall79 pending_response_requests[id] = continue80 if original == mutable81 devtools.fetch.continue_request(request_id: id)82 else83 devtools.fetch.continue_request(84 request_id: id,85 url: mutable.url,86 method: mutable.method,87 post_data: mutable.post_data,88 headers: do |k, v|89 {name: k, value: v}90 end91 )92 end93 end94 end95 def intercept_response(id, params)96 return devtools.fetch.continue_request(request_id: id) unless block_given?97 body = fetch_response_body(id)98 original = DevTools::Response.from(id, body, params)99 mutable = DevTools::Response.from(id, body, params)100 yield mutable101 if original == mutable102 devtools.fetch.continue_request(request_id: id)103 else104 devtools.fetch.fulfill_request(105 request_id: id,106 body: (Base64.strict_encode64(mutable.body) if mutable.body),107 response_code: mutable.code,108 response_headers: do |k, v|109 {name: k, value: v}...

Full Screen

Full Screen


Using AI Code Generation


Full Screen

1 request.respond_with(200, 'text/plain', 'Hello World')2 request.respond_with(200, 'text/plain', 'Hello World')3 request.respond_with(200, 'text/plain', 'Hello World')4 request.respond_with(200, 'text/plain', 'Hello World')5 request.respond_with(200

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?