Analyzing information in JavaScript using Selenium

Currently, I am facing an issue while trying to extract data from a JSON file using JavaScript. I am able to successfully read data from the file, but when I try to pass this data within my code, it results in errors. Any guidance or assistance on this matter would be greatly appreciated.

Here is the code snippet I am working with:

 module.exports = {
'@tags': ['TC2'],
"LAUNCHURL" : function (browser) {
var fs = require('fs');

fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {

if (err) throw err; 
var mydata = JSON.parse(data);

var url_get = mydata.credentials[0]['url']

    browser
    .url(url_get)
    .waitForElementVisible('//body', 1000)
    });

},

"Login": function(browser) {
 var fs = require('fs');
 fs.readFile('C:/NightWatch_Automation/credentials.json', 'utf8', function (err, data) {
 if (err) throw err; 
 var mydata = JSON.parse(data);

 var email = mydata.credentials[0]['email']

  browser
    .useXpath()
    .click("//a[contains(text(),'Sign In')]")
    .waitForElementVisible('//body',1000)
    .setValue("//input[@aria-label='Enter email address']", email )
    .click("//button[@type='button']")
    .waitForElementVisible("//input[@id='password']")
    .setValue("//input[@id='password']","password12345")
    .click("//button[@type='button']")
     });

 }
};

This is how my JSON file is structured:

{ "credentials": [  {"url": "https://www.walmart.com", "search": "bandaids", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384c5d4b4c784159505757165b5755">[email protected]</a>", "password" : "password12345"}] }

Additional Information: After testing, I noticed that the script only functions correctly when the values are hardcoded. When attempting to dynamically pass in data, the code breaks and fails to execute. I am using Nightwatch as my testing tool.

Answer №1

The routes diverge, potentially causing the issue:

LAUNCHURL:

fs.readFile('C:/NightWatch_Automation/credentials.json'

Login:

fs.readFile('C:/Users/NightWatch_Automation/credentials.json'

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

How to make Selenium wait for a specified number of seconds

I have a specific goal in mind: to navigate to a webpage, wait for the content to fully load, and then capture and save a screenshot of it. Below is the code I've currently implemented: WebDriver driver = new FirefoxDriver(); driver.get("http://ww ...

What is causing the beforeUpdate hook in Sequelize to fail?

I have a user model with 2 hooks that I am working on. User.beforeCreate(setSaltAndPass) User.beforeUpdate(setSaltAndPass) The first hook works perfectly fine, but the beforeUpdate hook is not triggering as expected. As per the documentation, there should ...

Using driver.findElements() to verify the presence of an element can be time-consuming

Is there a faster method to determine if a link is present on a webpage without having to wait for findElements to evaluate the statement? driver.findElements(By.linkText("New pool")).size()<=0 ...

Strategies for extracting data from multiple Textareas in React

I am facing an issue with a form that contains multiple textarea elements (specifically 3). The problem is that when I type in one textarea, the content is automatically filled and updated in the other textareas as well. This behavior is not desired. I h ...

How can I use Selenium in Python to extract images from a webpage?

Someone on a different platform is looking for someone to scrape images from a website. They want the images to load on the same page. I struggled to find a way to do this other than loading all the images on the page using Selenium, then extracting each i ...

Is it Possible to Remove an Item from an Array in Vue without Explicitly Knowing the Array's

I'm currently working on a feature that involves removing an item from an array when it is clicked. The code I have so far looks like this: <span @click="deleteItem(index)" v-for="(item, index) in customTaxonomies.featured" v-html="item"></s ...

What steps can I take to enhance the efficiency of this JavaScript DOM data manipulation algorithm?

Purpose I am working with a DOM that contains around 70 elements (divs with content). I have the need to move and toggle the display of these divs frequently and rapidly. Speed is crucial in this process. The trigger for moving and toggling these divs is ...

Tips on linking a condition-reaction to document.querySelector

I am struggling to connect the condition-reactions to the input id of passid. I am unsure where to place the document.querySelector() method in order to link the indexed conditions correctly. Below is the code snippet: <!doctype html> <html> ...

Feature exclusively displays malfunctioning image URLs for the web browser

Hello everyone! I've been diving into learning JavaScript and recently attempted to create a function that randomly selects an image from an array and displays it on the browser. Unfortunately, despite my efforts, all I see are broken link images. Her ...

don't forget to store the last JSON value in an array

Currently, I am dealing with a sorted array list and need to implement a certain formatting rule based on the value of a json boolean in the previous element of the array. In the code snippet provided, the formatting decision is made using the current valu ...

Having trouble showing the fa-folders icon in Vuetify?

Utilizing both Vuetify and font-awesome icons has been a successful combination for my project. However, I am facing an issue where the 'fa-folders' icon is not displaying as expected: In the .ts file: import { library } from '@fortawesome/ ...

Using React Native to assign state to a constant variable

As I venture into the world of react native, I encountered an interesting scenario while utilizing a library called react native paper. In the code snippet below, you can observe that the state is being assigned to a const. import * as React from 're ...

Using Vue to implement a "v-model" on a custom component that incorporates the ace-editor

Snippet CustomEditor.vue: <template> <div class="custom-container"> <div class="custom-editor" ref="editor"></div> </div> </template> <script> import ace from 'ace-builds' import 'ace- ...

Fetching information from JSON file is unsuccessful

Attempting to retrieve data from a JSON source (). The goal is to extract information about each game including goals, location, and teams. However, the current code implementation seems to be ineffective. let url = "http://www.openligadb.de/api/getma ...

How can I deactivate the main color of the FormLabel when the focus is on the radio button group?

Is there a way to change the color of FormLabel to black instead of the primary color when the radio button group is focused? https://i.sstatic.net/h3hML.png const styles = { formLabel: { color: "#000" }, formLabelFocused: { color: "#000" ...

Enhancing React URLs

Our company deals with URLs in this format: http://helloworld.com/product?filter[category][0]=persian We aim to transform the URL into a cleaner version: http://helloworld.com/product-persian When additional filters are added to the current UR ...

Text transitions in a gentle fade effect, appearing and disappearing with each change

I want to create a smooth fade in and out effect for the text within a div when it changes or hides. After researching on Google and Stack Overflow, I found that most solutions involve adding a 'hide' CSS class and toggling it with a custom func ...

Guide on creating an AngularJS application using Yesod

After successfully developing a small app using Yesod, I am now focused on adding improved interaction to it with the help of AngularJS. Currently, it appears that the AngularJS support in Yesod is still in an experimental phase. Additionally, the documen ...

Error: react-router v4 - browserHistory is not defined

I'm diving into the world of creating my very first React app within Electron (also my first experience with Electron). I have two routes that need to navigate from one to another. Here's the code snippet I am using: Root ReactDOM.render( < ...

Utilizing the $skip parameter in the SharePoint 2013 RESTful API

Excuse my lack of experience with REST, as I am new to it. Currently, I am utilizing SP2013 Odata (_api/web/lists/getbytitle('<list_name>')/items?) in order to retrieve the contents of a list. This particular list contains 199 items, so I ...