Home > Mobile >  How to get last xpath executed or used by selenium webdriver?
How to get last xpath executed or used by selenium webdriver?

Time:09-20

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related