Run Selenium automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package day3;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.openqa.selenium.support.locators.RelativeLocator.RelativeBy;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
import static org.openqa.selenium.By.id;
public class LearnLocators {
public static void main(String[] args) {
// Set the ChromeDriver Exe Path
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
// Launch your browser
ChromeDriver driver = new ChromeDriver();
//
driver.get("http://leafground.com/pages/sorttable.html");
// Find the new way
// WebElement thisElement = driver.findElement(RelativeLocator.withTagName("button").above(By.xpath("//*[text()='Get Position']")));
/*WebElement thisElement = driver.findElement(
RelativeLocator.withTagName("button").
below(By.xpath("//*[text()='Get Position']")));*/
/* driver.findElement(id(""));
*/
/*driver.findElement(RelativeBy.);
*/
/*List<WebElement> allBelowElements = driver.findElements(
RelativeLocator.withTagName("*").
below(By.xpath("//*[text()='Get Position']")));
// Print the text of the element
for (WebElement thisElement : allBelowElements) {
System.out.println(thisElement.getText());
}
*/
/*List<WebElement> belowElements = driver.findElements(RelativeLocator
.withTagName("input")
.above(By.xpath("//label[text()='Clear the text']")));
System.out.println(belowElements.size());
for (WebElement ele : belowElements) {
System.out.println(ele.getAttribute("name"));
ele.clear();
}*/
/*List<WebElement> elements = driver.findElements(RelativeLocator
.withTagName("td")
.toRightOf(By.xpath("//td[text()='100%']")));
System.out.println(elements.size());
for (WebElement ele : elements) {
System.out.println(ele.getText());
}*/
List<WebElement> belowElements = driver.findElements(RelativeLocator
.withTagName("td")
.toRightOf(By.xpath("//td[text()='1001']")));
System.out.println(belowElements.size());
for (WebElement ele : belowElements) {
System.out.println(ele.getText());
}
}
}
package pages;
import org.openqa.selenium.*;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.locators.RelativeLocator;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
public class ScaleFocusHomePage {
protected WebDriver driver;
public ScaleFocusHomePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// SELECTORS
@FindBy(css = ".icons-menu .icon-share.open-share")
protected WebElement shareNavigationIcon;
@FindBy(className = "search-container")
protected WebElement searchContainer;
@FindBy(css = ".gtm-hp-vmbtn-serv.view-more-btn")
protected WebElement servicesViewMoreButton;
@FindBy(css = ".desciprtion .h3-p")
protected WebElement descriptionServicesParagraph;
// METHODS
public void clickSearchIconFromNavigation() {
// Click to right of Careers in the Navigation drop down menu
driver.findElement(RelativeLocator.withTagName("i")
.toLeftOf(shareNavigationIcon))
.click();
}
public String getSearchContainerAttribute(String attribute) {
// Return element's passed attribute
return searchContainer.getAttribute(attribute);
}
public void clickCloudServicesViewMoreButton() {
// To remove "RelativeLocator." use: import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
driver.findElement(withTagName("span")
.toRightOf(servicesViewMoreButton)
.below(descriptionServicesParagraph))
.click();
}
}
package io.testproject.blog11selenium4relativelocators;
import static org.openqa.selenium.support.locators.RelativeLocator.*;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.testng.annotations.Test;
public class DifferentImportStatements {
WebDriver driver;
@Test
public void useRelativeLocatorInSyntax () {
driver.findElement(RelativeLocator.withTagName(""));
}
@Test
public void skipRelativeLocatorInSyntax () {
driver.findElement(withTagName(""));
}
}
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Warning: (143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)' is deprecated
v4.0.0-alpha-3
==============
* Add "relative" locators. The entry point is through the `RelativeLocator`.
Usage is like `driver.findElements(withTagName("p").above(lowest));`
* Add chromedriver cast APIs to remote server (#7282)
* `By` is now serializable over JSON.
* Add ApplicationCache, Fetch, Network, Performance, Profiler,
ResourceTiming, Security and Target CDP domains.
* Fixing Safari initialization code to be able to use Safari Technology
Preview.
* Ensure that the protocol converter handles the new session responses
properly.
* Expose devtools APIs from chromium derived drivers.
* Expose presence of devtools support on a role-based interface
* Move to new Grid, deleting the old standalone server and grid implementation.
* Switch to using `HttpHandler` where possible. This will impact projects that
are extending Selenium Grid.
* Respect "webdriver.firefox.logfile" system property in legacy Firefox driver.
Fixes #6649
* Back out OpenCensus support: OpenTracing and OpenCensus are merging, so
settle on one for now.
* Only allow CORS when using a —allow-cors flag in the Grid server
* If you're using the Java Platform Module System, all modules
associated with the project are generated as "open" modules. This
will change in a future release.
* The version of Jetty being used is unshadowed.
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
import java.time.Duration;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
@Deprecated
public WebDriverWait(WebDriver driver, long timeoutInSeconds) {
this(driver, Duration.ofSeconds(timeoutInSeconds));
}
new WebDriverWait(driver, Duration.ofSeconds(10));
By locatorBase = By.xpath("./div/div[contains(@class,'listRoot')]";
By relativeLocator = getNthZElementLocator("2");
ByChanged chainedLocator = new ByChained(locatorBase, relativeLocator);
String XPATH_Z_ELEMENT_STRING = ".//ul/li[%s]";
public By getNthZElementLocator(String replaceArg)
{
return By.xpath(String.format(XPATH_Z_ELEMENT_STRING, replaceArg));
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.