How to pass command line arguments to the browser driver being used - selenium

I'm currently using protractor for some tests. Unfortunately, I can't figure out a way to pass command line arguments to the actual driver being used.
For example, chromedriver.exe accepts '--whitelisted-ips' as a command line argument. Is there any way, in my protractor config, that I can pass this to chromedriver.exe?
Another example is, with MicrosoftWebDriver.exe, it has a flag called '--package' which allows me to pass the package id of the app to target. How would I get protractor launch the driver with those arguments?
I thought that maybe I could launch the standalone selenium server with an argument to launch the driver with those arguments, but from my investigation I couldn't find a way to make that happen.
Just to clarify, I'm not asking to pass command line arguments into protractor to use in my tests. I want the browser drivers being that are running (chromedriver.exe, firefoxdriver.exe, MicrosoftWebDriver.exe) to be run with specific command line arguments.

Add the arguments to your config file as capabilities. This is a driver-specific property.
For Chrome/Chromedriver:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['./tmp/specs/*.spec.js'],
capabilities: {
'browserName' : 'chrome',
'goog:chromeOptions' : {
args: ['--start-maximized']
}
}
}
For Firefox/Geckodriver (only changes shown):
capabilities: {
'browserName' : 'firefox',
'moz:firefoxOptions' : {
args: ['-headless']
}
}
MDN has a (very short) list of vendor-specific capabilities.
See https://sites.google.com/a/chromium.org/chromedriver/capabilities for more.

Related

Enable clipboard in automated tests with Protractor and webdriver

I want to test with Protractor an Angular application that accesses the system clipboard using the clipboard api. The problem is that, when executing the test, the Chromium browser asks the user for permission to access the clipboard. I can't close this box from the test, because this is not a DOM element nor an alert window, and it seems not to be any way to access it from Protractor.
clipboard permissions
I have tried to automatically give permission in Chrome, in protractor setup. First, I looked for a command line switch that do it, but it seems not to exist.
Then I found a way that apparently works, but I don't know if its stable: to set an "exception" in Chrome user profile, using the Capabilities of the chrome driver of webdriver. Basically, I add the following lines of my protractor config file:
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'prefs': {
'profile.content_settings.exceptions.clipboard': {
'http://localhost:8000,*': {'last_modified': Date.now(), 'setting': 1}
}
}
}
}
I have found the value by looking inside Default/Preferences file inside my Chrome profile.
It now works for me, but I don't want to use undocumented features, since they can change anytime without notice. Do you know a better way of doing this? Thanks.
If anyone is looking to do something similar with C#, I have adapted the code as such:
var options = new ChromeOptions();
var clipboardException = new Dictionary<string, object> {
{"[*.]myurl.com,*",
new Dictionary<string, object> {
{"last_modified", DateTimeOffset.Now.ToUnixTimeMilliseconds()},
{"setting", 1}
}
}
};
options.AddUserProfilePreference("profile.content_settings.exceptions.clipboard", clipboardException);

Geb test ignoring GebConfig.groovy file launched in IntelliJ

