изучил через лекцию, его код работает здорово, но мой не работает, показывает такую ошибку, перечисленную ниже кода. Создайте два разных класса, используя объектную модель страницы, в python PyCharm IDE os Windows 10 Pro 2019. Происходит каждый раз, пожалуйста, дайте мне знать, по какой причине эта ошибка атрибута может возникнуть и в будущем.
Мой код для страницы:
from selenium.webdriver.support.select import Select
class CreateAccount:
def __init__(self, driver):
self.driver = driver
self.createAccount = "(//a[text() = 'create account'])[1]"
self.userName = "//input[@name='name']"
self.userMail = "(//input[@name='email'])[2]"
self.userPhone = "//input[@name='phone']"
self.userPassword = "(//input[@name='password'])[2]"
self.userConf_Password = "//input[@name='confirmPassword']"
self.userRole = "//select[@name = 'role']"
self.userSubmit = "(//button[@type='submit'])[2]"
self.close_Button = "(//button[@class='close'])[2]"
def create_Account(self):
self.driver.find_element_by_xpath(self.createAccount).click()
def user_Name(self, name):
self.driver.find_element_by_xpath(self.userName).send_keys(name)
def user_Mail(self, mail):
self.driver.find_element_by_xpath(self.userMail).send_keys(mail)
def user_Phone(self, phone):
self.driver.find_element_by_xpath(self.userPhone).send_keys(phone)
def user_Password(self, password):
self.driver.find_element_by_xpath(self.userPassword).send_keys(password)
def user_ConfPassword(self, conf_Password):
self.driver.find_element_by_xpath(self.userConf_Password).send_keyS(conf_Password)
def user_Role(self, role):
select = Select(self.driver.find_element_by_xpath(self.userRole))
select.select_by_visible_text(role)
def user_submit(self):
self.driver.find_element_by_xpath(self.userSubmit).click()
def closeButton(self):
self.driver.find_element_by_xpath(self.close_Button).click()
# def getAllDetails(self, name, phone, mail,
# password, conf_password, role):
# self.user_Name(name)
# self.user_Phone(phone)
# self.user_Mail(mail)
# self.user_Password(password)
# self.user_ConfPassword(conf_password)
# self.user_Role(role)
# self.user_submit()
--------------------------------------------------------------------------------------------------
class LogIn:
def __init__(self, driver):
self.driver = driver
self.logInButton = "(//a[text() = 'login'])[1]"
self.userMail = "(//input[@name='email'])[1]"
self.userPassword = "(//input[@name='password'])[1]"
self.submitButton = "(//button[@type='submit'])[1]"
self.close = "(//button[@class='close'])[1]"
def loginButton(self):
self.driver.find_element_by_xpath(self.logInButton).click()
def mailAddress(self, mail):
self.driver.find_element_by_xpath(self.userMail).send_keys(mail)
def password(self, password):
self.driver.find_element_by_xpath(self.userPassword).send_keys(password)
def submit(self):
self.driver.find_element_by_xpath(self.submitButton).click()
def closeButton(self):
self.driver.find_element_by_xpath(self.close).click()
Мой код для теста:
from selenium import webdriver
from YFT_Pages.LogInPage import LogIn
from YFT_Pages.CreateAccount import CreateAccount
import unittest
class MyTestCase(unittest.TestCase):
@classmethod
def setUp(cls) -> None:
driver = webdriver.Chrome(executable_path = r"E:\Doc & Sw\chromedriver.exe")
def test_LogIn(self):
driver = self.driver
driver.get("any url: can't share mine one")
createAccount = CreateAccount(driver)
createAccount.user_Name("Shorya Kaushik")
createAccount.user_Phone("8791517915")
createAccount.user_Mail("Shorya@Test.com")
createAccount.user_Password("123")
createAccount.user_ConfPassword("123")
createAccount.user_Role("Player")
createAccount.user_submit()
login = LogIn(driver)
login.userMail("shorya@test.com")
login.userPassword("123")
login.submit()
@classmethod
def tearDown(cls) -> None:
cls.driver.close()
cls.driver.quit()
if __name__ == '__main__':
unittest.main()
Ошибка:
Testing started at 12:29 AM ...
C:\Users\Pt.Shorya_Kaushik\PycharmProjects\Footech_PY\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.4\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path C:/Users/Pt.Shorya_Kaushik/PycharmProjects/Footech_PY/YFT_Tests/LogInTest.py
Launching unittests with arguments python -m unittest C:/Users/Pt.Shorya_Kaushik/PycharmProjects/Footech_PY/YFT_Tests/LogInTest.py in C:\Users\Pt.Shorya_Kaushik\PycharmProjects\Footech_PY\YFT_Tests
Ran 1 test in 2.248s
FAILED (errors=2)
Error
Traceback (most recent call last):
File "C:\Python38-32\lib\unittest\case.py", line 60, in testPartExecutor
yield
File "C:\Python38-32\lib\unittest\case.py", line 676, in run
self._callTestMethod(testMethod)
File "C:\Python38-32\lib\unittest\case.py", line 633, in _callTestMethod
method()
File "C:\Users\Pt.Shorya_Kaushik\PycharmProjects\Footech_PY\YFT_Tests\LogInTest.py", line 14, in test_LogIn
driver = self.driver
AttributeError: 'MyTestCase' object has no attribute 'driver'
Error
Traceback (most recent call last):
File "C:\Python38-32\lib\unittest\case.py", line 60, in testPartExecutor
yield
File "C:\Python38-32\lib\unittest\case.py", line 679, in run
self._callTearDown()
File "C:\Python38-32\lib\unittest\case.py", line 636, in _callTearDown
self.tearDown()
File "C:\Users\Pt.Shorya_Kaushik\PycharmProjects\Footech_PY\YFT_Tests\LogInTest.py", line 32, in tearDown
cls.driver.close()
AttributeError: type object 'MyTestCase' has no attribute 'driver'
Assertion failed
Process finished with exit code 1
Assertion failed
Assertion failed