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; import org.testng.annotations.Test; import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; public class RelativeLocators { private WebDriver driver; boolean status = false; @BeforeClass public void setUp(){ System.setProperty("webdriver.chrome.driver","C:\\Location-To\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://4dvanceboy.github.io/lambdatest/lambdasampleapp.html"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @AfterClass public void tearDown() throws Exception { if (driver != null) { driver.quit(); } } @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); } @Test public void test_login_using_relative_locators_3(){ WebElement txt_name = driver.findElement(withTagName("input") .toLeftOf(By.id("addbutton")) .below(By.name("li5"))); txt_name.sendKeys("Relative locators test"); // Get details of the Submit/Add button WebElement submitbutton = driver.findElement(By.xpath("//*[@id=\'addbutton\']")); // Submit the new entry submitbutton.click(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } }