Skip to content

Instantly share code, notes, and snippets.

View muditlambda's full-sized avatar

Mudit Singh muditlambda

View GitHub Profile
WebElement txt_label = driver.findElement(By.cssSelector("label[id='uname']"));
WebElement txt_label = driver.findElement(withTagName("input").toRightOf(txt_label));
String txt_name = driver.findElement(withTagName("input").toLeftOf(By.id("some_button"))
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Group-Name</groupId>
<artifactId>Artifact-Name</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
WebElement heightUserBox = driver.findElement(withTagName("input")
WebElement heightLabel = driver.findElement(By.xpath("//*[@id='app']/section/form/div/div/h1"));
// Locate the textbox where username should be inputted
WebElement heightUserBox = driver.findElement(withTagName("input")
.below(heightLabel));
heightUserBox.sendKeys("user-name");
WebElement heightPasswordBox = driver.findElement(withTagName("input")
.below(heightUserBox));
heightPasswordBox.sendKeys("password");
package RelativeLocators;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
<ul class="list-unstyled">
<!-- ngRepeat: sampletodo in sampleList.sampletodos --><li ng-repeat="sampletodo in sampleList.sampletodos" class="ng-scope">
<input type="checkbox" ng-model="sampletodo.done" name="li1" class="ng-pristine ng-untouched ng-valid">
<span class="done-false">First Item</span>
</li><!-- end ngRepeat: sampletodo in sampleList.sampletodos --><li ng-repeat="sampletodo in sampleList.sampletodos" class="ng-scope">
<input type="checkbox" ng-model="sampletodo.done" name="li2" class="ng-pristine ng-untouched ng-valid">
<span class="done-false">Second Item</span>
</li><!-- end ngRepeat: sampletodo in sampleList.sampletodos --><li ng-repeat="sampletodo in sampleList.sampletodos" class="ng-scope">
<input type="checkbox" ng-model="sampletodo.done" name="li3" class="ng-pristine ng-untouched ng-valid">
<span class="done-false">Third Item</span>
@Test
public void test_login_using_relative_locators_1(){
String name = driver.findElement(withTagName("input")
.above(By.name("li5"))
.below(By.name("li3")))
.getAttribute("name");
assertEquals(name, "li4");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void test_login_using_relative_locators_2(){
String txt_name = driver.findElement(withTagName("input")
.toLeftOf(By.id("addbutton"))
.below(By.name("li5")))
.getAttribute("id");
assertEquals(txt_name, "sampletodotext");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}