I am running a django test using selenium. Everything really works fine however when I look at the terminal it returns this outputs "None" and "3" as shown in the picture
I want to remove those as it consume too much space when running so many tests.
I am using python 3.5.1 and djnago 1.9.1
and my code for testing is this:
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import TestCase, LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from login.tests import LogInReusablesScript as LRS , LogInTestFT as LTFT
from unittest import skip
import time
import inspect
# Create your tests here.
# Command Line: python manage.py test crew_menu.crew_admin.rank_management
# python manage.py test crew_menu.crew_admin.rank_management.tests.RankManagementEmptyTestListPage
class RankManagementEmptyTestListPage(StaticLiveServerTestCase):
# Used LTFT because LRS already inherits the appended json all throughout
fixtures = ['login_users.json', 'bio.json']
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
self.url = '/crew-menu/crew-admin/rank-management/'
def tearDown(self):
self.browser.quit()
def set_goToListPage(self):
# self._setup_login_with_access
# User Logs in
self.browser.get(self.live_server_url) # .get is tells the browser to go to a new page
# Type a user's user name in the field
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('adgc')
# Type a user's password in the field
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('adgcadgc')
# Click the login button
login = self.browser.find_element_by_tag_name('button')
login.click()
time.sleep(2)
# Server goes to the Rank Management List Page
self.browser.get(self.live_server_url+self.url)
time.sleep(2)
# System checks the title of the current page if it is Rank Management
self.assertIn('Rank Management', self.browser.title)
def test_IsEmpty(self):
# User Goes to the the Rank List Page
self.set_goToListPage()
time.sleep(1)
# System checks the output if the table is empty
tr = self.browser.find_elements_by_tag_name("tr")
self.assertIn('No records found', tr[1].text)
Related
this is the confest.py file present in the test cases file
import pytest
from selenium import webdriver
#pytest.fixture()
def setup():
driver = webdriver.Chrome()
return driver
this is login file present in the page package
import pytest
from selenium import webdriver
class Loginchatbot():
username_xpath = '//input[#id="username"]'
password_xpath = '//input[#id="password"]'
login_xpath = '//span[contains(text(),"Log in")]'
def __init__(self,driver):
self.driver = driver
def set_username(self,username):
#launching the browser
#login in the application
self.wait.driver.find_element_by_xpath(self.username_xpath).clear()
self.wait.driver.find_element_by_xpath(self.username_xpath).click().send_keys(username)
def set_password(self, password):
# launching the browser
# login in the application
self.wait.driver.find_element_by_xpath(self.password_xpath).clear()
self.wait.driver.find_element_by_xpath(self.password_xpath).click().send_keys(password)
def clicklogin(self):
# launching the browser
# login in the application
self.wait.driver.find_element_by_xpath(self.login_xpath).click()
this is the test_login file present in the test cases folder
import pytest
from selenium import webdriver
from p username = "shubhamgupta191190#gmail.com"ages.login import Loginchatbot
class test001_login:
baseurl = "https://teacher-learning-test.ef.com/"
password = "freeBugs!"
def test_login1(self,setup):
self.driver = setup
self.driver.get(self.baseurl)
self.lp = Loginchatbot(self.driver)
self.lp.set_username(self.username)
self.lp.set_password(self.password)
self.lp.clicklogin()
I am trying to design automation framework and I am new to selenium and I am using python with selenium and pytest and while executing the above script written in test_login and function name as test_login1 getting the error message as
platform win32 -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: C:\\Users\\GREEN EARTH\\PycharmProjects\\Chatbot
collected 0 items
================================================================ no tests ran in 0.01s \
Option 1. Change class name
Your test class is called test001_login.
pytest by default allows class names that start with Test.
Consider renaming your test class to Test001Login.
Source
Option 2. Change pytest naming convention
If you want to stick with test001_login class name and change the standard naming convention then you need to define it in your pytest.ini file like this:
[pytest]
python_classes = test
Source
So I'm currently working on redoing a project I did when I was new to Django. This time I'd like to ensure I'm using tests for practice and getting to know Django better. My issue is that I have a User that I know is in the system, as I have ogged in as them before, but when I run a test using self.client.post(), it returns None. I have triple checked spelling of username and password along with copy-paste the working strings in the func test and still nothing.
Here is the test in question
def test_login_authenticates_users(self):
response = self.client.post('/', {'username' : 'TestUser', 'password' : 'password'})
self.assertRedirects(response, '/')
# IDK why this line fails
self.assertNotEqual(None, authenticate(username='Test.User', password='Usetheforceluke'))
response = self.client.post('/', {'username' : 'Test.User', 'password' : 'Usetheforceluke'})
self.assertRedirects(response, '/home')
Here is the functional test that works:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewTeacher(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_teacher_adds_a_new_class(self):
# The MCSP Department has a webpage used for all of the assignments
# A teacher decides to go to the MSCP homepage to manage their classes
self.browser.get('http://localhost:8000')
# The title mentions MCSP in the title
self.assertIn('MCSP', self.browser.title)
# They then log in using their username and password
username_input = self.browser.find_element_by_id('username_input_box')
password_input = self.browser.find_element_by_id('password_input_box')
self.assertEqual(username_input.get_attribute('placeholder'),
'Username')
self.assertEqual(password_input.get_attribute('placeholder'),
'Password')
# It goes through here to the set failure later in the test
username_input.send_keys('Test.User')
password_input.send_keys('Usetheforceluke')
password_input.send_keys(Keys.ENTER)
time.sleep(1)
# From there they see a navibar that shows "Home", "Grades", "Courses", and "Logout"
self.assertIn('Home Page', self.browser.title)
# They are currently on the home page which lets them see upcoming assignments and recent submissions
self.fail('Finish the test')
# Next they create a class by using the "Add Course" button on the navbar
self.fail('Finish the test')
# They also do so by uploading a spreadsheet
self.fail('Finish the test')
# The work done, they log out
self.fail('Finish the test')
if __name__ == '__main__':
I'm trying to keep this down to posting relevant code, i could post the view and the form i have done but as far as i am aware, that shouldn't be affecting this. I have looked into other questions before posting and have added the
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
to my settings.py without change. If anyone needs more code just let me know what!
EDIT: So i did some more messing around by printing all the users in various points and remembered that Djangos TestCase does NOT use the database that the system is on. I forgot that if you want to test authentication, you will need to create the users in the test.
I am trying to run one method from a different module in pytest using import. It runs correctly but in the report it says empty suite. Its not printing the print statements.
roboForm.py -
import unittest
from selenium import webdriver
from selenium.webdriver.support.select import Select
class Roboform(unittest.TestCase):
def setUp(self):
# create a new Chrome session
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.driver.get("https://www.roboform.com/filling-tests")
def click_Custom_Form(self):
# get the xpath of the link and page
title = self.driver.title
assert title== "RoboForm Form Filling Tests"
print("We are on the right page")
#click on custom form link
self.custom_form = self.driver.find_element_by_xpath("//*[contains(text(),'Custom')]").click()
def close_browser(self):
self.driver.quit()
I am running the below code in pytest - test_classB.py
import self as self
from roboForm import Roboform
class Test():
Roboform.setUp(self)
print ("Browser and page launched from RoboForm")
self.driver.find_element_by_xpath("//*[contains(text(),'Custom')]").click()
print ("Test Passed")
Roboform.close_browser(self)
Getting the below error :
======================== no tests ran in 11.53 seconds ========================
Process finished with exit code 0
Empty suite
Empty suite
Agreed with #Corey Goldberg.
Try
def test_click_Custom_Form(self):
instead
def click_Custom_Form(self):
I'm trying to create a test suite with pytest and Selenium using Page Object Models for pattern designing. For using my page classes on my tests, I just imported them inside my TestClass __init__ method, since they need to instanced with a driver.
I know that, by default, pytest ignores classes with an __init__ method. I also know, by reading here that it's possible to configure where pytest collects tests. Is it also possible to make it consider class tests with __init__, instead of returning an "Empty Suite" error?
#pytest.fixture(scope="session")
def driver_init(request):
from selenium import webdriver
driver = webdriver.Chrome()
session = request.node
page = PageFunctions(driver)
login_page = LoginPage(driver)
registration_page = RegistrationPage(driver)
for item in session.items:
cls = item.getparent(pytest.Class)
setattr(cls.obj, "driver", driver)
setattr(cls.obj, "page", page)
setattr(cls.obj, "login", login_page)
setattr(cls.obj, "registration", registration_page)
Pytest and Unittest have some different conventions. Mixing the two in the same test function is usually worth avoiding.
If you're working exclusively with Pytest, you would pass in your fixtures as arguments to your test function, e.g.:
import pytest
from selenium import webdriver
#pytest.fixture
def driver():
driver = webdriver.Chrome()
return driver
def test_func(driver):
# `driver` is found by pytest in the fixture above and
# automatically passed in
request = ... # Instantiate your request (not in your included code)
session = request.node
page = PageFunctions(driver)
login_page = LoginPage(driver)
registration_page = RegistrationPage(driver)
# Make some assertions about your data, e.g.:
assert page is not None
You haven't included all of the object definitions/imports so it's hard to see what you're trying to accomplish with the test, but hopefully that gives you an idea of the pytest conventions.
I am trying to add some selenium tests to my Django project, but the second test always fails with a Server Error (500) . Since both tests start exactly the same, I figure it must have something to do with the setUp and tearDown methods. Can someone help? Thanks.
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User
from selenium.webdriver.support.ui import Select
class UserTest(LiveServerTestCase):
def setUp(self):
User.objects.create_user(username='user', password='pass', email='test#test.com')
self.browser = webdriver.Chrome()
def tearDown(self):
self.browser.quit()
def changeSelector(self, browser, value):
mealSelector = Select(browser.find_element_by_id('mealsToday'))
mealSelector.select_by_visible_text(str(value))
def login_user(self):
self.browser.get(self.live_server_url)
self.timeout(5)
self.assertIn('Animals', self.browser.title)
# Log in
login_button = self.browser.find_element_by_id('login').click()
self.browser.find_element_by_id('id_username').send_keys('user')
self.browser.find_element_by_id('id_password').send_keys('pass')
def timeout(self, time_to_sleep):
import time
time.sleep(time_to_sleep)
def test_one_test(self):
self.login_user()
def test_two_test(self):
self.login_user()
Edit: I should mention that the first test works fine and returns success. Any test after the first one fails right on starting up with the 500 error.
Edit 2: What I see when I run my tests:
======================================================================
FAIL: test_two_test (functional_tests.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/functional_tests/tests.py", line 34, in test_two_test
self.login_user()
File "/functional_tests/tests.py", line 20, in login_user
self.assertIn('Animals', self.browser.title)
AssertionError: 'Animals' not found in 'http://localhost:8081/'
Even this minimal code fails:
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User
from selenium.webdriver.support.ui import Select
class UserTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def tearDown(self):
self.browser.quit()
def login_user(self):
self.browser.get(self.live_server_url)
self.assertIn('Animals', self.browser.title)
def test_one_test(self):
self.login_user()
def test_two_test(self):
self.login_user()
The second time the get is called in the second method I can see that the 500 Error is there and that nothing is correctly loaded. Why would this be?
After some code to be able to show me errors (the testing suite sets DEBUG=False to closer simulate the real env) by setting DEBUG=True
Then I saw that the code was bombing because a row wasn't there that the system expected. This is because I add this row in a migration script. This passes the first test because all migration scripts are run at the beginning of testing, but after all data is deleted after the first test, it is never added again.
It's difficult to say without seeing the complete traceback, but, it could be because of the create_user() call - it fails to create a user with an existing username. Try moving the create_user() under the setUpClass():
class UserTest(LiveServerTestCase):
#classmethod
def setUpClass(cls):
User.objects.create_user(username='user', password='pass', email='test#test.com')
super(UserTest, cls).setUpClass()
def setUp(self):
self.browser = webdriver.Chrome()
Maybe, it will be useful for somebody.
I have problems with testing with Selenium using LiveServerTestCase, Debug was True, all about last answers were about was OK.
But, earlier I tried to deploy my web-application on Heroku, and I have such line of code in my settings.py:
django_heroku.settings(locals())
But when I removed it, I didn't catch this error.