python+selenium實例: 登陸 與文本框輸入內容,保存並返回斷言標題的正確性

#coding: utf-8
import unittest
from selenium import webdriver
import timephp

class LoginCase(unittest.TestCase):css

def setUp(self): #每一個用例執行以前執行
print 'before test'
self.dr = webdriver.Chrome()
self.dr.get('http://localhost/wordpress/wp-login.php')web

# def test_login(self):
# user_name = password = 'admin'
# self.login(user_name, password)
# self.assertTrue('wp-admin' in self.dr.current_url)
# greeting_link = self.by_css('#wp-admin-bar-my-account .ab-item')
# self.assertTrue(user_name in greeting_link.text)wordpress

def test_post(self):
user_name = password = 'admin'
self.login(user_name, password)
title = 'this is title %s' %(time.time())
#加上時間戳,是爲了區分跟上次的內容不同post

self.dr.get('http://localhost/wordpress/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/wordpress/wp-admin/edit.php')
self.assertEqual(self.by_css('.row-title').text, title)
#斷言,返回到列表,判斷第一列的標題內容是否與原輸入的一致。ui


def set_content(self, text):
js = "document.getElementById('content_ifr').contentWindow.document.body.innerText = '%s'" %(text)
#在文本框裏輸入內容
print js
self.dr.execute_script(js)this

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

def by_id(self, the_id):
return self.dr.find_element_by_id(the_id)ip

def by_css(self, css):
return self.dr.find_element_by_css_selector(css)utf-8

def by_name(self, name):
return self.dr.find_element_by_name(name)

def tearDown(self): #每一個用例執行以後
print 'after every test'
self.dr.quit()

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

相關文章
相關標籤/搜索