0

I am automating hybrid application i.e. Desktop and WebApplication. We have desktop application in which some of pages are integrated using WebPages and some one pages are in desktop application. Below is the code which I have used to open Desktop Application. And its opening the application successfully. But When I am going to print resource list size, it comes 'null' where locator for resourcelist is showing correct count in chrome browser. I can login successfully using winium driver instance. After logged in, list of resources are present which I integrated in webpage.

Let me know if I am doing wrong in given code?

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class VMSWiniumBaseDriver {

    public WiniumDriver driver;

    
    String applicationPath = "C:\\Users\\prashantn\\Downloads\\Publish\\ABC.exe";

    @Parameters({ "windowsPlatform" })
    @BeforeClass(alwaysRun = true)
    public void initialize(String browser) throws IOException, InterruptedException {

        if (browser.equalsIgnoreCase("desktop")) {

            try {
                WiniumDriverService service;
                DesktopOptions option;
                option = new DesktopOptions();

                option.setApplicationPath(applicationPath);

                File driverPath = new File(System.getProperty("user.dir") + File.separator + "driver" + File.separator
                        + "Winium.Desktop.Driver.exe");

                service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999)
                        .withVerbose(true).withSilent(false).buildDesktopService();

                try {
                    service.start();
                } catch (IOException e) {
                    System.out.println("Exception while starting WINIUM service");
                    e.printStackTrace();
                }
                driver = new WiniumDriver(service, option);

            } catch (Exception e) {

                System.out.println(e);

            }

        }

    }

    @AfterClass(alwaysRun = true)
    public void TeardownTest() {

        if (null == driver) {
            driver.close();
            driver.quit();
        }
    }

    public WiniumDriver getDriver() {
        return driver;
    }

}

Here is

CommonUtilities.Java

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.ThreadLocalRandom;

import org.openqa.selenium.WebDriver;
import org.sikuli.hotkey.Keys;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.sikuli.script.ScreenImage;

import dataproviders.VMSConfigFileReader;
import managers.VMSWiniumBaseDriver;

public class CommonUtilities extends VMSWiniumBaseDriver {

    public  WebDriver getWebDriverInstance() {
        WebDriver webDriver = (WebDriver) driver;
        return webDriver;
    }
}

LoginPage.java

 import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.winium.WiniumDriver;
    
    import utilities.CommonUtilities;
    
    public class LoginPage{
        
    
        @FindBy(id = "PART_EditableTextBox")
        WebElement userName;
    
        @FindBy(id = "TextBlock_Password")
        WebElement passwordInput;
    
        @FindBy(id = "LoginButton")
        WebElement loginButton;

@FindBy(xpath = "//dd[contains(@class,'vms-tree-enabled')]/span[@class='vms-treeview-label'][@title!='Removed channels'][@title!='Unassociated']")
    List<WebElement> resourcesList;
    
        public void getResourcesList() {
            CommonUtilities commonUtilities = new CommonUtilities();
        //  System.out.println("Page Source :: "+commonUtilities.getWebDriverInstance());
            List<WebElement> list = commonUtilities.getWebDriverInstance().findElements(By.xpath(
                    "//dd[contains(@class,'vms-tree-enabled')]/span[@class='vms-treeview-label'][@title!='Removed channels'][@title!='Unassociated']"));
            System.out.println("Resources Size :: " + list.size());
            for (int i = 0; i < resourcesList.size(); i++) {
                System.out.println("Resource Name :: " + resourcesList.get(i).getText());
            }
        }
    }

Exception Log:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElements(org.openqa.selenium.By)" because the return value of "utilities.CommonUtilities.getWebDriverInstance()" is null
    at pageobjects.LoginPage.getResourcesList(LoginPage.java:57)
    at testcases.AppLaunchTest.selectLayOutBasic1(AppLaunchTest.java:62)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:764)
    at org.testng.TestRunner.run(TestRunner.java:585)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.runSuites(TestNG.java:1069)
    at org.testng.TestNG.run(TestNG.java:1037)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Prashant
  • 1,351
  • 3
  • 20
  • 32

0 Answers0