Trying to implement page object model practise with selenium and python but an instance of chrome is not launching. can anyone point me in the right direction? I'm expecting Chrome to launch and fill in a login form.
尝试使用selenium和python实现页面对象模型练习,但是没有启动chrome的实例。任何人都能指出我正确的方向吗?我希望Chrome能够启动并填写登录表单。
this is what the console is spitting out currently: Process finished with exit code 0. And an " unresolved reference 'Login' " error on the Login_test method.
这是控制台当前正在吐出的内容:进程以退出代码0完成。并且Login_test方法上出现“未解析的引用'Login'”错误。
Also getting a red line under Login. while trying to use it in the test_login method.
在登录下也获得一条红线。同时尝试在test_login方法中使用它。
This is how my files are set up. I have 2 python files in separate folders. Both of these folders are in a project folder.
这就是我的文件设置方式。我在单独的文件夹中有2个python文件。这两个文件夹都在项目文件夹中。
Pages/login_page.py
Tests/login_test.py
#login_page.py file
> from selenium.webdriver.common.by import By
class Login:
_username_input = {"by": By.NAME, "value": "username"}
_password_input = {"by": By.NAME, "value": "password"}
_submit_button = {"by": By.ID, "value": "btn-inloggen"}
_success_message = {"by": By.CSS_SELECTOR, "value": ".#header > div.right > a:nth-child(4)"}
def __init__(self, driver):
self.driver = driver
self.driver.get("website")
def with_(self, username, password):
self.driver.find_element(self._username_input["by"],
self._username_input["value"]).send_keys(username)
self.driver.find_element(self._password_input["by"],
self._password_input["value"]).send_keys(password)
self.driver.find_element(self._submit_button["by"],
self._submit_button["value"]).click()
def login_succesfull_(self):
return self.driver.find_element(self._success_message["by"],
self._success_message["value"]).is_displayed()<
#test_login.py file
from selenium import webdriver
from Pages import login_page
class TestLogin:
def login(self, request):
driver = webdriver.Chrome()
def quit():
driver.quit()
request.addfinalizer(quit)
return login_page.Login(driver)
def test_login(self):
Login.with_("username", "password")
assert Login.login_succesfull_()
1 个解决方案
#1
0
The error is because there is no name Login
defined in the available scopes where you are using it. Instead you must use the qualified name login_page.Login
as in your login()
funciton.
该错误是因为在您使用它的可用范围中没有定义名称Login。相反,您必须在login()函数中使用限定名称login_page.Login。
However, more importantly, you cannot call with_()
or login_succesfull_()
on the class name. Instead, you need an instance of the Login
class.
但是,更重要的是,您不能在类名上调用with_()或login_succesfull_()。相反,您需要Login类的实例。
There are several pieces missing that are needed to fix this. First of all, Selenium is often used within the context of a testing framework. This isn't strictly required but is the most common use case. PyCharm defaults to running tests with unittest. To use unittest, you must create a class which inherits from the TestCase
class:
缺少修复此问题所需的部分内容。首先,Selenium经常在测试框架的上下文中使用。这不是严格要求,但是最常见的用例。 PyCharm默认使用unittest运行测试。要使用unittest,您必须创建一个继承自TestCase类的类:
import unittest.TestCase
class TestLogin(TestCase):
pass
Most such classes will have a setUp()
method which initializes the fixture for each test in the class. For example, this is where you can perform the login:
大多数这样的类都有一个setUp()方法,它初始化类中每个测试的fixture。例如,您可以在此处执行登录:
import unittest.TestCase
class TestLogin(TestCase):
def setUp(self):
self.page = self.login()
Next, your login()
function takes a request
parameter which it never uses. You can just remove it:
接下来,您的login()函数接受一个从不使用的请求参数。你可以删除它:
def login(self):
...
Now in your test, use the page
member variable which we created in setUp()
:
现在在测试中,使用我们在setUp()中创建的页面成员变量:
def test_login(self):
self.page.with_("username", "password")
assert self.page.login_succesfull_()
I suggest that you read more about unittest
before continuing with Selenium. You also need to learn more about Object Oriented Programming in Python, especially the syntax required to create and use objects.
我建议你在继续使用Selenium之前先阅读更多关于unittest的内容。您还需要了解有关Python中面向对象编程的更多信息,尤其是创建和使用对象所需的语法。
#1
0
The error is because there is no name Login
defined in the available scopes where you are using it. Instead you must use the qualified name login_page.Login
as in your login()
funciton.
该错误是因为在您使用它的可用范围中没有定义名称Login。相反,您必须在login()函数中使用限定名称login_page.Login。
However, more importantly, you cannot call with_()
or login_succesfull_()
on the class name. Instead, you need an instance of the Login
class.
但是,更重要的是,您不能在类名上调用with_()或login_succesfull_()。相反,您需要Login类的实例。
There are several pieces missing that are needed to fix this. First of all, Selenium is often used within the context of a testing framework. This isn't strictly required but is the most common use case. PyCharm defaults to running tests with unittest. To use unittest, you must create a class which inherits from the TestCase
class:
缺少修复此问题所需的部分内容。首先,Selenium经常在测试框架的上下文中使用。这不是严格要求,但是最常见的用例。 PyCharm默认使用unittest运行测试。要使用unittest,您必须创建一个继承自TestCase类的类:
import unittest.TestCase
class TestLogin(TestCase):
pass
Most such classes will have a setUp()
method which initializes the fixture for each test in the class. For example, this is where you can perform the login:
大多数这样的类都有一个setUp()方法,它初始化类中每个测试的fixture。例如,您可以在此处执行登录:
import unittest.TestCase
class TestLogin(TestCase):
def setUp(self):
self.page = self.login()
Next, your login()
function takes a request
parameter which it never uses. You can just remove it:
接下来,您的login()函数接受一个从不使用的请求参数。你可以删除它:
def login(self):
...
Now in your test, use the page
member variable which we created in setUp()
:
现在在测试中,使用我们在setUp()中创建的页面成员变量:
def test_login(self):
self.page.with_("username", "password")
assert self.page.login_succesfull_()
I suggest that you read more about unittest
before continuing with Selenium. You also need to learn more about Object Oriented Programming in Python, especially the syntax required to create and use objects.
我建议你在继续使用Selenium之前先阅读更多关于unittest的内容。您还需要了解有关Python中面向对象编程的更多信息,尤其是创建和使用对象所需的语法。