From 58f417762ba1c5b5313a4ba518355f7fddc5d33a Mon Sep 17 00:00:00 2001 From: DakaD <3106338+Dakad@users.noreply.github.com> Date: Fri, 5 May 2023 16:21:29 +0200 Subject: [PATCH] Update Service & Driver class to run multiple instances of same driver on different port --- spec/features/session_spec.cr | 33 +++++++++++++++++++++++++++++++++ src/selenium/service.cr | 7 +++++++ 2 files changed, 40 insertions(+) create mode 100644 spec/features/session_spec.cr diff --git a/spec/features/session_spec.cr b/spec/features/session_spec.cr new file mode 100644 index 0000000..cda8429 --- /dev/null +++ b/spec/features/session_spec.cr @@ -0,0 +1,33 @@ +require "../spec_helper" + +module Selenium::Command + describe "session", tags: "feature" do + it "run multiple browsers on separate process" do + TestServer.route "/home", "

The Title

" + TestServer.route "/next-page", "

You made it!

" + TestServer.route "/action-page", <<-HTML + Click Me! + HTML + + begin + driver1, args1 = Selenium::TestDriverFactory.build(ENV["SELENIUM_BROWSER"]? || "chrome") + session1 = driver1.not_nil!.create_session(args: args1.not_nil!) + driver2, args2 = Selenium::TestDriverFactory.build(ENV["SELENIUM_BROWSER"]? || "chrome") + session2 = driver2.not_nil!.create_session(args: args2.not_nil!) + + session1.navigate_to("http://localhost:3002/home") + session2.navigate_to("http://localhost:3002/action-page") + element = session2.find_element(:link_text, "Click Me!") + element.click + session1.current_url.should eq("http://localhost:3002/home") + session2.current_url.should eq("http://localhost:3002/next-page") + ensure + session1.try &.delete + driver1.try &.stop + session2.try &.delete + driver2.try &.stop + TestServer.reset + end + end + end +end diff --git a/src/selenium/service.cr b/src/selenium/service.cr index a21b481..f97ba1a 100644 --- a/src/selenium/service.cr +++ b/src/selenium/service.cr @@ -20,6 +20,7 @@ abstract class Selenium::Service end def start + find_free_port start_process verify_running end @@ -37,6 +38,12 @@ abstract class Selenium::Service abstract def default_port : Int32 + private def find_free_port + while listening? + @port += 1 + end + end + private def start_process @process = Process.new( @driver_path,