New to Selenium. Been doing some activity but getting stuck every now and then. Plus if there is any error, at times it is not showing me the error. Below is my query.
Address Book is a page that stores address.
URL: http://webapps.tekstac.com/AddressBook/
This is the procedure: Invoke the driver using getWebDriver() method defined in DriverSetup(). Navigate to "http://webapps.tekstac.com/AddressBook/". Identify ancestor of the 'Nick Name' label text. That is the ancestor 'div' of the form. Get the text of this ancestor and store it in a static variable fName.
This is the code for which I am 66.67% evaluated.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class NameLocator //DO NOT Change the class name
{
//Use the declared variables for stroing required values
static String fName;
static String baseUrl;
static WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
DriverSetup ds = new DriverSetup();
return ds.getWebDriver();
/* Replace this comment by the code statement to create and return the driver */
/* Naviaget to the url 'http://webapps.tekstac.com/AddressBook/' */
}
public String getNameLocator() //DO NOT Change the method Signature
{
WebElement element = driver.findElement(By.xpath("//div[.//[text()='NickName']]/ancestor::div"));
fName = element.getText();
driver.close();
return fName;
/*Using the driver, Find the element ancestor and its text and assign the text to 'fName' */
/*Close the driver*/
}
public static void main(String[] args)
{
NameLocator namLocator=new NameLocator();
//Add required code here
}
}
Error while compiling: Ancestor's div text is not correct.
THIS IS HOW THE ORIGINAL TEMPLATE LOOKED BEFORE I STARTED FILLING UP THE CODE. THIS IS HOW THEY HAVE DESIGNED THE ACTIVITY AND THE COMMENTS WERE NOT MANUALLY ENTERED BY ME.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class NameLocator //DO NOT Change the class name
{
//Use the declared variables for stroing required values
static String fName;
static WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
/* Replace this comment by the code statement to create and return the driver */
/* Naviaget to the url 'http://webapps.tekstac.com/AddressBook/' */
}
public String getNameLocator() //DO NOT Change the method Signature
{
/*Using the driver, Find the element ancestor and its text and assign the text to 'fName' */
/*Close the driver*/
}
public static void main(String[] args)
{
NameLocator namLocator=new NameLocator();
//Add required code here
}
}
CodePudding user response:
Code according to the comments. Screenshot with result is here image link:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class NameLocator {
static String fName;
static WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
/* Replace this comment by the code statement to create and return the driver */
/* Naviaget to the url 'http://webapps.tekstac.com/AddressBook/' */
System.setProperty("webdriver.chrome.driver", "d:\\Downloads\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://webapps.tekstac.com/AddressBook/");
return driver;
}
public String getNameLocator() //DO NOT Change the method Signature
{
/*Using the driver, Find the element ancestor and its text and assign the text to 'fName' */
/*Close the driver*/
WebElement element = driver.findElement(By.xpath("//*[text()='NickName']/ancestor::div"));
fName = element.getText();
driver.quit();
return fName;
}
public static void main(String[] args) {
NameLocator namLocator = new NameLocator();
//Add required code here
namLocator.setupDriver();
namLocator.getNameLocator();
System.out.println(fName);
}
}