Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disconnect-driver resets headless mode #193

Closed
metametadata opened this issue Jan 24, 2019 · 3 comments · Fixed by #248 or #249
Closed

disconnect-driver resets headless mode #193

metametadata opened this issue Jan 24, 2019 · 3 comments · Fixed by #248 or #249
Assignees

Comments

@metametadata
Copy link

Lib version: 0.3.2.

I've bumped into this issue because I want to start driver process once for the whole test suite and let every test connect/disconnect to it (instead of starting and quiting browser process in every test).

Steps:

  1. Run headless Chrome driver.
  2. Connect to it.
  3. Disconnect.
  4. Connect again.

Expected: nothing happens.

Actual: browser window pops up.

Code example:

(require '[etaoin.api :as e])

(def driver nil)

(alter-var-root #'driver (fn [_]
                           (-> (e/create-driver :chrome)
                               (e/run-driver {:headless true}))))
(alter-var-root #'driver e/connect-driver)
(alter-var-root #'driver e/disconnect-driver)
(alter-var-root #'driver e/connect-driver)

It is caused by disconnect-driver which erases :capabilities from driver atom including :chromeOptions {:args ["--headless"]}. Thus second connect-driver is made without the needed CLI arg.

Possible solution is to not modify :capabilities in run-driver and let connect-driver generate :args depending on :headless flag from driver map.

@Uunnamed
Copy link
Contributor

Hi, @metametadata

For your goals, you can use fixtures, like this:

(def ^:dynamic *driver*)

(defn fixture-browser [f]
  (let [driver (chrome-headless)]
    (binding [*driver* driver]
      (f))
    (quit driver)))

;; if you want reset some changes (delete cookie, refresh page and etc), or Vice versa,
;; make pre-settings before test you can use fixture like this:
(defn fixture-clear-browser [f]
  (delete-cookies *driver*)
  (go *driver* "https://google.com")
    (f))

;; this is run `once` before running the tests
(use-fixtures
  :once
  fixture-browser)

;;this is run `every` time before each test
(use-fixtures
  :each
  fixture-clear-browser)

@metametadata
Copy link
Author

metametadata commented Jul 24, 2020

Thanks. But if I understand everything correctly, I would prefer to not "clear" by hand like this. Connecting/disconnecting looks more bulletproof (it for sure clears all state, e.g. including local storage) and needs less code.

In either case, the API behaves unexpectedly (IMO of course), the MR doesn't address this root problem.

This is my current code:

; Driver type used for all the tests
(def -driver-type :chrome)
(def -headless? true)

; Global driver instance
(def d nil)

(defn with-driver
  ":each fixture which will create a new driver session for each test case.
  Driver process will be spawned if it's not already running."
  [f]
  ; Start driver process if needed.
  ; Note that there's no need to destroy it explicitly because it will be killed when the parent process quits.
  (when (nil? d)
    (println (str "Start " (pr-str -driver-type) " (headless = " (pr-str -headless?) ") driver process..."))
    (alter-var-root #'d (fn [_]
                          (-> (e/create-driver -driver-type)
                              (e/run-driver {:headless -headless?})))))

  (e.client/with-pool {}
    ; HACK: needed because e/disconnect-driver erases all capabilities from driver.
    ; https://github.com/igrishaev/etaoin/issues/193.
    (when -headless?
      (swap! d e.driver/set-headless))

    ; Bump logging level to get fuller logs on test failures.
    ; It seems to be WARNING by default in Chrome and PhantomJS.
    (swap! d e.driver/set-browser-log-level :debug)

    (alter-var-root #'d e/connect-driver)
    (f)
    (alter-var-root #'d e/disconnect-driver)))

...

(use-fixtures :each with-driver)

(deftest ...)

@Uunnamed
Copy link
Contributor

Uunnamed commented Jul 24, 2020

@metametadata We’ll fix it, thanks for feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants