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

Fix chrome profile #289

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
10 changes: 7 additions & 3 deletions src/etaoin/driver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,13 @@
;; Chrome adds the trailing `/Default` part to the profile path.
;; To prevent duplication, let's clear the given path manually.
[driver profile]
(let [default #"(\\|/)Default$"
p (string/replace profile default "")]
(set-options-args driver [(format "--user-data-dir=%s" p)])))
(let [default #"(\\|/)Default$"
Copy link
Collaborator

@igrishaev igrishaev Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, этот код нужно отрефакторить. Основной момент в том, чтобы для файловых путей использовать объект java.io.File -- это безопасней и работает везде с учетом особенностей ОС.

  • Обернуть строковый путь в io/file или (new File)
  • если имя файла Default, то получить родительский файл (.getParent или как-то так)
  • потом еще раз поняться на уровень выше
  • этот уровнеь станет data-dir, а имя профиля -- имя файла (.getFileName или как-то так)

profile-path (string/replace profile default "")
splited-p (string/split profile-path #"/")
user-data-dir (string/join "/" (drop-last splited-p))
profile-dir (last splited-p)]
(set-options-args driver [(format "--user-data-dir=%s" user-data-dir)
(format "--profile-directory=%s" profile-dir)])))

(defmethod set-profile
:firefox
Expand Down
10 changes: 9 additions & 1 deletion test/etaoin/api_test2.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns etaoin.api-test2
(:require [etaoin.api :refer :all]
[etaoin.proc]
[clojure.test :refer :all]))
[clojure.test :refer :all]
[clojure.java.io :as io]))

(deftest test-firefox-driver-args
(with-redefs
Expand Down Expand Up @@ -29,6 +30,13 @@
(is (= ["geckodriver" "--port" 1234 "--marionette-port" 2821]
(:args @driver)))))))

(deftest test-chrome-profile
(let [profile-path (str (io/resource "chrome-profile"))]
(with-chrome {:profile profile-path} driver
(go driver "chrome://version")
(is profile-path
(get-element-text driver :profile_path)))))

(deftest test-fail-run-driver
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
Expand Down