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 的提示:

selenium-python编写unittest运行代码时候不执行_第1张图片

我前前后后代码修改了一下午,终于找到方法:

函数

 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就能执行了

selenium-python编写unittest运行代码时候不执行_第2张图片


什么原理现在还不知晓

你可能感兴趣的:(selenium-python编写unittest运行代码时候不执行)