R
R
ryndenkov2022-04-04 13:24:54
Python
ryndenkov, 2022-04-04 13:24:54

How to optimize this code in python - selenium?

20 tests pass in 120 seconds, which is a very long time, how can it be run in several threads, let's say 10. Well, or make one function, replacing the parameter in send_key?

import unittest
#import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

URL = 'https://psy.edu.ru/'

class check_psyedu(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome(r'C:\Users\Алексей\YandexDisk\Job\Python\Test\chromedriver\chromedriver.exe')
        
    #def read_excel():
        #list_name = pd.read_excel('./list_name.xlsx')
        #list_name.head()

    def test_1(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("aleksei")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()
    
    def test_2(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("Алексей")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()
    
    def test_3(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys(" Рынденков Алексей")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
    
    def test_4(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("123123")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
        
    def test_5(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("@алексей")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
    
    def test_6(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("sofa")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()
    
    def test_7(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
    
    def test_8(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys(" ")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()   
    
    def test_9(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()   
    
    def test_10(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("Софья")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()   
    
    def test_11(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("Иван")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()   
        
    def test_12(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys(".Алексей")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
        
    def test_13(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("-Кирилл")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
        
    def test_14(self):
        self.driver.get(URL)
        self.driver.set_window_size(1920, 1080)
        self.driver.find_element(By.NAME, "name").send_keys("уцкаушатуоатурашгукратиукалукшгарукта")
        self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
        assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"
        self.driver.close()    
        
    def tearDown(self):
          self.driver.quit()

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sswwssww, 2022-04-04
@ryndenkov

setUp is called before each test method, i.e. you have it called 14 times, like tearDown . Replace it with setUpClass , which will only be called once. And accordingly, you change tearDown to tearDownClass .
And of course you change your tests (for example, remove unnecessary self.driver.set_window_size(1920, 1080) etc. in each method, just move it to setUpClass ). This will save you a LOT of time.
Or, to remove code duplication, you can use the subTest context manager:

import unittest

from selenium import webdriver
from selenium.webdriver.common.by import By

URL = 'https://psy.edu.ru/'


class check_psyedu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(r'C:\Users\Алексей\YandexDisk\Job\Python\Test\chromedriver\chromedriver.exe')
        self.driver.set_window_size(1920, 1080)

    def test_1(self):
        names = ("aleksei", "Алексей", " Рынденков Алексей",)
        for name in names:
            # тут name просто как название теста, чтобы когда тест не прошёл было наглядно видно где споткнулся
            with self.subTest(name):  
                self.driver.get(URL)
                self.driver.find_element(By.NAME, "name").send_keys(name)
                self.driver.find_element(By.CSS_SELECTOR, ".btn").click()
                assert self.driver.find_element(By.CSS_SELECTOR,
                                                ".still-quest_item:nth-child(1) > .err_message").text == "Напишите русскими буквами!"
                assert self.driver.find_element(By.CSS_SELECTOR, ".err_message:nth-child(3)").text == "Обязательное поле!"
                assert self.driver.find_element(By.CSS_SELECTOR, ".still-quest_item:nth-child(3)").text == "Обязательное поле!"

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


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

There is a third option, to use a decorator that adds the ability to run the same test function with different arguments. In pytest, such a "feature" is built in, in unittest you need to use the parameterized library .
Example:
@parameterized([
    (2, 2, 4),
    (2, 3, 8),
    (1, 9, 1),
    (0, 9, 0),
])
def test_pow(base, exponent, expected):
   assert_equal(math.pow(base, exponent), expected)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question