Created
October 19, 2013 03:23
-
-
Save easonhan007/7051335 to your computer and use it in GitHub Desktop.
edit_post_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding: utf-8 | |
require 'watir-webdriver' | |
describe 'Create Post' do | |
before :all do | |
puts 'start browser' | |
@b = Watir::Browser.new :chrome | |
# 如何在watir里面使用原生的webdriver | |
#@b.wd == Selenium::WebDriver.for(:chrome) | |
@password = @user_name = 'admin' | |
end | |
before :each do | |
login(@b, @user_name, @password) | |
end | |
=begin | |
it 'should login successfully' do | |
# String#include | |
@b.title.should include('仪表盘') | |
@b.link(:href, 'http://localhost/wordpress/wp-admin/profile.php').text.should include(@user_name) | |
end | |
it 'should create post successfully' do | |
post_title = "my post created at #{Time.now.to_s}" | |
content = 'this is my post' | |
create_post(@b, post_title, content) | |
@b.div(:id, 'message').text.should include('文章已发布') | |
end | |
it 'should create post successfully and verify post list page' do | |
post_title = "my post created at #{Time.now.to_s}" | |
content = 'this is my post' | |
create_post(@b, post_title, content) | |
# 跳转到文章列表页面 | |
goto_list_page(@b) | |
@b.table(:class, 'wp-list-table')[2][1].text.should eql(post_title) | |
end | |
it 'should move all posts to garbige' do | |
post_title = "my post created at #{Time.now.to_s}" | |
content = 'this is my post' | |
create_post(@b, post_title, content) | |
# 跳转到文章列表页面 | |
delete_all_posts(@b) | |
@b.table(:class, 'wp-list-table').rows.size.should eql(3) | |
end | |
=end | |
it 'edit post successfully' do | |
# 创建一篇文章 | |
post_title = 'my post' | |
content = 'this is post' | |
create_post(@b, post_title, content) | |
# 查找这篇文章 | |
title_after_edit = 'after edit' | |
goto_list_page(@b) | |
@b.link(:text, post_title).click | |
# 修改这篇文章 | |
@b.text_field(:id, 'title').set title_after_edit | |
@b.button(:id, 'publish').click | |
# 查找这篇文章 | |
goto_list_page(@b) | |
# 断言 | |
begin | |
# should 和 should_not | |
@b.link(:text, title_after_edit).should be_exist # link.exist? is true | |
@b.link(:text, title_after_edit).exist?.should be_true | |
ensure | |
delete_all_posts(@b) | |
end | |
# cleanup | |
end | |
def login(browser, user_name, password) | |
login_url = 'http://localhost/wordpress/wp-login.php' | |
browser.goto login_url | |
browser.text_field(:id, 'user_login').set user_name | |
browser.text_field(:id, 'user_pass').set password | |
browser.button(:id, 'wp-submit').click | |
end | |
def goto_list_page(browser) | |
list_url = 'http://localhost/wordpress/wp-admin/edit.php' | |
browser.goto list_url | |
end | |
def create_post(browser, post_title, content) | |
create_post_url = 'http://localhost/wordpress/wp-admin/post-new.php' | |
browser.goto create_post_url | |
browser.text_field(:name, 'post_title').when_present.set post_title | |
script = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML='#{content}'" | |
browser.wd.execute_script(script) | |
browser.button(:name, 'publish').when_present.click | |
end | |
def delete_all_posts(browser) | |
list_url = 'http://localhost/wordpress/wp-admin/edit.php' | |
browser.goto list_url | |
browser.checkbox(:id, 'cb-select-all-1').set true | |
browser.select(:name, 'action').options.last.click | |
browser.button(:id, 'doaction').click | |
end | |
after :all do | |
@b.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment