Search code examples
cucumbercucumber-jvmcucumber-java

Common methods for different page objects in Cucumber


Question with respect to common methods for different page objects in Cucumber

Has anyone worked upon creating common methods which can be used across different page objects in cucumber.

Example: Click method. I specify page objects in feature file (And I click on object o). This in turns calls the step defination. In step defination, we have written a generic method for click (object o.click())

We also have a separate class where all the page objects are defined (eg: xpath of object o). Now the question is how to integrate, these page objects with the common step defination of click method.

If this is achievable, we only require to change the steps in feature file for different objects(object o to object b). Single click method will work for all different page objects, we just need to add xpath of these objects in common page object class.

Anyone worked upon achieving this ?


Solution

  • Its totally depend on your project framework in which you want to setup.

    Yes it possible

    Example:

    PageOjectclass:

    WebDriver driver = null;
    private WebElement element = null;
    private By By = null;
    
    public PageOjectclass(WebDriver driver) {
        this.driver = driver;
    }
    
    public static WebElement button_submit() throws Exception {
        try {
            element = driver.findElement(By.xpath("//h1[@class='txtCenter white ico30']"));
    } catch (Exception e) {
        AutomationLog.error("HomePageHeader Element not found");
        throw (e);
    }
    return element;
    }
    

    CommonClass

    public static void Customclick(WebElement e) {
         e.click();
    }
    

    StepDefinationClass

    @When("^testing$")
    public void test() throws Throwable {
    
             CommonClass.Customclick(PageOjectclass.button_submit());
    
    }
    

    Just take care of passing the webdriver initialized object, pass them with constructor etc