Skip to content
Merged
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
88 changes: 87 additions & 1 deletion examples/dotnet/SeleniumDocs/Elements/FindersTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;

namespace SeleniumDocs.Elements
{
[TestClass]
public class FindersTest : BaseTest
{
private const string LocatorsPage = "https://www.selenium.dev/selenium/web/locators_tests/locators.html";

[TestMethod]
public void FindsFirstMatchingElement()
{
StartDriver();
driver.Url = LocatorsPage;
IWebElement firstInput = driver.FindElement(By.ClassName("information"));

Assert.AreEqual("fname", firstInput.GetAttribute("id"));
}

[TestMethod]
public void FindsElementWithinASubsetOfTheDom()
{
StartDriver();
driver.Url = LocatorsPage;
IWebElement form = driver.FindElement(By.TagName("form"));
IWebElement input = form.FindElement(By.ClassName("information"));

Assert.AreEqual("fname", input.GetAttribute("id"));
}

[TestMethod]
public void UsesAnOptimizedLocator()
{
StartDriver();
driver.Url = LocatorsPage;
IWebElement input = driver.FindElement(By.CssSelector("form .information"));

Assert.AreEqual("fname", input.GetAttribute("id"));
}

[TestMethod]
public void FindsAllMatchingElements()
{
StartDriver();
driver.Url = LocatorsPage;
var inputs = driver.FindElements(By.TagName("input"));

Assert.IsTrue(inputs.Count > 1);
}

[TestMethod]
public void GetsElementFromACollection()
{
StartDriver();
driver.Url = LocatorsPage;

var elements = driver.FindElements(By.TagName("p"));
foreach (var element in elements)
{
System.Console.WriteLine("Paragraph text:" + element.Text);
}

Assert.IsTrue(elements.Count > 0);
}

[TestMethod]
public void FindsElementsFromElement()
{
StartDriver();
driver.Url = LocatorsPage;

IWebElement form = driver.FindElement(By.TagName("form"));
var elements = form.FindElements(By.TagName("input"));
foreach (var e in elements)
{
System.Console.WriteLine(e.GetAttribute("value"));
}

Assert.IsTrue(elements.Count > 0);
}

[TestMethod]
public void GetsActiveElement()
{
StartDriver();
driver.Url = LocatorsPage;

driver.FindElement(By.CssSelector("#fname")).SendKeys("webElement");
string attr = driver.SwitchTo().ActiveElement().GetAttribute("name");

Assert.AreEqual("fname", attr);
}
}
}
}
86 changes: 86 additions & 0 deletions examples/java/src/test/java/dev/selenium/elements/FindersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,92 @@

import dev.selenium.BaseTest;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FindersTest extends BaseTest {

private static final String LOCATORS_PAGE =
"https://www.selenium.dev/selenium/web/locators_tests/locators.html";

@Test
public void findsFirstMatchingElement() {
startChromeDriver();
driver.get(LOCATORS_PAGE);
WebElement firstInput = driver.findElement(By.className("information"));

assertEquals("fname", firstInput.getAttribute("id"));
}

@Test
public void findsElementWithinASubsetOfTheDom() {
startChromeDriver();
driver.get(LOCATORS_PAGE);
WebElement form = driver.findElement(By.tagName("form"));
WebElement input = form.findElement(By.className("information"));

assertEquals("fname", input.getAttribute("id"));
}

@Test
public void usesAnOptimizedLocator() {
startChromeDriver();
driver.get(LOCATORS_PAGE);
WebElement input = driver.findElement(By.cssSelector("form .information"));

assertEquals("fname", input.getAttribute("id"));
}

@Test
public void findsAllMatchingElements() {
startChromeDriver();
driver.get(LOCATORS_PAGE);
List<WebElement> inputs = driver.findElements(By.tagName("input"));

assertTrue(inputs.size() > 1);
}

@Test
public void getsElementFromACollection() {
startChromeDriver();
driver.get(LOCATORS_PAGE);

List<WebElement> elements = driver.findElements(By.tagName("p"));
for (WebElement element : elements) {
System.out.println("Paragraph text:" + element.getText());
}

assertTrue(elements.size() > 0);
}

@Test
public void findsElementsFromElement() {
startChromeDriver();
driver.get(LOCATORS_PAGE);

WebElement form = driver.findElement(By.tagName("form"));
List<WebElement> elements = form.findElements(By.tagName("input"));
for (WebElement e : elements) {
System.out.println(e.getAttribute("value"));
}

assertTrue(elements.size() > 0);
}

@Test
public void getsActiveElement() {
startChromeDriver();
driver.get(LOCATORS_PAGE);

driver.findElement(By.cssSelector("#fname")).sendKeys("webElement");
String attr = driver.switchTo().activeElement().getAttribute("name");

assertEquals("fname", attr);
}
}
78 changes: 78 additions & 0 deletions examples/javascript/test/elements/finders.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const {Builder, By} = require('selenium-webdriver');
const assert = require('assert');

