How to download a file with phantomJS using Robot Framework? - phantomjs

I am using Robot Framework with PhantomJS (headless browser) and want to download a file. But PhantomJS is not having any desired Capabilities or options to set the preferences as we do in Chrome. Looking for any suggestions or other options to download a CSV file using PhantomJS with Robot Framework.

As you highlighted that downloading files is not the issue. Below you'll find an example of Robot Script that starts Chrome in Headless mode.
*** Settings ***
Library Selenium2Library
Suite Teardown Close All Browsers
*** Test Cases ***
Headless Chrome - Open Browser
${chrome options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${prefs} Create Dictionary credentials_enable_service=${false} # Verberg de sla wachtwoord op pop-up
Call Method ${chrome_options} add_experimental_option prefs ${prefs}
Call Method ${chrome options} add_argument start-maximized # Open de browser in gemaximaliseerd.
Call Method ${chrome_options} add_argument --headless
Call Method ${chrome_options} add_argument --disable-gpu
Call Method ${chrome_options} add_argument --window-size\=1920,1080
Create Webdriver Chrome chrome_options=${chrome options}
Go To http://cnn.com
Capture Page Screenshot
In case you want to run in headless mode in a Grid environment use the following example:
*** Settings ***
Library Selenium2Library
Suite Teardown Close All Browsers
*** Test Cases ***
Headless Chrome - Create Webdriver2
${chrome options} = Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} add_argument --window-size\=1920,1080
Call Method ${chrome options} add_argument --start-maximized
Call Method ${chrome options} add_argument --headless
Call Method ${chrome options} add_argument --disable-gpu
${options}= Call Method ${chrome_options} to_capabilities
Create Webdriver Remote command_executor=http://localhost:4444/wd/hub desired_capabilities=${options}
Go to http://cnn.com
Capture Page Screenshot

Related

WebDriverException (Status code 127) when running Selenium + webdriver_manager on gitlab-CI machine (linux)

I'm running a simple CI pipeline on GitLab for a Selenium script headlessly + using webdriver_manager to handle chrome driver binary.
This part is passed:
Get LATEST chromedriver version for None google-chrome
There is no [linux64] chromedriver for browser None in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/100.0.4896.60/chromedriver_linux64.zip
Driver has been saved in cache [/root/.wdm/drivers/chromedriver/linux64/100.0.4896.60]
But after that I'm getting this error:
WebDriverException: Message: Service /root/.wdm/drivers/chromedriver/linux64/100.0.4896.60/chromedriver unexpectedly exited. Status code was: 127`
What is the problem? Seems like webdriver_manager has a problem by running in CI.
Here is a simple script for reproduce:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("http://google.com")
driver.find_element('name', 'q').send_keys("Wikipedia")
This is one of the pipelines:
https://gitlab.com/mmonfared/test/-/jobs/2350697126
This is a sample project:
https://gitlab.com/mmonfared/test
I've also opened an issue in webdriver_manager github repo, no answers yet:
https://github.com/SergeyPirogov/webdriver_manager/issues/363
This error message...
WebDriverException: Message: Service /root/.wdm/drivers/chromedriver/linux64/100.0.4896.60/chromedriver unexpectedly exited. Status code was: 127`
...implies that you are executing your tests as the root user.
Deep Dive
As per Chrome doesn't start or crashes immediately
A common cause for Chrome to crash during startup is running Chrome as
root user (administrator) on Linux. While it is possible to work
around this issue by passing --no-sandbox flag when creating your
WebDriver session, such a configuration is unsupported and highly
discouraged. Please configure your environment to run Chrome as a
regular user instead.
Solution
Execute your tests as a non-root user.

getting error WebDriverException: Message: unknown error: call function result missing 'value'

here is my first and simple code in robot framework , I am just trying to input text in google searchbox and then close the browser but it keeps on failing.
*** Settings ***
Documentation Suite description
Library Selenium2Library
*** Variables ***
${Browser} chrome
${URL} https://www.google.com/
${text} ab
*** Test Cases ***
TC 001 Browser Open and Close
Open Browser ${URL} ${Browser}
Set Browser Implicit Wait 5
Input Text name=q ab
Close Browser
error message -
WebDriverException: Message: unknown error: call function result missing 'value'
(Session info: chrome=80.0.3987.132)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.17763 x86_64)
strong text
Strange thing if i run only below it works fine so i am suspecting it has to do with step where i am trying to insert text in google search box
*** Settings ***
Documentation Suite description
Library Selenium2Library
*** Variables ***
${Browser} chrome
${URL} https://www.google.com/
${text} ab
*** Test Cases ***
TC 001 Browser Open and Close
Open Browser ${URL} ${Browser}
Set Browser Implicit Wait 5
Close Browser
You don't have the correct chromedriver version for your version of chrome. The error shows you're using chrome 80, but chromedriver 2.33. You need to be using chromedriver version 80.

