From f3e27404c24892e4077d5d6d89a9e5068de409f4 Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Sun, 20 Aug 2023 15:30:43 +0200
Subject: [PATCH] feature(account): choose

---
 CHANGELOG.md    |   4 ++
 src/Shopify.php | 110 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 78 insertions(+), 36 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5bee090..e78d6f2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## [1.0.1] - 2023-08-17
+### Fixed
+- account choose
+
 ## [1.0.0] - 2023-08-17
 ### Added
 - initial version
diff --git a/src/Shopify.php b/src/Shopify.php
index 28aca73..1d43761 100644
--- a/src/Shopify.php
+++ b/src/Shopify.php
@@ -12,50 +12,19 @@ class Shopify extends WebDriver
     public function loginAsAdmin(): void
     {
         $i    = $this;
-        $isPl = false;
         if ($i->loadSessionSnapshot('admin')) {
             return;
         }
         $i->amOnUrl($this->config['admin_url']);
         try {
-            $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
-            $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
+            $this->amLoggedInAsAdmin($i);
         } catch (Exception $e) {
-            $i->wait(rand(1, 2));
             try {
-                $i->waitForText('Continue with Google', 10);
+                $i->wait(rand(1, 2));
+                $i->see('Choose an account');
+                $i->click($this->config['admin_username']);
             } catch (Exception $e) {
-                $i->waitForText('Kontynuuj z Google', 30);
-                $isPl = true;
-            }
-            $i->click($isPl ? 'Kontynuuj z Google' : 'Continue with Google');
-            try {
-                $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
-                $i->dontSeeInCurrentUrl('google.com');
-            } catch (Exception $e) {
-                try {
-                    $i->waitForText($this->config['admin_username']);
-                    $i->clickWithLeftButton(Locator::find('div', ['data-email' => $this->config['admin_username']]));
-                    $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
-                    $i->dontSeeInCurrentUrl('google.com');
-                } catch (Exception $e) {
-                    $i->wait(rand(1, 5));
-                    $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
-                    try {
-                        $i->seeElement('input[name="identifier"]');
-                        $i->fillField('identifier', $this->config['admin_username']);
-                        $i->click($isPl ? 'Dalej' : 'Next');
-                        $i->wait(rand(1, 5));
-                        $i->moveMouseOver('input[name=Passwd]', rand(1, 100), rand(1, 100));
-                        $i->fillField('Passwd', $this->config['google_password']);
-                        $i->click($isPl ? 'Dalej' : 'Next');
-                    } catch (Exception $e) {
-                        $i->wait(rand(1, 5));
-                        $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
-                        $i->fillField('password', $this->config['google_password']);
-                        $i->click($isPl ? 'Dalej' : 'Next');
-                    }
-                }
+                $this->loginWithGoogle($i);
             }
         }
         $i->wait(rand(1, 5));
@@ -80,4 +49,73 @@ class Shopify extends WebDriver
         }
     }
 
+    /**
+     * @param Shopify $i
+     *
+     * @return void
+     */
+    private function amLoggedInAsAdmin(Shopify $i): void
+    {
+        $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
+        $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
+    }
+
+    /**
+     * @param Shopify $i
+     *
+     * @return void
+     * @throws Exception
+     */
+    private function loginWithGoogle(Shopify $i): void
+    {
+        $isPl = false;
+        try {
+            $i->waitForText('Continue with Google', 5);
+        } catch (Exception $e) {
+            $i->waitForText('Kontynuuj z Google', 5);
+            $isPl = true;
+        }
+        $i->click($isPl ? 'Kontynuuj z Google' : 'Continue with Google');
+        try {
+            $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
+            $i->dontSeeInCurrentUrl('google.com');
+        } catch (Exception $e) {
+            try {
+                $i->waitForText($this->config['admin_username']);
+                $i->clickWithLeftButton(Locator::find('div',
+                    ['data-email' => $this->config['admin_username']]));
+                $i->seeInCurrentUrl(parse_url($this->config['admin_url'], PHP_URL_PATH));
+                $i->dontSeeInCurrentUrl('google.com');
+            } catch (Exception $e) {
+                $this->loginToGoogle($i, $isPl);
+            }
+        }
+    }
+
+    /**
+     * @param Shopify $i
+     * @param bool $isPl
+     *
+     * @return void
+     */
+    private function loginToGoogle(Shopify $i, bool $isPl): void
+    {
+        $i->wait(rand(1, 5));
+        $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
+        try {
+            $i->seeElement('input[name="identifier"]');
+            $i->fillField('identifier', $this->config['admin_username']);
+            $i->click($isPl ? 'Dalej' : 'Next');
+            $i->wait(rand(1, 5));
+            $i->moveMouseOver('input[name=Passwd]', rand(1, 100), rand(1, 100));
+            $i->fillField('Passwd', $this->config['google_password']);
+            $i->click($isPl ? 'Dalej' : 'Next');
+        } catch (Exception $e) {
+            $i->wait(rand(1, 5));
+            $i->moveMouseOver('body', rand(1, 100), rand(1, 100));
+            $i->fillField('password', $this->config['google_password']);
+            $i->click($isPl ? 'Dalej' : 'Next');
+        }
+    }
+
 }
-- 
GitLab