const LOCATORS_PAGE = 'https://www.selenium.dev/selenium/web/locators_tests/locators.html';

describe('Finders', function () {
it('finds the first matching element', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const firstInput = await driver.findElement(By.className('information'));

assert.equal(await firstInput.getAttribute('id'), 'fname');
await driver.quit();
});

it('finds an element within a subset of the DOM', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const form = await driver.findElement(By.tagName('form'));
const input = await form.findElement(By.className('information'));

assert.equal(await input.getAttribute('id'), 'fname');
await driver.quit();
});

it('uses an optimized locator', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const input = await driver.findElement(By.css('form .information'));

assert.equal(await input.getAttribute('id'), 'fname');
await driver.quit();
});

it('finds all matching elements', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const inputs = await driver.findElements(By.tagName('input'));

assert.ok(inputs.length > 1);
await driver.quit();
});

it('gets an element from a collection', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const elements = await driver.findElements(By.tagName('p'));
for (const element of elements) {
console.log('Paragraph text:' + await element.getText());
}

assert.ok(elements.length > 0);
await driver.quit();
});

it('finds elements from an element', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
const form = await driver.findElement(By.css('form'));
const elements = await form.findElements(By.css('input'));
for (const e of elements) {
console.log(await e.getAttribute('value'));
}

assert.ok(elements.length > 0);
await driver.quit();
});

it('gets the active element', async function () {
let driver = await new Builder().forBrowser('chrome').build();
await driver.get(LOCATORS_PAGE);
await driver.findElement(By.css('#fname')).sendKeys('webElement');
const attr = await driver.switchTo().activeElement().getAttribute('name');

assert.equal(attr, 'fname');
await driver.quit();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dev.selenium.elements

import dev.selenium.BaseTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.openqa.selenium.By
import java.time.Duration

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class FindersTest : BaseTest() {

private val locatorsPage = "https://www.selenium.dev/selenium/web/locators_tests/locators.html"

@Test
fun findsFirstMatchingElement() {
driver.get(locatorsPage)
val firstInput = driver.findElement(By.className("information"))

assertEquals("fname", firstInput.getAttribute("id"))
}

@Test
fun findsElementWithinASubsetOfTheDom() {
driver.get(locatorsPage)
val form = driver.findElement(By.tagName("form"))
val input = form.findElement(By.className("information"))

assertEquals("fname", input.getAttribute("id"))
}

@Test
fun usesAnOptimizedLocator() {
driver.get(locatorsPage)
val input = driver.findElement(By.cssSelector("form .information"))

assertEquals("fname", input.getAttribute("id"))
}

@Test
fun findsAllMatchingElements() {
driver.get(locatorsPage)
val inputs = driver.findElements(By.tagName("input"))

assertTrue(inputs.size > 1)
}

@Test
fun getsElementFromACollection() {
driver.get(locatorsPage)
val elements = driver.findElements(By.tagName("p"))
for (element in elements) {
println("Paragraph text:" + element.text)
}

assertTrue(elements.isNotEmpty())
}

@Test
fun findsElementsFromElement() {
driver.get(locatorsPage)
val form = driver.findElement(By.tagName("form"))
val elements = form.findElements(By.tagName("input"))
for (e in elements) {
println(e.getAttribute("value"))
}

assertTrue(elements.isNotEmpty())
}

@Test
fun getsActiveElement() {
driver.get(locatorsPage)
driver.findElement(By.cssSelector("#fname")).sendKeys("webElement")
val attr = driver.switchTo().activeElement().getAttribute("name")

assertEquals("fname", attr)
}

@BeforeEach
fun configureImplicitWait() {
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500))
}
}
Loading
Loading