Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 deletions examples/ruby/spec/elements/locators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,100 @@

require 'spec_helper'

RSpec.describe 'Element Locators', skip: 'These are reference following the documentation example' do
RSpec.describe 'Element Locators' do
let(:driver) { start_session }
let(:url) { 'https://www.selenium.dev/selenium/web/locators_tests/locators.html' }

before { driver.get(url) }

it 'finds element by class name' do
driver.find_element(class: 'information')
ele1 = driver.find_element(class: 'information')
expect(ele1).not_to be_nil
expect(ele1.tag_name).to eq('input')
end

it 'finds element by css selector' do
driver.find_element(css: '#fname')
ele = driver.find_element(css: '#fname')
expect(ele).not_to be_nil
expect(ele.attribute('value')).to eq('Jane')
end

it 'finds element by id' do
driver.find_element(id: 'lname')
ele2 = driver.find_element(id: 'lname')
expect(ele2).not_to be_nil
expect(ele2.attribute('value')).to eq('Doe')
end

it 'find element by name' do
driver.find_element(name: 'newsletter')
it 'finds element by name' do
ele3 = driver.find_element(name: 'newsletter')
expect(ele3).not_to be_nil
expect(ele3.tag_name).to eq('input')
end

it 'finds element by link text' do
driver.find_element(link_text: 'Selenium Official Page')
ele = driver.find_element(link_text: 'Selenium Official Page')
expect(ele).not_to be_nil
expect(ele.attribute('href')).to eq('https://www.selenium.dev/')
end

it 'finds element by partial link text' do
driver.find_element(partial_link_text: 'Official Page')
ele = driver.find_element(partial_link_text: 'Official Page')
expect(ele).not_to be_nil
expect(ele.attribute('href')).to eq('https://www.selenium.dev/')
end

it 'finds element by tag name' do
driver.find_element(tag_name: 'a')
ele = driver.find_element(tag_name: 'a')
expect(ele).not_to be_nil
expect(ele.attribute('href')).to eq('https://www.selenium.dev/')
end

it 'finds element by xpath' do
driver.find_element(xpath: "//input[@value='f']")
ele = driver.find_element(xpath: "//input[@value='f']")
expect(ele).not_to be_nil
expect(ele.attribute('type')).to eq('radio')
end

context 'with relative locators' do
let(:url) do
form = <<~HTML
<form>
<label id="lbl-email" for="email">Email</label> <input id="email" type="email"><br>
<label id="lbl-password" for="password">Password</label> <input id="password" type="password"><br>
<button id="cancel" type="button">Cancel</button> <button id="submit" type="button">Submit</button>
</form>
HTML
"data:text/html,#{form}"
end

it 'finds element above' do
driver.find_element({relative: {tag_name: 'input', above: {id: 'password'}}})
ele = driver.find_element(relative: {tag_name: 'input', above: {id: 'password'}})
expect(ele).not_to be_nil
Comment thread
rpallavisharma marked this conversation as resolved.
Resolved
end

it 'finds element below' do
driver.find_element({relative: {tag_name: 'input', below: {id: 'email'}}})
ele = driver.find_element(relative: {tag_name: 'input', below: {id: 'email'}})
expect(ele).not_to be_nil
end

it 'finds element to the left' do
driver.find_element({relative: {tag_name: 'button', left: {id: 'submit'}}})
ele = driver.find_element(relative: {tag_name: 'button', left: {id: 'submit'}})
expect(ele).not_to be_nil
end

it 'finds element to the right' do
driver.find_element({relative: {tag_name: 'button', right: {id: 'cancel'}}})
ele = driver.find_element(relative: {tag_name: 'button', right: {id: 'cancel'}})
expect(ele).not_to be_nil
end

it 'finds near element' do
driver.find_element({relative: {tag_name: 'input', near: {id: 'lbl-email'}}})
ele = driver.find_element(relative: {tag_name: 'input', near: {id: 'lbl-email'}})
expect(ele).not_to be_nil
end

