Search code examples
javaselenium-webdrivertestngqaf

QAF | Switch driver from Web to Mobile or Vice verse is not holding the driver instance parallel


Using "switchDriver" to switch between Web and Mobile. When I switch Driver between web to web (Eg: Chrome to Firefox or Vice Versa) the driver instance is holding parallelly, but when I switch Driver between Web and Mobile or Vice Versa it is not holding the driver instance parallelly.

Note: I'm using two different service providers. Web (Browserstack) and Mobile (ExperiTest).

@QAFTestStep(description = "I switch driver {drivername}") 
public static void iSwitchDriver(String drivername) {

if (drivername.equals("appiumDriver")) {
            getBundle().setProperty("remote.server","https://experitest/xxxx");
        } else { // chromeRemoteDriver/firefoxRemoteDriver/edgeRemoteDriver
            getBundle().setProperty("remote.server","https://browserstack/xxxx");
        }
        switchDriver(drivername); 
}

Some execution logs when switching from Web to Mobile

11:53:23.937 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: And I take screenshot
11:53:23.938 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - Executing screenshot parameters: {}
11:53:24.367 [TestNG] DEBUG c.q.qaf.automation.core.QAFTestBase - Capturing screen shotTest1_Test_EribankScenario_ntqee73ykjnh52.png
11:53:24.367 [TestNG] DEBUG c.q.qaf.automation.core.QAFTestBase - Info: 
11:53:24.367 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: And I take screenshot
11:53:24.373 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: Given I switch driver "appiumDriver" with arguments: [appiumDriver]
11:53:24.374 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - Executing quit parameters: {}
11:53:24.474 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - command: quit[ param-1: {}] Result: null
11:53:24.474 [TestNG] INFO  c.q.q.automation.ui.UiDriverFactory - UI-driver tear down complete...
11:53:24.474 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: switch to {driverName} with arguments: [appiumDriver]
11:53:24.475 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: switch to {driverName} with arguments: [appiumDriver]
11:53:24.475 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: Given I switch driver "appiumDriver" with arguments: [appiumDriver]
11:53:24.489 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: Given I assert "Mobile.login.button" is present with arguments: [Mobile.login.button]
11:53:24.489 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: assert {loc} is present with arguments: [Mobile.login.button]
Driver not configured!... 
Using firefoxDriver as default value. Please configure driver to be used using 'driver.name' property
11:53:24.490 [TestNG] INFO  c.q.qaf.automation.core.QAFTestBase - Initializing Driver...browser_str:appiumDriver,base_url:http://www.google.com/,sel_server:https://experitest.com/wd/hub,port:4444
11:53:24.490 [TestNG] INFO  c.q.q.automation.ui.UiDriverFactory - Driver: appiumDriver
Oct 03, 2021 11:54:21 AM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: OSS
11:54:21.718 [TestNG] INFO  c.q.qaf.automation.core.QAFTestBase - driver init done
11:54:21.719 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - Executing findElements parameters: {"value":"//*[@id='loginButton']","using":"xpath"}

Some execution logs from Mobile to Web switching:

1:54:31.317 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: Given I assert "Mobile.login.button" is present with arguments: [Mobile.login.button]
11:54:31.323 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: Given I switch driver "firefoxRemoteDriver" with arguments: [firefoxRemoteDriver]
11:54:31.324 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - Executing quit parameters: {}
11:54:38.295 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - command: quit[ param-1: {}]
11:54:38.296 [TestNG] INFO  c.q.q.automation.ui.UiDriverFactory - UI-driver tear down complete...
11:54:38.296 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: switch to {driverName} with arguments: [firefoxRemoteDriver]
11:54:38.296 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: switch to {driverName} with arguments: [firefoxRemoteDriver]
11:54:38.296 [TestNG] INFO  c.q.q.a.step.TestStepListener - Completed Teststep: Given I switch driver "firefoxRemoteDriver" with arguments: [firefoxRemoteDriver]
11:54:38.298 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: Given I open "https://demobank1.xxxxx.com/" with arguments: [https://demobank1.xxxxx.com/]
Driver not configured!... 
Using firefoxDriver as default value. Please configure driver to be used using 'driver.name' property
11:54:38.299 [TestNG] INFO  c.q.qaf.automation.core.QAFTestBase - Initializing Driver...browser_str:firefoxRemoteDriver,base_url:http://www.google.com/,sel_server:https://browserstack.com/grid1/wd/hub,port:4444
11:54:38.299 [TestNG] INFO  c.q.q.automation.ui.UiDriverFactory - Driver: firefoxRemoteDriver
Oct 03, 2021 11:54:42 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
11:54:42.900 [TestNG] INFO  c.q.qaf.automation.core.QAFTestBase - driver init done
11:54:42.900 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - Executing maximizeCurrentWindow parameters: {}
11:54:43.222 [TestNG] INFO  c.q.q.a.ui.WebDriverCommandLogger - command: maximizeCurrentWindow[ param-1: {}] Result: {x=0, y=0, width=1920, height=1055}
11:54:43.222 [TestNG] INFO  c.q.q.a.step.TestStepListener - Invoking Teststep: get {url} with arguments: [https://demobank1.xxxxx.com/]

Appium capabilities:

appium.capabilities.accessKey=asdljj.sdasdad.asdasdasd.asd
appium.capabilities.uuid=asdsd23424
appium.capabilities.platformName=android
appium.capabilities.app=cloud:com.xxxx.xxx/.LoginActivity
appium.capabilities.instrumentApp=true
appium.capabilities.appPackage=com.xxx.xxx
appium.capabilities.appActivity=com.xxx.xxx.LoginActivity
appium.capabilities.driverClass=io.appium.java_client.android.AndroidDriver

Browser Capabilities:

chrome.additional.capabilities={'name': 'Chrome - Test Run','browserName': 'chrome', 'browserVersion': '89.0' }

I have already tried referring below links for solution: Switch back and forth between drivers in qmetry , How to configure one test in TestNG XML file to start two driver, like Chrome Driver and Android Driver when using QAF


Solution

  • By design qaf tear-downs driver instance when observe any driver specific property change. You can try by removing configuration listener(s).

        ConfigurationManager.getBundle().clearConfigurationListeners();
    
    

    It may impact on resource load behavior.

    To overcome that you can utilize driver specific resources feature which allows you to create separate properties file/folder and set as driver resource. In that way when changing driver you don't need to write code for load resources. For example:

    env.name=qa
    env.resources=resources/common;resources/${env.name}
    driver.name=androidRemoteDriver
    
    android.resources=resources/android;resources/mobilecloude.properties
    ios.resources=resources/ios;resources/mobilecloude.properties
    chrome.resources=resources/web;resources/browsercloude.properties
    firefox.resources=resources/web;resources/browsercloude.properties
    edge.resources=resources/web;resources/browsercloude.properties
    

    in above example,

    • resources/web, resources/android, resources/ios may contains locator repositories if locators are different for android/ios/desktop-web.
    • resources/${env.name} may contain resources specific to environment. For example environment specific test data.