Selenium 4

Angie Jones

https://angiejones.tech

https://TestAutomationU.com

@techgirl1908

Senior Director, Developer Relations

Applitools

Test Automation University

🚘 Test Driving 🚘

@techgirl1908

@techgirl1908

Relative Locators

@techgirl1908

@techgirl1908

<ul id="productList">

	<!-- Test Automation in the Real World -->
	<li id="pid1" class="ui-li-has-thumb ui-first-child">...</li>

	<!-- Experiences of Test Automation -->
	<li id="pid2" class="ui-li-has-thumb">...</li>

	<!-- Agile Testing -->
	<li id="pid3" class="ui-li-has-thumb">...</li>

	<!-- How Google Tests Software -->
	<li id="pid4" class="ui-li-has-thumb">...</li>

	<!-- Java for Testers -->
	<li id="pid5" class="ui-li-has-thumb">...</li>

	<!-- Advanced Selenium in Java -->
	<li id="pid6" class="ui-li-has-thumb">...</li>

	<!-- The Cucumber for Java Book -->
	<li id="pid7" class="ui-li-has-thumb">...</li>

	<!-- BDD in Action -->
	<li id="pid8" class="ui-li-has-thumb ui-last-child">...</li>
</ul>

@techgirl1908

@techgirl1908

var element = driver.findElement(


);

@techgirl1908

@techgirl1908

var element = driver.findElement(
	withTagName("li").

);

@techgirl1908

@techgirl1908

var element = driver.findElement(
	withTagName("li").
	toLeftOf(By.id("pid3"))
);

@techgirl1908

@techgirl1908

var element = driver.findElement(
	withTagName("li").
	toLeftOf(By.id("pid3"))
);

assertEquals("pid2", element.getAttribute("id"));
ComparisonFailure: 

Expected :pid2
Actual   :pid1

@techgirl1908

@techgirl1908

pid1

pid3

@techgirl1908

var elements = driver.findElements(
        withTagName("li").
        toLeftOf(By.id("pid3"))
);

System.out.println(
        elements.
        stream().
        map(e->e.getAttribute("id")).
        collect(Collectors.toList())
);
 [pid1, pid2, pid5, pid6]

@techgirl1908

@techgirl1908

pid1

pid3

pid2

pid5

pid6

var element = driver.findElement(
        withTagName("li").
        toLeftOf(By.id("pid3")).
        toRightOf(By.id("pid1"))
);

assertEquals("pid2", element.getAttribute("id"));

@techgirl1908

var element = driver.findElement(
        withTagName("li").
        toLeftOf(By.id("pid3")).
        toRightOf(By.id("pid1"))
);

assertEquals("pid2", element.getAttribute("id"));

@techgirl1908

@techgirl1908

@techgirl1908

var element = driver.findElement(
        withTagName("li").
        toLeftOf(By.id("pid3")).
        toRightOf(By.id("pid1"))
);

assertEquals("pid2", element.getAttribute("id"));

@techgirl1908

Things are not always as they appear...

@techgirl1908

@techgirl1908

<div class="view">
    <input class="toggle" type="checkbox">
    <label>goodbye world</label>
    <button class="destroy"></button>
</div>
driver.findElement(
    withTagName("input").
    toLeftOf(By.xpath("//label[text()='goodbye world']")))
    .click();
NoSuchElementException

@techgirl1908

@techgirl1908

@techgirl1908

Use it sparingly

@techgirl1908

Use for interaction,

not verification

@techgirl1908

Questions on

Relative Locators

@techgirl1908

New Window/Tab

@techgirl1908

driver.get("https://applitools.com/");

@techgirl1908

driver.get("https://applitools.com/");
driver.switchTo().newWindow(WindowType.TAB);

@techgirl1908

driver.get("https://applitools.com/");
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://testautomationu.applitools.com");

@techgirl1908

var windows = driver.getWindowHandles();
for(var window : windows){
    driver.switchTo().window(window);
    if("Automated Visual Testing with Visual AI".equals(driver.getTitle())){
        break;
    }
}

@techgirl1908

Questions on

New Windows/Tabs

@techgirl1908

Chrome DevTools Protocol

@techgirl1908

@techgirl1908

@techgirl1908

@techgirl1908

@techgirl1908

@techgirl1908

ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();

@techgirl1908

@techgirl1908

ChromeDriver driver = new ChromeDriver();

DevTools devTools = driver.getDevTools();
devTools.createSession();

devTools.send(Emulation.setGeolocationOverride(
    Optional.of(35.8235),
    Optional.of(-78.8256),
    Optional.of(100))
);

driver.get("https://mycurrentlocation.net/");

@techgirl1908

devtools.send(
        Network.emulateNetworkConditions(
                true,
                250,
                -1,
                -1,
                of(ConnectionType.CELLULAR2G)
        ));

@techgirl1908

How can I

test drive Selenium 4?

@techgirl1908

@techgirl1908

Selenium 4

Angie Jones

https://angiejones.tech

https://TestAutomationU.com

@techgirl1908

Senior Director, Developer Relations

Applitools

Test Automation University

🚘 Test Driving 🚘