How to run pytest-selenium with xdist and use cli arg driver?

I am trying to run multiple tests in parallel using xdist and determine the webdriver based on the user's command line argument such as
--driver Remote/Chrome/etc
Ultimately, I am trying throw this into docker so that it can be used in a gitlab pipeline.
Here is what I've tried..
conftest.py
#pytest.fixture(scope="session")
def setup(request, selenium):
# Get cli specified driver
driver = selenium
driver.get(os.environ.get('server'))
# Collection
session = request.node
for item in session.items:
cls = item.getparent(pytest.Class)
setattr(cls.obj, "driver", driver)
# Teardown when tests finish
yield driver
driver.close()
test_login.py
#pytest.mark.usefixtures("setup")
class Test:
def test_loginValid(self, selenium):
util.loginEnv(selenium)
Which gives me the error..
========================================================================= ERRORS =========================================================================
_________________________________________________________ ERROR at setup of Test.test_loginValid _________________________________________________________ ScopeMismatch: You tried to access the 'function' scoped fixture 'selenium' with a 'session' scoped request object, involved factories
tests\conftest.py:37: def setup(request, selenium)
..\appdata\local\programs\python\python37-32\lib\site-packages\pytest_selenium\pytest_selenium.py:205: def selenium(driver)
If I attempt to remove the scope from the setup fixture, I get the following error from conftest.py
# Collection
session = request.node
> for item in session.items:
E AttributeError: 'Function' object has no attribute 'items'
tests\conftest.py:59: AttributeError
-------------------------------------------------------------------- pytest-selenium --------------------------------------------
Driver log: ...\pytest-426\test_loginValid0\driver.log
URL: *** CENSORED ***
WARNING: Failed to gather log types: Message: unknown command: Cannot call non W3C standard command while in W3C mode
Doing this method has worked previously without the xdist collection. How can I make this work if it is possible and what am I doing wrong?

Serenity test error element not available in headless mode

The scenario is:
I have serenity tests that i launch with chromedriver that work
When i launch the tests without headless option the tests are "passed"
while with the headless mode the tests are failed with error:
net.serenitybdd.core.exceptions.SerenityManagedException: The following error occurred: Timed out after 5 seconds. Element not available
here the line command that launch the test : mvn clean verify -Dwebdriver.driver=chrome
And the serenity.properties:
webdriver.chrome.driver = chromedriver
webdriver.base.url= ********
webdriver.timeouts.implicitlywait=5000
chrome.switches=--headless;
serenity.browser.maximized = true
The solution is to add in chrome.switches --window-size=1920,1080;
chrome.switches=--window-size=1920,1080;--headless;

Robot Framework can't find chromedriver in PATH variable

I want to use Chrome for automation test with Robot Framework. Here are my settings:
*User variables:*
name: webdriver.chrome.driver
value: C:\chromedriver_win32\chromedriver.exe
name: PATH
values: ......;C:\chromedriver_win32\chromedriver.exe
My code:
*** Settings ***
Library BuiltIn
Library Selenium2Library
Library SikuliLibrary
Library OperatingSystem
*** Variables ***
${HOST} = VM
${URL} = http://VM.com
${BROWSER} = Chrome
*** Test Cases ***
Begin Web Test
Open Browser ${URL} ${BROWSER}
maximize browser window
After execution of command: pybot test.robot:
==============================================================================
[ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: No brows
er is open
Test Case FException
AttributeError: "'Service' object has no attribute 'process'" in <bound method
Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x000000
0003670278>> ignored
Test Case | FAIL |
Setup failed:
WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Plea
se see https://sites.google.com/a/chromium.org/chromedriver/home
------------------------------------------------------------------------------
What is wrong here?
Thank you
"chromedriver' executable needs to be in PATH" means that the location of chromedriver needs to be in the path. In your case, path needs to be "...;C:\chromedriver_win32". PATH contains folders, not executables.
Download Chromedriver.exe from its official website, extract the .exe file and copy this .exe file in scripts folder of Python installation, e.g. C:\Python36\Scripts
After this, add this scripts path in Environment variable, as below:
and now execute your test case.