1

I have a Selenium WebDriver based testsuite. XPATH on site are changing sometimes, so I would like to use this to identify which XPATH has to be refreshed. So in case element isn't visible or not clickable, it should write to log currently executed class and method name with an error message. So test should be continued in case of failure.

2

If you use any programming language wrapper for web driver there should be exception handling mechanism so that you catch the exception thrown by Selenium and just output the log information for your particular cases you'd like to handle in your special way. Here is my example for Java language:

public class Test {
    public static void main(String[] args) {
        String[] paths = new String[]{
            "//img[@alt='Google']", //Proper path
            "//img[@alt='Yahoo']"   //Improper path
        };
        WebDriver driver = new HtmlUnitDriver();
        driver.get("http://google.com");
        for(String path: paths){
            System.out.println("Test xPath [" + path + "]..");
            try{
                driver.findElement(By.xpath(path));
                System.out.println("Status: OK");
            }catch(NoSuchElementException e){
                System.out.println("Status: STALE");
            }

        }
    }
}

The output would look like:

Test xPath [//img[@alt='Google']]..
Status: OK
Test xPath [//img[@alt='Yahoo']]..
Status: STALE

In my example I'm just logging out the stale element, but you can change the way of how to handle this according to your requirements (e.g. update some element repository if you have one)

  • I use a lot of XPATH, so it should be handled automatically, because there are a lot of findElement() method calls. – plaidshirt Jan 29 '18 at 11:56
  • If you have the existing tests which original purpose is to catch the defects then you will any way have to put effort to rework the design. findelement is a part of selenium. You cannot inject any loginc there. Only use those pricinples and mechanisms Selenium developers let you use. – Alexey R. Jan 29 '18 at 12:02
  • I see, but it would be really helpful, if testsuite shouldn't be executed a lot of times to found obsolete XPATH. If would be easier if I could know it after first execution and after that refresh each one. – plaidshirt Jan 30 '18 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.