Testing Electron application with Selenium (Python - RobotFramework) - selenium

I have a desktop application based on Electron (with Electron we can build cross-platform desktop apps with JavaScript, HTML, and CSS)
Can I test an it using Selenium Library?

Electron contains Chromium and ChromeDriver, so it can talk to Chromium and Selenium, it’s just Webdriver implementation.
Selenium needs this to be able to make calls to the Electron App. ChromeDriver acts as a bridge between Selenium and our application, it follows Selenium wire protocol. By default, chromium runs on port 9515.
- Demo
1. Install and start ChromeDriver: we need to download ChromeDriver version which matches what our application uses.
With Python:
Arguments:
command_executor: Local or remote port where chromedriver is running (9515 in our case)
desired_capabilities: dictionary specifying location of Electron App executable (ElectronApp.exe)
remote-debugging-port: port for the application (7070 in our case)
==> After this you should see the first page of your Electron Application pop up!
with RobotFramework:
The options argument can be used to launch Chomium-based applications which utilize the Chromium Embedded Framework . To launch Chomium-based application, use options to define binary_location attribute and use add_argument method to define remote-debugging-port port for the application. Once the browser is opened, the test can interact with the application.

By RobotFramework
Download Chrome Driver. It must be same Chrome version in Electron.
Start chromedriver.exe as a service
***Keywords***
Start WebDriver Service
${port} Convert To Integer ${portNumber}
${service}= Evaluate sys.modules['selenium.webdriver'].chrome.service sys
${service} CallMethod ${service} Service path/to/chromedriver.exe port=${port}
Call Method ${service} start
the ${portNumber} is a variable for port of ChromeDriver
Start Electron application
Start Electron App
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys
Call Method ${options} add_argument --remote-debugging-port\=7070
Call Method ${options} add_argument --no-sandbox
${options.binary_location} Set Variable path/to/electron/application
Create WebDriver Remote command_executor=127.0.0.1:${portNumber} options=${options}
then you can run test cases like browser Apps.

Related

In Selenium how does ChromeDriver executable finds Chrome browser?

For Selenium, we define the chrome executable path in System.setProperty. When a URL is passed in driver.get and Chrome invokes:
1 - How does chrome executable know where Chrome browser is actually installed?
2 - What would happen if I do not have Chrome browser?
Responses appreciated!!
As per the Requirements of ChromeDriver:
The ChromeDriver consists of three separate pieces. There is the browser itself i.e. chrome, the language bindings provided by the Selenium project i.e. the driver and an executable downloaded from the Chromium project which acts as a bridge between chrome and the driver. This executable is called the chromedriver, we generally refer to it as the server to reduce confusion.
The server expects you to have Chrome installed in the default location for each system as per the image below:
1For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary. In case you are using a Chrome executable in a non-standard location you have to override the Chrome binary location. as follows:
Google chrome doesn't have built-in driver server, so you need to install ChromeDriver so that the selenium code communicates with the chrome browser.This ChromeDriver implements webdriver's wire protocol (client being system on which webdriver API is used & server being browser acting as/containing stand alone server).
For Internet explorer one needs to install InternetExplorerDriver as stand alone server. For Selenium 3.0 & above to work with firefox, Geckodrver has to be installed.

Do headless web browser need selenium WebDriver?

I am trying to use headless web browser(such as headless chrome) for our selenium tests.Should I have to use selenium WebDriver(for python or c# bindings)?
Headless Chrome
As per Getting Started with Headless Chrome the Headless Chrome is the server environment where you don't need a visible UI shell.
If you've got Chrome 59+ installed, you start Chrome with the --headless flag as follows:
chrome \
--headless \ # Runs Chrome in headless mode.
--disable-gpu \ # Temporarily needed if running on Windows.
chrome should always point to your installation of Chrome. The exact location of-coarse varies from platform to platform.
ChromeDriver
As per ChromeDriver - WebDriver for Chrome, in simple words WebDriver is an open source tool for automated testing of webapps across many browsers which provides capabilities for navigating to web pages, user input, JavaScript execution, and much more. ChromeDriver is the standalone server which implements WebDriver's wire protocol for Chromium.
Conclusion
If you intend to use Chrome Browser in Headless Mode(i.e. Headless Chrome) for your selenium tests you have to mandatorily use ChromeDriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
Before we set up a Chrome web driver instance, we have to create an Options object that allows us to specify how exactly we want to launch Chrome. Let’s tell it that we want the browser to launch headless and that the window size should be set to 1920x1080. We also need ChromeDriver to be able to run Chrome at all 
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads and put it in the
# current directory
chrome_driver = os.getcwd() +"\\chromedriver.exe"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get("https://www.google.com")

Selenium not finding elements in Firefox when run via Jenkins

I have a suite of Nightwatch tests that I regularly run across multiple browsers on Windows 10
Recently selenium has been unable to locate any elements when running Firefox through a local instance of Jenkins that is run as a Windows service. Firefox uses geckodriver webdriver in Nightwatch.
Test run flawlessly when run locally via cmd or git bash etc.
Anyone have any idea what may be causing this issue.
Versions installed:
Geckodriver v.0.19.1
Firefox v.57.0.4
Nightwatch v0.9.19
Selenium-server-standalone-3.8.1
Failure trace snippet
Starting selenium server... started - PID: 3804
[Widgets\ C36310 Widgets Available On Dispatch] Test Suite
==============================================================
Running: C36310 Widgets Available On Dispatch
√ Details & Strings Loaded
√ Home Page Element Selectors Loaded
× Check For Visibility Of Sign In Banner. - expected "visible" but got:
"not visible"
at Object.exports.command (C:\Users\matthewf\Documents\master-ui\workspace\Workspaces\master_eld\src\resources\custom_commands\login.js:22:6)

Mink and Behat: Is there a way to launch selenium (Chrome) in background?

When running tests, I would like not to be bothered by Chrome popping-up every time Behat launches a scenario needing Chrome.
I know there is a maximizeWindow() method for session objects, but nothing like "minimize".
Nowadays, you can set Chrome to run in headless mode like so (behat.yml):
default:
extensions:
Behat\MinkExtension:
javascript_session: browser
sessions:
browser:
selenium2:
browser: chrome
capabilities:
chrome:
switches:
- "--headless"
You could run Chrome with a virtual framebuffer, so that the window appears on a virtual screen instead of your real screen.
This also allows to easily run your tests on a headless machine, such as a build server.
On Linux, we're using xvfb for this particular purpose. More info: How To Run Your Tests Headlessly with Xvfb
For Windows, see Is there anything like xvfb or xnest for Windows?
Seems there is no method for launching in background. maximizeWindow is for setting the resolution.
You should try to run in a virtual machine or on another PC in order to avoid this kind of issues, popups and interacting by mistake with the window that runs automation.
Running on the same machine should be done when writing new tests and when debugging.
I found an acceptable solution for this that didn't require me to run Chrome headless or in a virtual machine.
Simply log in with a secondary user account on your local machine, and run selenium on that account. Then, switch back to your main user account and run your tests. The chrome browser will be created under the user running selenium and you will never see the chrome windows pop up.

Open phantomJS using robot framework with javascript off

Is there a way to open PhantomJS browser with JavaScript off?
I have used the following code but JavaScript is still enabled.
Open Browser url phantomjs desired_capabilities=javascriptEnabled:False
Thanks
Zied
No. The Robot framework uses Selenium to run web tests. The Selenium Webdriver support in PhantomJS is provided by Ghostdriver which is fully implemented in JavaScript and partially runs inside of the page context. If JavaScript would be disabled, then you couldn't use the webdriver protocol anymore to talk to PhantomJS.