Running in IntelliJ IDEA.
GebConfig.groovy is in /src/test/resources.
I am using the Chrome driver.
When I type
System.setProperty("webdriver.chrome.driver", "my/path")
inside my spec file, and I right click and select run, the test works, meaning it opens Chrome and loads the page.
When I don't do that in the spec file, but just leave it in the GebConfig.groovy file, I get the error message "the page to the driver executable must be set".
There's an air-gap, so I can't copy-paste; I'll type as much as I can here:
GebConfig.groovy:
import org.openqa.selenium.chrome.ChromeDriver
...
environments {
chrome {
System.setProperty("webdriver.chrome.driver", "my/path")
driver = {new ChromeDriver()}
}
}
The spec file is really simple, like the example on GitHub
import LoginPage
import geb.spock.GebReportSpec
class LoginSpec extends GebReportSpec
{
// Works when I put this here, but I should not have to do this!
System.setProperty("webdriver.chrome.driver", "my/path")
def "user can log in" () {
when: "log in as me"
def loginPage = to LoginPage
loginPage.login("me")
then:
....
}
}
To fix your problem if you want to keep the path in the geb config, setting the path outside of the environment section like so should work:
import org.openqa.selenium.chrome.ChromeDriver
System.setProperty("webdriver.chrome.driver", "my/path")
//You can also set the driver up here as a default and running with an environment set will override it
driver = {new ChromeDriver()}
environments {
chrome {
driver = {new ChromeDriver()}
}
}
Personally I would avoid adding the driver path to the geb config and create a run configuration in intelliJ for running locally.
Right click the spec file > Click "Create 'nameOfMySpec'".
Now add your driver path to the VM parameters:
-Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path
It's also worth considering a shell script that could then also be called via Jenkins etc:
mvn test -Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path

Test work properly on Chrome, not on Firefox

I wrote test using TestNG and selenium.
code...
actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));
actions.sendKeys(Keys.BACK_SPACE);
actions.build().perform();
code...
I wanted to delete text in login window using these sendKeys, with DataProvider
#DataProvider(name = "inputs")
public Object[][] getData() {
return new Object[][]{
{"000000000", true},
{"000000000", true}
};
}
Html:
<div><input type="tel" class="valid TextInput-kJdagG iVdKHC" name="recoveryPhone" id="eb69ff0b-3427-6986-7556-b7af40ffb156" aria-describedby="eb69ff0b-3427-6986-7556-b7af40ffb156" value="+48 "></div>
Error message:
Unable to read VR
Path1523545392670 Marionette INFO Enabled via --marionette
1523545393744 Marionette INFO Listening on port 52644
1523545394180 Marionette WARN TLS certificate errors will be ignored for this session
Test work as I expected on Chrome, but on firefox these sendKeys not always mark the text, and clear this text. In project I have to use action class. Why the test runs differently?
Check this post https://github.com/mozilla/geckodriver/issues/665 against your versions (browser, browser driver, selenium etc which are always sensible to include in any question), it could be a known bug with the geckodriver.
The post includes a work around of creating the chord in a different way, using:
List<CharSequence> keyWithModifiers = new ArrayList<CharSequence>();
keyWithModifiers.add(Keys.CONTROL);
keyWithModifiers.add("a");
String ctrlA = Keys.chord(keyWithModifiers);
textFieldElem.sendKeys(ctrlA);
This approach worked for me using Selenium 3.7.1 Java bindings, gecko driver 0.18.0 (64 bit) and Firefox 57.0.2 - 59.0

How to set the firefox profile at the node end in remote webdriver/grid configuration

It is always suggested to set the firefox profile in DesiredCapabilities and pass that through the wire ,where the hub is running . Like below
DesiredCapabilities caps = DesiredCapabilities.firefox();
FirefoxProfile profile=new FirefoxProfile(new File("Local Path to firefox profile folder"));
caps.setCapability(FirefoxDriver.PROFILE, profile);
URL url = new URL("http://localhost:4444/wd/hub");
WebDriver driver= new RemoteWebDriver(url,caps );
But sending the huge 87-90 mb profile info to hub over http ,for each selenium test case slowing down the test case execution .
I have tried configuring the grid node with "Dwebdriver.firefox.profile=E:\\Firefox_Profile_Location":"", property in json node config file like below.
{
"configuration":
{
.//Other Settings
.//Other Settings
.//Other Settings
"Dwebdriver.firefox.profile=E:\\Firefox_Profile_Location":"",
"maxSession":7,
"registerCycle":5000,
"register":true
},
"capabilities":
[
{"browserName":"firefox",
"seleniumProtocol":"WebDriver",
"maxInstances":5,
"platform":"VISTA"
}
]
}
But running with the above configuration is throwing below error .
WebDriverException: Firefox profile 'E:\Firefox_Profile_Location'
named in system property 'webdriver.firefox.profile' not found
Advanced thanks for any help on how to configure the firefox profile from the node side .
You need to provide the profile in the capabilities object as a base64 encoded zip:
var fs = require('fs');
capabilities: [
{
browserName: 'firefox',
seleniumProtocol: 'WebDriver',
maxInstances: 5,
platform: 'VISTA',
firefox_profile: new Buffer(fs.readFileSync("./profile.zip")).toString('base64')
}
]
Moreover Firefox creates the missing files for a given profile. So you should keep just the necessary files in the profile depending on your needs:
Preferences: user.js
Passwords: key3.db
logins.json
Cookies: cookies.sqlite
Certificate: cert8.sqlite
Extensions: extensions/
I think you'll have to use firefox profile name and not the location.
"webdriver.firefox.profile":"default"
Have a look at this and this and this
If you want know how to create a profile follow this and this

