2016-08-12 59 views
2

我想要的功能測試send_keys不Django的測試工作

功能測試過程

  1. Create_user
  2. 使用創建的用戶
  3. 點擊一個名爲按鈕登錄網站booksale

myproject/functional_test/test_booksale.py

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from django.test import LiveServerTestCase 

from users.models import User 


class OurClientLogInTest(LiveServerTestCase): 

    def setUp(self): 
     self.browser = webdriver.Firefox() 
     ## create 1 user id 
     User.objects.create_user(username='MyTestID', password='123', nickname='NICKNAMETEST', phone='01011111111') 


    def tearDown(self): 
     self.browser.quit() 

    def test_our_client_can_log_in_this_website(self): 

     # url connect 
     self.browser.get(self.live_server_url) 

     # web page header title has 'Korea book' 
     self.assertIn('Korea book', self.browser.title) 

     # click log_in button in main page 
     log_in = self.browser.find_element_by_id('log_in') 
     log_in.send_keys(Keys.ENTER) 
     self.assertIn('로그인', self.browser.title) 

     # insert username, password 
     # click enter 
     username_box = self.browser.find_element_by_name('username') 
     password_box = self.browser.find_element_by_name('password') 

     username_box.send_keys('MyTestID') 
     password_box.send_keys('123') 

     self.browser.find_element_by_id('button').click() 

     ##redirect main page 
     self.assertIn('Korea book', self.browser.title) 

     ## click a button named book_sale 
     book_sale = self.browser.find_element_by_id('book_sale') 
     book_sale.send_keys(Keys.ENTER) 

     from IPython import embed; embed() 

我命令python manage.py functional_test

它告訴我錯誤

raceback (most recent call last): 
    File "/Users/hanminsoo/.pyenv/versions/3.5.1/lib/python3.5/wsgiref/handlers.py", line 137, in run 
self.result = application(self.environ, self.start_response) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/test/testcases.py", line 1198, in __call__ 
return super(FSFilesHandler, self).__call__(environ, start_response) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 177, in __call__ 
response = self.get_response(request) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/test/testcases.py", line 1181, in get_response 
return self.serve(request) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/test/testcases.py", line 1193, in serve 
return serve(request, final_rel_path, document_root=self.get_base_dir()) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/views/static.py", line 54, in serve 
fullpath = os.path.join(document_root, newpath) 
    File "/Users/hanminsoo/.pyenv/versions/3.5.1/lib/python3.5/posixpath.py", line 89, in join 
genericpath._check_arg_types('join', a, *p) 
    File "/Users/hanminsoo/.pyenv/versions/3.5.1/lib/python3.5/genericpath.py", line 143, in _check_arg_types 
(funcname, s.__class__.__name__)) from None 
TypeError: join() argument must be str or bytes, not 'NoneType' 

當我刪除代碼

book_sale.send_keys(Keys.ENTER) 

測試通過的很清楚

我無法不明白爲什麼book_sale按鈕不發送回車鍵..

請一些身體幫我

+2

錯誤並不在此代碼,但在當表單提交 – e4c5

+1

如果發生了什麼'book_sale'的按鈕你爲什麼不使用'book_sale.click()的''而不是send_keys()'? –

+0

@ e4c5 ummm ,,,,我不能站在你的評論',但在提交表格時會發生什麼...... ...你能告訴我一個更容易的建議嗎? –

回答

1

如果book_sale元素是按鈕,你應該嘗試使用.click()代替send_keys()爲: -

book_sale.click() 
+1

謝謝@Saurahb Gaur我用你的解決方案解決了我的問題! –

+1

@MinHan您好,我在您提供的代碼中看到,您正在使用'.send_keys(Keys.ENTER)'執行單擊操作的每個位置,並且需要使用'.click()'替換它。..: ) –

+0

哦,,,謝謝!!我會接受你的好建議謝謝你! –