Mark
Mark

Reputation: 11

RSpec/Ruby verify a value exists in text field

For example purposes, lets use google. Let us also assume in the search bar on google we enter texts that reads, "hello". Lastly lets assume the search bar has a save button and we select it to store the data in the field. So now, when you look at the search bar it reads, "hello".

I want to go back into the browser navigate to google and check that text field to ensure it saved as expected - my data that is. I know how to navigate there. But can someone explain to me in Rspec/Ruby how to go into a browser, find the web element you want to verify the value by way of id, name, xpath, etc....and write a command using selenium webdriver with Rspec/Ruby to do so. NO CAPYBARA.

Upvotes: 1

Views: 1172

Answers (1)

orde
orde

Reputation: 5273

After entering the search term, you can get the value (e.g. element['value']) from the text field, and use an rspec expectation to validate that the correct string has been entered. For example:

#foo_spec.rb

require 'selenium-webdriver'
require 'rspec'

describe "Contrived Example" do

  it "enters a search term" do

    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://google.com"

    element = driver.find_element(:name, 'q')
    element.send_keys "test string"
    sleep 1
    expect(element['value']).to eq "test string"
    driver.quit

  end

end

Upvotes: 1

Related Questions