0

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.

4
  • Do you have a problem with storing each xpath string in a variable? Commented Sep 17, 2022 at 11:12
  • No not at all, it's just if I start storing each of the x-path in a variable then the changes will be too much for me and it will take around 7 to 8 days to make all those changes as the code itself contains 3k+ X-path whereas the actions are only 100. Commented Sep 17, 2022 at 11:20
  • So basically, you want to programmatically change all the xpaths? Commented Sep 17, 2022 at 11:21
  • Not to change, just I wanted to get the X-path. for example, I have one flow in which clickButton action will be there but what I didn't know is which param has passed for the clickButton if it's Save or Search, but at the end, I wanted to store all the XPath for this particular flow in Database. Commented Sep 17, 2022 at 11:33

2 Answers 2

0

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:

public void actions(String action, String param, Webdriver driver, String xpath){ switch(action){ case "Click Button": clickButton(driver, param, xpath); } } 

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.,

public void clickButton(Webdriver driver, String param, String xpath){ List<String> xpathList = new ArrayList<>(); xpathList.add(xpath); try{ if (param.equalsIgnoreCase("Search")) { WebElement waittext = driver.findElement(By.xpath(xpath)); } else if(param.equalsIgnoreCase("Save")) { WebElement waittext = driver.findElement(By.xpath(xpath)); } Actions actions = new Actions(driver); actions.moveToElement(waittext).build().perform(); waittext.click(); return; }catch(Exception e){ System.out.println(e); } } 

If you need the last xpath used, then you can get that from 'xpathList' arraylist by using index.

Sign up to request clarification or add additional context in comments.

1 Comment

Now the problem is I can’t pass the Xpath dynamically as parameter because it’s already hardcoded in every try catch.
0

Right now you are using strings as references to the elements, as you have discovered this is VERY hard to maintain and needs a lot of changes.

One quick and dirty solution would be to just pass the xpath to the method in question instead of translating it, which means you will have that xpath reference available upstream.

Another key insight that might help you is that you can actually pass around references to the WebElements themselves. Take for example a method:

public void clickButton(Webdriver driver, WebElement element) { try{ Actions actions = new Actions(driver); actions.moveToElement(element).build().perform(); element.click(); return; }catch(Exception e){ System.out.println(e); } } 

This has a much clearer purpose and it's much easier to maintain and debug. And now, you would call it with something like

clickButton(driver, searchButton); 

Where:

WebElement searchButton = driver.findElement(By.xpath(searchButtonXpath)); 

Now, you can further abstract those details into a "Page" class that will have all the references to your website's elements and will make it much easier to implement, this is called the Page Object Model and is very encouraged design pattern for your tests since it will save you a lot of time in the future.

Finally, I'd suggest to look into WebDriver waits so you can avoid timing issue when locating the elements and between page refreshes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.