I would appreciate some help. I am using a Mac, a Jupyter notebook and Python and when running this code the automated browser opens the window and instantly closes it (unless I set a sleep or 10 seconds for example, then, it closes it after the 10 secs). This code works when running it out of a unittest, I can login in removing all code related to the test.
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
import unittest
import sys
class TestClass(unittest.TestCase):
def setUp(self):
print("=>Running before each test")
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
chrome_driver_binary = "/Users/ana/Desktop/drivers/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
driver.get("url_to_the_app")
#time.sleep(10)
pass
def test_1(self):
print("TEST 1")
# Log in
driver.find_element(By.XPATH, "//input[@formcontrolname='username']").click()
time.sleep(3)
driver.find_element(By.XPATH, "//input[@formcontrolname='username']").send_keys("myusername")
time.sleep(3)
driver.find_element(By.XPATH, "//input[@formcontrolname='password']").click()
time.sleep(3)
driver.find_element(By.XPATH, "//input[@formcontrolname='password']").send_keys("mypassword")
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, ".button-wrapper").click()
pass
def tearDown(self):
print("=>Running after each test")
pass
# RUN TEST:
suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
unittest.TextTestRunner(verbosity=4,stream=sys.stderr).run(suite)
This is the output:
/var/folders/c6/62__rcfn4gj90d8qpl37d_zr0000gp/T/ipykernel_1941/2377839007.py:10: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
/var/folders/c6/62__rcfn4gj90d8qpl37d_zr0000gp/T/ipykernel_1941/2377839007.py:10: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
test_1 (__main__.TestClass) ... /var/folders/c6/62__rcfn4gj90d8qpl37d_zr0000gp/T/ipykernel_1941/984023069.py:14: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
/var/folders/c6/62__rcfn4gj90d8qpl37d_zr0000gp/T/ipykernel_1941/984023069.py:14: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
=>Running before each test
TEST 1
=>Running after each test
ok
----------------------------------------------------------------------
Ran 1 test in 1.793s
OK
Many thanks in advance!