Skip to content

Moved javascript code examples for element locators into test files #2312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed the tests to fetch the same element as the tests in other progr…
…amming languages
  • Loading branch information
SuperDXCEL committed May 19, 2025
commit 016e75ee5f7d7f2a44ebe6cbb6a668cdcb6ce516
51 changes: 26 additions & 25 deletions examples/javascript/test/elements/locators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('Element Locator Test', function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.className('information'));

const tag = await element.getTagName();
const id = await element.getAttribute("id");
const value = await element.getAttribute("value");
Expand Down Expand Up @@ -50,69 +49,71 @@ describe('Element Locator Test', function () {
it('Check if element can be found by name', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.name("gender"));
const element = await driver.findElement(By.name("newsletter"));

const tag = await element.getTagName();
const type = await element.getAttribute("type");
const value = await element.getAttribute("value");

assert.equal(tag, "input");
assert.equal(type, "radio");
assert.equal(value, "m");
assert.equal(type, "checkbox");
assert.equal(value, "1");
await driver.quit();
});

it('Check if element can be found by xpath', async function () {


it('Check if element can be found by link text', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.xpath('//input[@name="newsletter"]'));
const element = await driver.findElement(By.linkText("Selenium Official Page"));

const tag = await element.getTagName();
const type = await element.getAttribute("type");
const value = await element.getAttribute("value");
const href = await element.getAttribute("href");

assert.equal(tag, "input");
assert.equal(type, "checkbox");
assert.equal(value, "1");
assert.equal(tag, "a");
assert.equal(href, "https://www.selenium.dev/");
await driver.quit();
});

it('Check if element can be found by tag name', async function () {
it('Check if element can be found by partial link text', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.tagName("h2"));
const element = await driver.findElement(By.partialLinkText("Official Page"));

const tag = await element.getTagName();
const text = await element.getText();
const href = await element.getAttribute("href");

assert.equal(tag, "h2");
assert.equal(text, "Contact Selenium");
assert.equal(tag, "a");
assert.equal(href, "https://www.selenium.dev/");
await driver.quit();
});

it('Check if element can be found by link text', async function () {
it('Check if element can be found by tag name', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.linkText("Selenium Official Page"));
const element = await driver.findElement(By.tagName("a"));

const tag = await element.getTagName();
const href = await element.getAttribute("href");
const text = await element.getText();

assert.equal(tag, "a");
assert.equal(href, "https://www.selenium.dev/");
assert.equal(text, "Selenium Official Page");
await driver.quit();
});

it('Check if element can be found by partial link text', async function () {
it('Check if element can be found by xpath', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');
const element = await driver.findElement(By.partialLinkText("Official Page"));
const element = await driver.findElement(By.xpath('//input[@value="f"]'));

const tag = await element.getTagName();
const href = await element.getAttribute("href");
const type = await element.getAttribute("type");
const value = await element.getAttribute("value");

assert.equal(tag, "a");
assert.equal(href, "https://www.selenium.dev/");
assert.equal(tag, "input");
assert.equal(type, "radio");
assert.equal(value, "f");
await driver.quit();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ textbox, using css.
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L11" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L21-L23" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L20-L22" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -153,7 +153,7 @@ We will identify the Last Name field using it.
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L15" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L36-L38" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L35-L37" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -184,7 +184,7 @@ We will identify the Newsletter checkbox using it.
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L19" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L51-L53" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L50-L52" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -213,7 +213,7 @@ In the HTML snippet shared, we have a link available, let's see how will we loca
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L23" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L94-L96" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L67-L69" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -243,7 +243,7 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L27" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L106-L108" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L80-L82" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -271,7 +271,7 @@ From the above HTML snippet shared, lets identify the link, using its html tag "
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L31" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L81-L83" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L93-L95" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
val driver = ChromeDriver()
Expand Down Expand Up @@ -305,7 +305,7 @@ first name text box. Let us create locator for female radio button using xpath.
{{< gh-codeblock path="examples/ruby/spec/elements/locators_spec.rb#L35" >}}
{{< /tab >}}
{{< tab header="JavaScript" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L66-L68" >}}
{{< gh-codeblock path="examples/javascript/test/elements/locators.spec.js#L106-L108" >}}
{{< /tab >}}
{{< tab header="Kotlin" >}}
import org.openqa.selenium.By
Expand Down