0

I have this code as a part of a program to practice Cucumber, Xpath and Selenium:

@When("^I select Divany i Kresla from left vertical menu$")
        public void i_choose_Divany_i_Kresla_from_left_vertical_menu() {
            driver.findElement(By.xpath("//div[@class='top-left-menu']/ul/li/a")).click();
            driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        }

It's pretty repetitive in every scenario, everything that actually changes is data I select (which I can move to Examples in .feature file) and xpath which I use to access elements on the page. Is there a way to somehow move this step to a generalized class and set it's xpath from inherited classes?

1 Answer 1

1

You can try the following step definitions and which takes sub menu name as input and click on it.

@When("^I select .* from left vertical menu$")
    public void i_choose_Divany_i_Kresla_from_left_vertical_menu(String menu) {
        driver.findElement(By.xpath("//div[@class='top-left-menu']/ul/li/a[text()="+menu+"]")).click();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

however the better thing would be to use page object with Cucumber. Do not use hard coded identifier. put them into variable or use PageFactory. else it might be a maintenance night mare if application goes through some changes in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.