Selenium for Electron Applications

     Electron allows for the development of desktop GUI applications using front and back end components originally developed for web applications: Node.js run-time for the back-end and Chromium for the front-end.

    Electron uses HTML, JavaScript and CSS to build desktop applications. The vision of same codebase for web, mobile and desktop applications has never been closer.

   There is a small difference in the driver creation section when we compare with normal driver instantiation. Once the driver creation done; rest of the automation script executions are same as Selenium. Here I would like to explain the driver creation logic below,

//Driver creation for Electron Applications using WebDriverManager (in JAVA):

WebDriver driver;
ChromeOptions options = new ChromeOptions();
options.setBinary("pathToElectronApplication");
WebDriverManager.chromedriver().setup(); //you have to add WebDriverManager dependency or library.
driver = new ChromeDriver(options);
//Selenium version(>3.7.x) method to create driver session for Electron Applications (in JAVA):

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "pathTochromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary("pathToElectronApplication");
ChromeDriverService chromeservices = new ChromeDriverService.Builder().build(); 
driver = new ChromeDriver(chromeservices, options);
//Old selenium version method to create diver session for Electron Applications (in JAVA):

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "pathTochromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary("pathToElectronApplication");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", options ); 
capabilities.setBrowserName("chrome");
driver = new ChromeDriver(capabilities);
//Driver creation for Electron Applications using WebDriverManager (in C#):

IWebDriver driver;
ChromeOptions options = new ChromeOptions
{
 BinaryLocation = @"pathToElectronApplication"
};
options.AddArgument("no-sandbox");
options.AddArgument("enable-automation");
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
driver = new ChromeDriver(options);

NOTE:
 - You have to install WebDriverManager package.
 - Sometimes you may get System.UnauthorizedAccessException with chromedriver path denied. You need to run the Visual Studio as admin mode or make sure that \Chrome\<chromedriver-version>\X64 path available within IDE folder of Visual Studio.
 - Make sure that the chrome driver version should match with the chrome version that used in the electron application.    

Shortcut to open inspect mode in Electron Applications,

Windows: Ctrl+Shift+i

MAC: Command+Alt+i

make it perfect !

27 thoughts on “Selenium for Electron Applications

