我正在尝试修复旧的自动化测试脚本以进行工作。我是编程的新手,我已经走到了这一步:
# Check if I can play some game
Then(/^I should be able to play (.+)$/) do |game_name|
# TODO: better check for game play, game end, score count
if page.has_content?("GUEST")
find(:css, ".play", :text => "Play").click
else
start_game(game_name)
end
#Here is where the error pops up:
if page.has_content?('Writing')
# Dont wait for players to join
expect(page.has_content?('Waiting for players')).to eq(true)
else
# Check for game object
page.should have_css("object#game")
# Check if correct game loading
current_url.should match(/#{GameWorld::GAMES[game_name]}/)
end
#Quick escape
ensure_on?('city')
end
有人能给我一些如何解决问题的提示吗?
我得到的错误是:
`Error: Permission denied to access property "textContent" (Selenium::WebDriver::Error::JavascriptError)` .
如果需要更多信息,请与我联系。
任何提高的方法都会很棒。另外,我接受关于如何自动进行健全性测试的所有建议。
答案 0 :(得分:0)
如果不确切知道您正在使用哪个版本,很难确切地说出导致错误的原因是什么,但我猜想升级到最新版本的Capybara可能会修复该错误。除此之外,您的测试中还有一些需要改进的地方。
has_xxx?
方法具有内置的等待行为,当您期望他们检查的内容在95 +%的时间内发生时非常有用,但如果它更像50 / 50然后你的测试比它需要的慢。
永远不要对has_xxx?
方法的结果使用期望,而只是使用have_xxx
匹配器,因为错误消息在出现故障时会更具描述性和实用性
您绝不应将current_url
/ current_path
与eq
/ match
匹配器一起使用,而应使用have_current_path匹配器。由于内置的重试行为,这将使您的测试更加稳定。
不要混合期望和语法,这会导致难以阅读/理解测试。
总而言之,你的测试应该更像是
# Check if I can play some game
Then(/^I should be able to play (.+)$/) do |game_name|
# TODO: better check for game play, game end, score count
expect(page).to have_content(game_name) # This should be something that is on the page for both GUEST and logged in users just to verify the page has loaded
if page.has_content?("GUEST", wait: false) #disable the waiting/retrying behavior since we now know the page is already loaded
find(:css, ".play", :text => "Play").click
else
start_game(game_name)
end
expect(page).to have_content('Something') # Same as above - check for something that will be on the page when the actions triggered by the `click` or `start_game` calls above have finished
if page.has_content?('Writing', wait: false) #disable waiting because previous line has assured page is loaded
# Dont wait for players to join
expect(page).to have_content('Waiting for players')
else
# Check for game object
expect(page).to have_css("object#game")
# Check if correct game loading
expect(page).to have_current_path(/#{GameWorld::GAMES[game_name]}/)
end
#Quick escape
ensure_on?('city')
end