it 'chains relative locators' do
driver.find_element({relative: {tag_name: 'button', below: {id: 'email'}, right: {id: 'cancel'}}})
ele = driver.find_element(relative: {tag_name: 'button', below: {id: 'email'}, right: {id: 'cancel'}})
expect(ele).not_to be_nil
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ In Selenium, we can locate these specific elements using the class name locator.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L20-L21" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L7" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L12" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L6-L8" >}}
Expand Down Expand Up @@ -115,7 +115,7 @@ textbox, using css.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L23-L24" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L11" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L18" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L20-L22" >}}
Expand Down Expand Up @@ -143,7 +143,7 @@ We will identify the Last Name field using it.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L26-L27" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L15" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L24" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L35-L37" >}}
Expand Down Expand Up @@ -172,7 +172,7 @@ We will identify the Newsletter checkbox using it.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L29-L30" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L19" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L30" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L50-L52" >}}
Expand All @@ -199,7 +199,7 @@ In the HTML snippet shared, we have a link available, let's see how will we loca
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L32-L33" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L23" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L36" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L67-L69" >}}
Expand All @@ -225,7 +225,7 @@ based on a portion of their visible text, rather than needing the entire string.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L35-L36" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L27" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L42" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L80-L82" >}}
Expand All @@ -251,7 +251,7 @@ From the above HTML snippet shared, lets identify the link, using its html tag "
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L38-L39" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L31" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L48" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L93-L95" >}}
Expand Down Expand Up @@ -281,7 +281,7 @@ Let's see an example to create the XPath locator for the female radio button.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L41-L42" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L35" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L54" >}}
{{< /tab >}}
{{< tab header="JavaScript" text=true >}}
{{< gh-codeblock path="/examples/javascript/test/elements/locators.spec.js#L106-L108" >}}
Expand Down Expand Up @@ -424,7 +424,7 @@ email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L40" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L72" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let emailLocator = locateWith(By.tagName('input')).above(By.id('password'));
Expand All @@ -451,7 +451,7 @@ password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L44" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L77" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let passwordLocator = locateWith(By.tagName('input')).below(By.id('email'));
Expand All @@ -478,7 +478,7 @@ cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L48" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L82" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let cancelLocator = locateWith(By.tagName('button')).toLeftOf(By.id('submit'));
Expand All @@ -505,7 +505,7 @@ submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L52" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L87" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let submitLocator = locateWith(By.tagName('button')).toRightOf(By.id('cancel'));
Expand Down Expand Up @@ -534,7 +534,7 @@ email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L56" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L92" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let emailLocator = locateWith(By.tagName('input')).near(By.id('lbl-email'));
Expand All @@ -560,7 +560,7 @@ submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_r
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L60" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L97" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let submitLocator = locateWith(By.tagName('button')).below(By.id('email')).toRightOf(By.id('cancel'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ In Selenium, we can locate these specific elements using the class name locator.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L20-L21" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L7" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L12" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand Down Expand Up @@ -113,7 +113,7 @@ textbox, using css.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L23-L24" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L11" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L18" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand Down Expand Up @@ -141,7 +141,7 @@ We will identify the Last Name field using it.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L26-L27" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L15" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L24" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand Down Expand Up @@ -170,7 +170,7 @@ We will identify the Newsletter checkbox using it.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L29-L30" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L19" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L30" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand All @@ -197,7 +197,7 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L32-L33" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L23" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L36" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand All @@ -223,7 +223,7 @@ based on a portion of their visible text, rather than needing the entire string.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L35-L36" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L27" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L42" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand All @@ -249,7 +249,7 @@ From the above HTML snippet shared, lets identify the link, using its html tag "
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L38-L39" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L31" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L48" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand Down Expand Up @@ -279,7 +279,7 @@ Let's see an example to create the XPath locator for the female radio button.
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Elements/LocatorsTest.cs#L41-L42" >}}
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L35" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L54" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let driver = await new Builder().forBrowser('chrome').build();
Expand Down Expand Up @@ -423,7 +423,7 @@ email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L40" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L72" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let emailLocator = locateWith(By.tagName('input')).above(By.id('password'));
Expand All @@ -449,7 +449,7 @@ password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L44" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L77" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let passwordLocator = locateWith(By.tagName('input')).below(By.id('email'));
Expand All @@ -475,7 +475,7 @@ cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L48" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rbL#82" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let cancelLocator = locateWith(By.tagName('button')).toLeftOf(By.id('submit'));
Expand All @@ -501,7 +501,7 @@ submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L52" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L87" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let submitLocator = locateWith(By.tagName('button')).toRightOf(By.id('cancel'));
Expand Down Expand Up @@ -529,7 +529,7 @@ email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L56" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L92" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let emailLocator = locateWith(By.tagName('input')).near(By.id('lbl-email'));
Expand All @@ -554,7 +554,7 @@ submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_r
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
{{< /tab >}}
{{< tab header="Ruby" text=true >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L60" >}}
{{< gh-codeblock path="/examples/ruby/spec/elements/locators_spec.rb#L97" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
let submitLocator = locateWith(By.tagName('button')).below(By.id('email')).toRightOf(By.id('cancel'));
Expand Down
Loading
Loading