Add yours

  1. hi Sanoj,

    Thanks a lot for this ariticle.
    while running my application i am getting the following error:

    FAILED CONFIGURATION: @BeforeMethod setup
    org.openqa.selenium.WebDriverException: unknown error: no chrome binary at D:\Shared\roadMap
    (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 50 milliseconds
    Build info: version: ‘3.14.0’, revision: ‘aacccce0’, time: ‘2018-08-02T20:19:58.91Z’
    System info: host: ‘MUM-DSK-526’, ip: ‘10.102.193.204’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_191′
    Driver info: driver.version: ChromeDriver

    My code:

    WebDriver driver;

    @BeforeMethod
    public void setup() {

    System.setProperty(“webdriver.chrome.driver”, “D:\\softwares\\selenium\\chromedriver.exe”);
    ChromeOptions options = new ChromeOptions();
    options.setBinary(“D:\\Shared\\roadMap”);

    ChromeDriverService chromeservices = new ChromeDriverService.Builder().build(); driver = new ChromeDriver(chromeservices, options);

    }

    @Test
    public void test() throws InterruptedException {
    driver.findElement(By.xpath(“//p[contains(text(),’Call lists’)]”)).click();
    }
    }

    Can you please help in figuring it out.

    Like

  2. I am running into a different issue now for my electron desktop application.

    my code:
    WebDriver driver;

    @BeforeMethod
    public void setup() {

    System.setProperty(“webdriver.chrome.driver”,
    “D:\\softwares\\selenium\\chromedriver-v4.0.0-win32-x64\\chromedriver.exe”);
    ChromeOptions options = new ChromeOptions();
    options.setBinary(“D:\\Shared\\ProjectSandBox.exe”);
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(“chromeOptions”, options);
    capabilities.setBrowserName(“chrome”);

    options.addArguments(“start-maximized”); // open Browser in maximized mode
    options.addArguments(“disable-infobars”); // disabling infobars
    options.addArguments(“–disable-extensions”); // disabling extensions
    options.addArguments(“–disable-gpu”); // applicable to windows os only
    options.addArguments(“–disable-dev-shm-usage”); // overcome limited resource problems
    options.addArguments(“–no-sandbox”); // Bypass OS security model
    options.addArguments(“–headless”);
    driver = new ChromeDriver(capabilities);

    }

    }

    Issue:

    FAILED CONFIGURATION: @BeforeMethod setup
    org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally
    (unknown error: DevToolsActivePort file doesn’t exist)
    (The process started from chrome location D:\Shared\ProjectSandBox.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
    (Driver info: chromedriver=69.0.3497.106 (857b284701ddf7bef0f14fa76416cf7ca786b411),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)

    Chrome version : Version 71.0.3578.98 (Official Build) (64-bit)
    selenium 3.14.0
    testNg 6.11
    ChromeDriver chromedriver-v4.0.0-win32-x64
    Windows 10

    Any help would be appreciated.

    Like

  3. Hi Karthik,
    I am trying to automate testing of an electron application (from third party vendor) using Selenium and C# and am getting the error:
    OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:.
    Below is the piece of code.
    ChromeOptions options = new ChromeOptions();
    ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService(@”C:\Selenium\chromedriver_win32_1″,
    @””);
    options.AddArgument(“–no-sandbox”);
    options.AddArgument(“–disable-dev-shm-usage”);
    driver = new ChromeDriver(chromeService, options, TimeSpan.FromSeconds(180));

    Can you please help me with this error. Thanks !!!

    Like

  4. Hi Sanoj, Thanks for enlightening us with the knowledge to automate Electron Application through selenium.

    I’m facing a problem here. I need to switch between Electron App and WebApplication . I use two drivers.
    1. to initiate Web and
    2. initiate Electron.

    I’m unable to get things into view/focus. Windows application gets overlapped by Electron App in the screen. I want to work on both the things one after another. Please help.

    Like

    1. What is the actual case? Do you have any connection or data exchange between electron app and web app? If it is parallel execution, you can handle it via testng.xml. If you need switch from electron app in between steps of test cases of between test cases:
      1. Load a electron app and get the session Id of electron app.
      2. Launch another browser and get the session ID that browser,
      3. Do whatever action needed in web browser.
      4. Compare the session Id’s of electron app, setDriver to electron driver and perform action in electron app,
      5. Compare the session Id’s of web app, setDriver to web app driver and validate your actions.

      You need to write a method to launch web driver (chrome or gecko driver), in your test case you can call that method to launch the web driver. Use SessionId sessionid = ((RemoteWebDriver) driver).getSessionId(); to get the session id.

      Like

  5. Hi,
    For electron desktop application automation is there any way to automate Tool bar options like File, Edit, View, Window and Help using selenium webdriver with python?
    I’m not able to inspect tool bar options using ctrl + shift + i.
    Let me know if there is any possible way.
    Also i’m not able to Maximize the application using both chrome options and using driver.maximize_window().

    Like

    1. In the case of Electron application, the toolbar is a builtin space in the Electron framework and you can’t inspect in a normal fashion. But there is an alternative you can use sikulixapi to perform click on toolbar but you have to pass image of the menu’s. Below is the dependency:

      com.sikulix
      sikulixapi
      1.1.0

      Create object of Screen class and then you can do the click action like
      Screen screen = new Screen();
      screen.click(“path_to_your_toolbar_menu_image”);

      Like

  6. Hello Sanoj,

    I am using selenium webdriver c# to automation Electron App, I am able to launch the app but when i am trying to perform any action on the window, i am getting the below exception, could you please help me.

    no such window: window was already closed (Session info: chrome=80.0.3987.163) (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 10.0.18363 x86_64)

    Thanks in advance,
    Narayan

    Like

  7. not able to find elements using the below solution
    WebDriver driver;
    ChromeOptions options = new ChromeOptions();
    options.setBinary(“pathToElectronApplication”);
    WebDriverManager.chromedriver().setup(); //you have to add WebDriverManager dependency or library.
    driver = new ChromeDriver(options);

    Like

  8. Hi, I’m having an issue with testing the app which is on electron. I’m using Selenium for that. I used yours configuration settings and it works tottaly fine (thank you!), but it is not possible to localize any elements (I’m using developer console for elements search). I tried localize them by using xpath/css/id but I receive a NoSuchElementException all the time. Also I waited for elements as well. Thanks.

    Like

  9. Hi, is there any possibility to bypass the Microsoft account login for electron based application like we do in web application with below code.

    driver.get(“http://USERNMAE:PASSWORD@YOUR_URL”);

    Like

  10. Hello Sanoj,

    I have followed the steps mentioned in this article. But when I tried to execute, I am getting below error

    org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally.
    (unknown error: DevToolsActivePort file doesn’t exist)

    Could you please guide me to resolve it? Thanks in advance.

    Other Details:
    I am using Selenium 3.14 version – JAVA 8.
    Chromium version used by my electron application – 91
    chrome driver version – 91

    Like

      1. Thanks Sanoj for the reply. I tried adding the remote-debugging-port. But no luck. My Electron application launches and it closes with in 2 seconds and displays the same error message

        i.e.
        org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally.

        (unknown error: DevToolsActivePort file doesn’t exist)

        (The process started from chrome location C:/Users/Rathish/AppData/Local/Electron.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

        Could you please guide me to resolve it? Thanks in advance.

        Like

  11. Hi Sanoj,
    Could you please guide me to resolve the issue. I am completely blocked and couldn’t proceed further.

    All,
    If you have faced this error in your project, could you please guide me how did you resolve it?

    Thanks in advance

    Like

Leave a comment

Create a website or blog at WordPress.com

Up ↑