Search code examples
seleniumselenium-webdriverqaf

How to find the driver instance in QAF framework


During execution I want to know which browser currently the script is running and do some actions based on the browser the script is running.

I am using

Option 1:

if (new QAFExtendedWebDriver().getUnderLayingDriver().equals("ChromeDriver")) {
                 // Do`enter code here` some thing if this is a chrome browser
            }
else if (new QAFExtendedWebDriver().getUnderLayingDriver() instanceof FirefoxDriver) {   

        }

Thsi option is not working during my excution. What is the way to know which browser I am in ?


Solution

  • Below are few examples:

    //to get driver name for this thread which provided using driver.name
    String drivername = TestBaseProvider.instance().get().getDriverName();
    
    //if running in browser, browser name for this thread, will not be avialable for mobile native or hybrid apps
    String browserName =getDriver().getCapabilities().getBrowserName();
    
    //underlying driver class name, which can be remote driver if you are using remote driver
    String driverClassname = getDriver().getUnderLayingDriver().getClass().getSimpleName();
    

    you didn't provided details about what you are trying to achieve. If you want to do something when browser opens, you can utilize driver listener.

    If you are using bdd and you have platform/browser specific code, instead of using if else you can have separate step implementation for each platform/browser in different package and load step provider package accordingly, for example:

    step.provider.pkg=com.exmple.steps.common;com.exmple.steps.chrome