1. 程式人生 > >selenium-python編寫unittest執行程式碼時候不執行

selenium-python編寫unittest執行程式碼時候不執行

使用python+ selenium 編寫簡單的自動化指令碼的時候,自己寫出簡單的程式碼如下:
import unittest
from selenium import webdriver
import time
class LoginCase(unittest.TestCase):
    def setUp(self):
        print('before test')
        self.dr = webdriver.Chrome()
        self.dr.get('http://localhost:81/wp-login.php')
    def write(self):
        user_name = 'admin'
        pass_word = '123456'
        self.t_login(user_name, pass_word)
        title = 'this is title %s' % (time.time())
        self.dr.get('http://localhost:81/wp-admin/post-new.php')
        self.by_name('post_title').send_keys(title)
        self.set_content('content,content')
        self.by_name('publish').click()
        self.dr.get('http://localhost:81/wp-admin/edit.php')
        self.assertEqual(self.by_css('.row-title').text, title)
    def set_content(self, text):
        js = "document.getElementById('content_ifr').contentWindow.document.body.innerText = '%s'" % (text)
        self.dr.execute_script(js)
    def t_login(self, user_name, pass_word):
        self.by_id('user_login').send_keys(user_name)
        self.by_id('user_pass').send_keys(pass_word)
        self.by_id('wp-submit').click()
    def by_id(self, id):
        return self.dr.find_element_by_id(id)
    def by_css(self, css):
        return self.dr.find_element_by_css_selector(css)
    def by_name(self, name):
        return self.dr.find_element_by_name(name)
    def t_Down(self):
        self.dr.quit()
if __name__ == '__main__':
    unittest.main()

執行時候無結果無報錯,只有 Ran 0 tests in 0.00s 的提示:


我前前後後程式碼修改了一下午,終於找到方法:

函式

 def write(self):
        user_name = 'admin'
        pass_word = '123456'
        self.t_login(user_name, pass_word)
        title = 'this is title %s' % (time.time())
        self.dr.get('http://localhost:81/wp-admin/post-new.php')
        self.by_name('post_title').send_keys(title)
        self.set_content('content,content')
        self.by_name('publish').click()
        self.dr.get('http://localhost:81/wp-admin/edit.php')
        self.assertEqual(self.by_css('.row-title').text, title)

把這裡的函式在函式名上加上test就能執行了



什麼原理現在還不知曉