“In the life of every Django developer, there comes a moment when he vigorously vomits with his past, devoid of functional testing!”
pip install selenium
sudo apt-get install iceweasel
! Note:
When we try to run the test from the terminal, without launching the graphical environment, we will get an exception: raise WebDriverException ("The browser appears to have exegee.common.exceptions.WebDriverException: If you Specified a log_file in the FirefoxBinary constructor, check it for details.)
# Django Selenium from django.test import LiveServerTestCase # ( FireFox) from selenium import webdriver import time class SeleniumTests(LiveServerTestCase): def test_auth(self): # webdriver Firefox br = webdriver.Firefox() # , 'localhost:8081' URL br.get('%s%s' % (self.live_server_url, '/')) # br.find_element_by_xpath('//a[@href="/register/"]').click() # 3 time.sleep(3) # # username 'new' br.find_element_by_id('username').send_keys('new') # email 'new@new.ru' br.find_element_by_id('email').send_keys('new@new.ru') # 2- br.find_element_by_id('password1').send_keys('12345678') br.find_element_by_id('password2').send_keys('12345678') # br.find_element_by_id('btn_register').click() # pis = Myuser.objects.get(username='new') # - pis.is_active = True # pis.save() # br.find_element_by_xpath('//a[@href="/"]').click() # 3 time.sleep(3) # br.find_element_by_id('username').send_keys('new') br.find_element_by_id('password').send_keys('12345678') br.find_element_by_name('').click() # assert br.find_element_by_xpath('//a[@data-content=" "]').text == 'new' # , br.quit()
! Note:
It is necessary to pause time.sleep (3) in the program text to avoid the error: “selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {" method ":" id "," selector ":" username "}" , “Can not find element”, caused by the fact that it did not have time to load the whole context of the page. If an error occurs, you can increase the wait time.
python manage.py test authentication/
python manage.py test
python manage.py test authentication.tests.SeleniumTests
python manage.py test authentication.tests.SeleniumTests.test_auth
sudo apt-get install xvfb pip install pyvirtualdisplay
! Note:
We set PyVirtualDisplay on behalf of a local user, otherwise when testing from under a user in the import line: from pyvirtualdisplay import Display, there will be an error.
PyVirtualDisplay 0.1.5 officially supports python versions: 2.6, 2.7, 3.2, 3.3, tested at 3.4 - it works
# from pyvirtualdisplay import Display # Django Selenium from django.test import LiveServerTestCase # ( FireFox) from selenium import webdriver import time class SeleniumTests(LiveServerTestCase): def test_auth(self): # display = Display(visible=0, size=(800, 600)) display.start() # webdriver Firefox br = webdriver.Firefox() # , 'localhost:8081' URL br.get('%s%s' % (self.live_server_url, '/')) # br.find_element_by_xpath('//a[@href="/register/"]').click() # 3 time.sleep(3) # # username 'new' br.find_element_by_id('username').send_keys('new') # email 'new@new.ru' br.find_element_by_id('email').send_keys('new@new.ru') # 2- br.find_element_by_id('password1').send_keys('12345678') br.find_element_by_id('password2').send_keys('12345678') # br.find_element_by_id('btn_register').click() # pis = Myuser.objects.get(username='new') # - pis.is_active = True # pis.save() # br.find_element_by_xpath('//a[@href="/"]').click() # 3 time.sleep(3) # br.find_element_by_id('username').send_keys('new') br.find_element_by_id('password').send_keys('12345678') br.find_element_by_name('').click() # assert br.find_element_by_xpath('//a[@data-content=" "]').text == 'new' # display.stop() # , br.quit()
cd /usr/local/share sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2 sudo tar xjf phantomjs-1.9.8-linux-x86_64.tar.bz2 sudo ln -s /usr/local/share/phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs sudo ln -s /usr/local/share/phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs sudo ln -s /usr/local/share/phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/bin/phantomjs
phantomjs -v
! Note:
If the received packet is not suitable for this system, we get an error: “I can not run a binary file” (Often due to an attempt to install a 64-bit application (x64) on a 32-bit system (x86)).
# PhantomJS from selenium.webdriver import PhantomJS # Django Selenium from django.test import LiveServerTestCase import time # # ( FireFox) #from selenium import webdriver class SeleniumTests(LiveServerTestCase): def test_auth(self): # # webdriver Firefox #br = webdriver.Firefox() # PhantomJS br = PhantomJS() # br.set_window_size(800, 600) # , 'localhost:8081' URL br.get('%s%s' % (self.live_server_url, '/')) # br.find_element_by_xpath('//a[@href="/register/"]').click() # 3 time.sleep(3) # # username 'new' br.find_element_by_id('username').send_keys('new') # email 'new@new.ru' br.find_element_by_id('email').send_keys('new@new.ru') # 2- br.find_element_by_id('password1').send_keys('12345678') br.find_element_by_id('password2').send_keys('12345678') # br.find_element_by_id('btn_register').click() # pis = Myuser.objects.get(username='new') # - pis.is_active = True # pis.save() # br.find_element_by_xpath('//a[@href="/"]').click() # 3 time.sleep(3) # : # br.save_screenshot('screenshot_firstpage.png') # br.find_element_by_id('username').send_keys('new') br.find_element_by_id('password').send_keys('12345678') br.find_element_by_name('').click() # assert br.find_element_by_xpath('//a[@data-content=" "]').text == 'new' # , br.quit()
Source: https://habr.com/ru/post/268385/
All Articles