How to use same browser window for automated test using selenium-webdriver (ruby)?

后端 未结 3 689
孤城傲影
孤城傲影 2020-12-18 13:46

I am automating test cases for a website using selenium-webdriver and cucumber in ruby. I need each feature to run in a particular order and using the same browser window. A

3条回答
  •  旧巷少年郎
    2020-12-18 14:33

    As the answers solve the problem but do not answer the question "How to connect to an existing session".

    I managed to do this with the following code since it is not officially supported.

    # monkey-patch 2 methods
    module Selenium
      module WebDriver
        class Driver
          # Be able to set the driver
          def set_bridge_to(b)
            @bridge = b
          end
    
          # bridge is a private method, simply create a public version
          def public_bridge
            @bridge
          end
        end
      end
    end
    
    
    caps = Selenium::WebDriver::Remote::Capabilities.send("chrome")
    
    driver = Selenium::WebDriver.for(
      :remote,
      url: "http://chrome:4444/wd/hub",
      desired_capabilities: caps
    )
    used_bridge = driver.bridge
    driver.get('https://www.google.com')
    
    # opens a new unused chrome window
    driver2 = Selenium::WebDriver.for(
      :remote,
      url: "http://chrome:4444/wd/hub",
      desired_capabilities: caps
    )
    
    driver2.close() # close unused chrome window
    driver2.set_bridge_to(used_bridge)
    
    driver2.title # => "Google"
    

    Sadly this did not test work between 2 rescue jobs, will update this in the future when I made it work for my own use case.

提交回复
热议问题