(有错误信息)使用unittest编写word press创建文

2018-05-17  本文已影响15人  U一like
from selenium import webdriver
import unittest
import uuid

class CreateEssayInWP(unittest.TestCase):

    def __init__(self, driver, base_url = 'http://xxx'):
        self.driver = driver
        self.domain = base_url

    def setUp(self):
        _url = 'wp-login.php'
        url = self.domain + _url
        self.driver = webdriver.Chrome()
        self.driver.get(url)

    def login(self, username, password):
        self.by_id('user_login').send_keys(username)
        self.by_id('user_pass').send_keys(password)
        self.by_id('wp-submit').click()

    def create_essay(self, content):
        _url = 'post-new.php'
        url = self.domain + _url
        self.driver.get(url)
        title = 'new_log_' + str(uuid.uuid4())
        self.by_id('title').send_keys(title)
        self.body_content(content)
        self.by_id('publish').click()

    def test_create_essay(self):
        username = 'root'
        password = 'Beijing123'
        self.login(username, password)
        self.create_essay('Post New')

        self.driver.get(self.domain + 'wp-admin/edit.php')
        self.asserEqual(self.by_css('.row-title').text , title)


    def body_content(self, content):
        js = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML = '%s'" % (content)
        self.driver.execute_script(js)

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





    
    def by_id(self, the_id): # 元素定位 via id
        return self.driver.find_element_by_id(the_id)

    def by_css(self, the_css): # 元素定位 via css selector
        return self.driver.find_element_by_css_selector(the_css)


if __name__ == '__main__':
    unittest.main()

错误提示:

Traceback (most recent call last):
  File "D:\Learn\pysewp\create_esssy.py", line 61, in <module>
    unittest.main()
  File "D:\Software\Python\lib\unittest\main.py", line 95, in __init__
    self.runTests()
  File "D:\Software\Python\lib\unittest\main.py", line 256, in runTests
    self.result = testRunner.run(self.test)
  File "D:\Software\Python\lib\unittest\runner.py", line 176, in run
    test(result)
  File "D:\Software\Python\lib\unittest\suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "D:\Software\Python\lib\unittest\suite.py", line 122, in run
    test(result)
  File "D:\Software\Python\lib\unittest\suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "D:\Software\Python\lib\unittest\suite.py", line 122, in run
    test(result)
  File "D:\Software\Python\lib\unittest\case.py", line 653, in __call__
    return self.run(*args, **kwds)
  File "D:\Software\Python\lib\unittest\case.py", line 580, in run
    testMethod = getattr(self, self._testMethodName)
AttributeError: 'CreateEssayInWP' object has no attribute '_testMethodName'
[Finished in 0.4s]

这里出现的问题是由于在类中定义了__init__方法, 相当于是在对父类的__init__方法进行重写, 故会报错.
最简单的解决方式就是不要在unittest类中添加__init__方法

上一篇下一篇

猜你喜欢

热点阅读