What is the best way to leverage command line arguments with AngularJS Protractor?

Currently, I am utilizing Protractor for executing extensive end-to-end tests. Instead of storing login credentials in a spec file, I am keen on passing them through the command line. Upon stumbling across a post showcasing the usage of process.argv.forEach, I couldn't help but wonder how to retain and apply these values in a different spec file. Specifically, I have a designated file known as login-spec.js, where I aim to integrate the input obtained from the command-line.

Your guidance is greatly appreciated!

Answer №1

Within the provided example configuration, there is an intriguing section:

  // The params object will be passed directly to the protractor instance,
  // and can be accessed from your test. It is an arbitrary object and can
  // contain anything you may need in your test.
  // This can be changed via the command line as:
  //   --params.login.user 'Joe'
  params: {
    login: {
      user: 'Jane',
      password: '1234'
    }
  },

You can then access the params object in your code like so: browser.params.login.user

In your scenario, if you execute protractor in this manner:

protractor ... --params.login.user=abc --params.login.password=123

You would be able to utilize these variables within your code by accessing them like this:

browser.params.login.user and browser.params.login.password

Answer №2

One drawback of the accepted answer is that these variables will only be accessible when the browser is started. This means that if you plan to use them in configuration settings (such as creating if/else logic), it will not work as intended.

An Alternative Solution

Protractor operates as a node process, which means that any node process can be launched with custom node variables. While the process may differ for Windows systems (feel free to share if you know how), for Mac and other Linux/Unix operating systems, you can

Start Protractor with environment variables like so:

MY_VAR=Dev protractor tmp/config.js

This will make the variable available throughout your process:

console.log(process.env.MY_VAR)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The presence of 'touched' within Angular validation is causing a delay in method execution

Upon utilizing this validation method, it became apparent: <label>Password</label> <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': f.password.touc ...

When using Python Selenium, the driver.close() method may result in an error stating "no such window: target window already closed; web view not found" when attempting to close a new tab

Recently, I've delved into working with multiple tabs in Selenium and encountered a peculiar issue. When running the following code: WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) driver.switch_to.window(driver.window_handles[-1]) time ...

How can we simplify this React component to reduce its verbosity?

After creating a test project to delve into react, react-router and react-redux, I revisited the Settings.jsx file. Now, I am pondering on how to streamline it and reduce potential errors. import React, { Component } from "react"; import { connect } from ...

Transferring information from an online platform onto pre-arranged sheets for hard copy

Has anyone had success printing from a website? I have some papers that are already printed with checkboxes. These checkboxes need to be filled in based on information entered through a web form or retrieved from a MySQL database. All of this information ...

Utilize Webpack to integrate redux-form as an external library

I currently have a range of imports in my project, such as: import {Field, reduxForm, FormSection, formValueSelector} from 'redux-form'; My goal is to treat the redux-form imports as an external library so that they do not get included in the b ...

Discover the nodes with the highest connections in a D3 Force Graph

As I explore the functionalities of a D3 Force Directed Graph with zoom and pan features, I encounter an issue due to my limited knowledge of d3.js. Is there a way to estimate the number of links for each node in this scenario? I am currently at a loss on ...

Error: Selenium encountered an AttributeError stating that the type object 'By' does not contain the attribute 'id'

Currently, I am attempting to populate a dropdown form using Selenium. I have been trying to select the dropdown element based on its ID but encountered an error message stating: AttributeError: type object 'By' has no attribute 'id'. T ...

Steps for displaying a two-dimensional dataset in a tabular format

My dataset includes column names, row names, and values for specific row/column combinations. Here is a glimpse at the structure: cols = ['A', 'B', 'C', 'D']; rows = ['1', '2', '3', &ap ...

Tips on including starting information into an angularjs application from the displayed HTML

I'm currently working on a complex AngularJs application that requires User Login. Once the user is authenticated, a page will be displayed to them and additional data will be loaded later. There are two methods for achieving this: XHR Fetch af ...

Creating an array of objects in Node.js can be achieved by simply declaring an array

Here's the structure of my class: var Coin = { _id: null, createGame: function(id) { this._id = id; }, start: function() { } }; My issue is that I am unable to create an array of objects, as I can only create one. Can someo ...

Dynamic Configuration of API Base URL in AngularJS using RestangularIn AngularJS, the Restangular

I am currently utilizing AngularJS along with Restangular. My main concern is how to determine the baseUrl dynamically at runtime. It seems that using the run or config methods may not be a viable option because it is up to the user to select the server th ...

Manipulating the information pulled from an external PHP script that is currently being displayed

Although I don't consider myself a professional, I am determined to learn some web languages this summer to be able to turn my design ideas into prototypes. Currently, I am struggling to figure out how to manipulate elements that are being echoed bac ...

Tips for utilizing ng-checked and ng-disabled in conjunction with ng-repeat

I encountered a challenge with ng-repeat while incorporating ng-checked and ng-disable alongside it. <div ng-repeat="module in modulelist"> <input id="switch{{$index}}" ng-init="setState($index);" type="checkbox" ng-checked="switch.checked" ng-di ...

"Enhance user experience with AngularJS UI-Bootstrap Typeahead feature

I am currently utilizing AngularJS along with AngularUI Bootstrap's Typeahead. I am interested in implementing the filter feature with multiple parameters. Below is the code snippet of my input control: <input id="tbUsers" class="form-control" ng- ...

Can someone help me figure out the issue with my Angularjs ng-repeat implementation?

After spending hours trying to figure out why something so simple is not working, I'm at a loss. Testing with a dummy list proves the functionality works, but when I connect my server-side data source, it fails. The JSON returned from the AJAX call i ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...

The action is undefined and cannot be read for the property type

Using the React+Redux framework, I encountered an error: https://i.sstatic.net/0yqjl.png During debugging, the server data successfully reached the state, but the action was empty: https://i.sstatic.net/41VgJ.png Highlighted below is a snippet of my co ...

bcrypt is failing to return a match when the password includes numeric characters

I've integrated node-bcrypt with PostgreSQL (using Sequelizejs) to securely hash and store passwords. In the process, the user's password undergoes hashing within a beforeValidate hook as shown below: beforeValidate: function(user, model, cb) { ...

Attempting to extract text by utilizing the getText() function in Selenium along with Javascript

Attempting to use the getText() method in Selenium and Javascript, but encountering issues. const {Builder, By, Key, until} = require('selenium-webdriver'); (async function example() { let driver = await new Builder().forBrowser('chrom ...

How to use jQuery to dynamically assign a class to an li element

I'm attempting to use jQuery to add the 'block' class to specific li elements, but for some reason the class isn't being applied. The goal of the program is to display time slots and disable certain ones if they are blocked. Here's ...