0

Objective:

In our automation suite, we are using the driver factory implementation and would like to have it in a reusable method.

So, In our tests, we do the following ( want to get away from this duplication )

package org.Melos.pagobjects.members;

import org.Melos.methods.readers.membersReader;
import org.Melos.pagobjects.testbase.baseClass;
import org.Melos.pagobjects.testbase.driverFactory;
import org.openqa.selenium.By;

public class simpleSearchObj extends baseClass {
    
    public simpleSearchObj() throws Exception {

    }

    By menu = By.xpath(membersReader.memberReader("taskListMenu"));
    By SearchField = By.xpath(membersReader.memberReader("memberSimpleField"));
    By SearchButton = By.xpath(membersReader.memberReader("memberSearchButton"));
    By ResetButton = By.xpath(membersReader.memberReader("memberResetButton"));

    public void memberSimpleSearch() throws Exception {

        clickWait(driverFactory.getInstance().getDriver().findElement(menu), 30);
        Thread.sleep(1000);
        clickWait(driverFactory.getInstance().getDriver().findElement(SearchField), 30);
        sendKeys(driverFactory.getInstance().getDriver().findElement(SearchField), "Member Search Field", "---------");
        clicks(driverFactory.getInstance().getDriver().findElement(SearchButton), "Simple Search Button");
        switchTabs();

    }
}

Our Driver Factory

package org.Melos.pagobjects.testbase;

import org.openqa.selenium.WebDriver;

public class driverFactory {

        ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>();

        private static final driverFactory instance = new driverFactory();

        public static driverFactory getInstance() {
            return instance;
        }

        public WebDriver getDriver() {
            return driver.get();
        }

        public void setDriver(WebDriver diverParam) {
            driver.set(diverParam);
        }

        public void closeBrowser() {
            driver.get().close();
            driver.remove();
        }

        public driverFactory() {
        }
    }

Our baseclass:

package org.Melos.pagobjects.testbase;

import io.github.bonigarcia.wdm.WebDriverManager;

import org.Melos.methods.helpers.actionEngine;
import org.Melos.methods.readers.sysReader;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.time.Duration;


public class baseClass extends actionEngine {
    browserFactory browse = new browserFactory();

    @BeforeMethod
    public void setupMelos() throws Exception {

        String browser = sysReader.melosSetup("browser");
        String url = sysReader.melosSetup("urlTest");

        driverFactory.getInstance().setDriver(browse.CreateBrowserInstance(browser));
        WebDriver driver = driverFactory.getInstance().getDriver();
        WebDriverManager.chromedriver().setup();

        driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));

        driver.manage().window().maximize();
        driver.navigate().to(url);
    }
    @AfterMethod
    public void tearDown() {
        WebDriver driver = driverFactory.getInstance().getDriver();
        driver.close();
        driver.quit();
    }
}

Every time we call a reusable method, we have to type/use

driverFactory.getInstance().getDriver

The ask is how or what is the correct way to create a reusable method? My last attempt was this

public void factory() throws Exception {
String factory = driverFactory.getInstance().getdriver();
}

I would appreciate help in this matter, Please let me know if more information is needed.

0

Your Answer

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