R
R
rolandDiskeyn2020-06-24 13:53:06
Python
rolandDiskeyn, 2020-06-24 13:53:06

Why does chrome automatically close after a test completes?

I started studying testing, I use selenium + python for automation, I noticed such a problem, if the test itself is described in a class, then when the test is completed, the browser automatically closes.

import unittest
import time
from selenium import webdriver

class Test():
    def __init__(self):
        driver = webdriver.Chrome(executable_path=r'C:\Users\****** *****\Desktop\kodtest\python\AutoTesting\chromedriver.exe')
        time.sleep(1)

        driver.get("https://stepik.org/lesson/25969/step/12")
        time.sleep(5)

        textarea = driver.find_element_by_css_selector(".textarea")
        textarea.send_keys('get()')
        time.sleep(1)

        submit_button = driver.find_element_by_css_selector(".submit-submission")

        submit_button.click()
        time.sleep(3)

Test()


If you write a test without using classes, then everything works as it should, tell me please, what could be the reason for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2020-06-24
@rolandDiskeyn

Вполне возможно, потому что вы все делаете в __init__ методе.
Не нашел почему браузер закрывается, если делать так, как у вас

import time
from selenium import webdriver


class Test():
    def __init__(self):
        self.driver = webdriver.Chrome()

    def test_case(self):
        self.driver.get("https://stepik.org/lesson/25969/step/12")
        time.sleep(5)
        textarea = self.driver.find_element_by_css_selector(".textarea")
        textarea.send_keys('get()')
        time.sleep(1)

        submit_button = self.driver.find_element_by_css_selector(".submit-submission")
        submit_button.click()
        time.sleep(3)


if __name__ == '__main__':
    a = Test()
    a.test_case()

Лучший вариант - не использовать sleep в коде и запускать тесты через unittest. После каждого теста - нужно перезапускать инстанс браузера.
import unittest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

wait_time = 20  # seconds


class TestStepik(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, wait_time)

    def tearDown(self):
        self.driver.quit()

    def test_submit_button(self):
        self.driver.get("https://stepik.org/lesson/25969/step/12")
        textarea = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".textarea")))
        textarea.send_keys('get()')
        submit_button = self.driver.find_element_by_css_selector(".submit-submission")
        submit_button.click()
        self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".attempt-message_correct")))


if __name__ == '__main__':
    unittest.main()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question