No code coverage driver is available with a remote XDebug + Nebeans

I installed Netbeans 8.2 (on Fedora 24) and a web server with PHP7+XDebug .
The debugger works well with Netbeans but when I execute a test generated by Netbeans, I have this message :
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-json" "/tmp/nb-phpunit-log.json" "--coverage-clover" "/tmp/nb-phpunit-coverage.xml" "/home/karima/netbeans-dev-201608060002/php/phpunit/NetBeansSuite.php" "--" "--run=/home/karima/git/App/tests/selenium"
PHP Fatal error: Class 'WebDriverCapabilityType' not found in /home/karima/git/App/tests/selenium/htdocs/indexTest.php on line 22
PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
Error: No code coverage driver is available
Done.
Here the simple test :
class indexTest extends PHPUnit_Framework_TestCase {
/**
* #var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://app/', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'Test'
$this->assertContains('Test', $this->webDriver->getTitle());
}
}
Howto install the coverage driver in netbeans on linux (Fedora 24) for a remote server ? (and the framework of Selenium ?)
Or have you a good doc (step by step) ?
UPDATE 1 : The file /tmp/nb-phpunit-coverage.xml is empty... I created a bug report.
Thanks
I found.
There are 3 problems.
Install the basic tools of Netbeans
First, Netbeans didn't install the necessary tools for its interfaces. So, you have to install manually PHPUnit and the other tools (for inspect/format, etc.).Here the best method for me :
I installed composer. After, I installed the Netbeans's tools in my environment of development. :
sudo dnf install composer
# necessary tools for Netbeans
composer global require friendsofphp/php-cs-fixer
composer global require phpmd/phpmd
composer global require phpunit/phpunit
...
Sometimes Netbeans detect these tools. When it cannot find the good path, in the options of Netbeans, you have to precise the good path.
Composer pushes these tools in "~/.config/composer/vendor/".
After, the problem with PHPunit disappears.
Secondly, the problems of Selenium.
Here, my objective will execute the first test generated by Netbeans.
You need to follow these steps:
Step 1: Install the webdriver
# driver for selenium/php
composer global require facebook/webdriver
Step 2: Change the code of test in Netbeans. You need to replace the include path.
set_include_path('/home/karima/.config/composer');
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
class MyFirstTest extends PHPUnit_Framework_TestCase {
/**
* #var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = DesiredCapabilities::firefox();
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
}
public function tearDown() {
$this->webDriver->close();
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
}
}
Step 3: Download and uncompressed the last release of geckodriver
Step 4: Move geckodriver in a system's path
mv geckodriver /usr/bin/.
Step 5: Download the JAR of Selenium Standalone Server (I tested with 3.0.0-beta2)
Step 6: Start the selenium server :
java -jar /home/karima/Téléchargements/selenium-server-standalone-3.0.0-beta2.jar
Step 7: You can now run the test in Netbeans without error but...
No code coverage driver is available...
I search again...
I hope it will help others.