I have one method called click button where I'll be having multiple try catches containing X-path, so how will get to know which X-path is executed recently by the driver?
public void actions(String action, String param, Webdriver driver){
switch(action){
case "Click Button":
clickButton(driver, param);
}
}
public void clickButton(Webdriver driver, String param){
try{
if (param.equalsIgnoreCase("Search")) {
WebElement waittext = driver.findElement(By.xpath(("xpath for search button")));
Actions actions = new Actions(driver);
actions.moveToElement(waittext).build().perform();
waittext.click();
return;
}catch(Exception e){
System.out.println(e);
}
try{
if (param.equalsIgnoreCase("Save")) {
WebElement waittext = driver.findElement(By.xpath(("xpath for save button")));
Actions actions = new Actions(driver);
actions.moveToElement(waittext).build().perform();
waittext.click();
return;
}catch(Exception e){
System.out.println(e);
}
so on....
}
Like this, I'll be having so many try catches. My first thought was to return the X-path with return in every try catch but the changes will be too much for me and it will take a lot of time, so I was thinking is there any way where I can just simply get the last or recent X-path through the driver.
public void actions(String action, String param, Webdriver driver){
switch(action){
case "Click Button":
clickButton(driver, param);
String recentlyUsedXpath = driver.getLastXpath(); //something like this I needed here.
}
}
I know driver will not have any method like this: driver.getLastXpath(), but I was hoping if there is anything similar or any way where I can get the recent X-path.
You are calling 'clickButton()' method from 'actions()' method. In 'actions()' method, add another parameter for XPath and pass that parameter to 'clickButton()' method like this:
In 'clickButton()' method pass that xpath to the 'findElement' statement, then add an ArrayList to store the xpaths and try to minimize the number of 'if' conditions also, for ex.,
If you need the last xpath used, then you can get that from 'xpathList' arraylist by using index.