Leverage Selenium WebDriver to validate a JavaScript variable through an assertion

During my Selenium WebDriver testing of a webpage, I encountered a situation where I needed to make an assertion on a JavaScript file but was uncertain about the process.

The specific assertion I wanted to make was regarding the length of the servers array, which should ideally be 2.

Within my JavaScript file named config.js, I have defined the following array that needs to be confirmed:

var location = location || {};
location.Config = {
    servers: [
        {name: "a"}, 
        {name: "b"}
   ]
}

In attempting to complete this assertion, I initially used the className method but unfortunately, it did not yield the desired outcome:

Assert.assertThat(webDriver
        .findElement(className("config.js"))
        .getAttribute("servers")
        .length(), 
    Matchers.is(2));

Answer №1

Great news!

By sending a JavaScript command to the browser and retrieving its output, we were able to solve the issue.

long totalServers = (long) ((JavascriptExecutor) webDriver).executeScript("return location.Config.servers.length");

Assert.assertThat(totalServers, Matchers.greaterThan((long) 0));

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 userscript is designed to function exclusively on pages originating from the backend, rather than on the frontend in a single-page application

I have a userscript that I use with Greasemonkey/Tampermonkey. It's set to run on facebook.com, where some pages are served from the backend during bootstrapping and others are loaded on the fly in the front-end using HRO, similar to how a Single Pag ...

Successive Alerts with Material-UI and React-Redux

After realizing that having multiple snackbars scattered across different components was messy, I decided to revamp my app by creating a single custom component that can be called using Redux to display any type of snackbar needed. Desired outcome: I exp ...

Explore how Next.js's getServerSideProps feature incorporates loading animations and improves

I have implemented getServerSideProps in my project's pages/post/index.js file: import React from "react"; import Layout from "../../components/Layout"; function Post({ post }) { console.log("in render", post); return ( <Layout title={pos ...

In Selenium Webdriver, there is a feature that allows for generating reports that specifically display only the failed methods and

During my Selenium TestNG course, I learned about various methods like method1 and method2. I made sure to include fail and success conditions for each method in my code. public class TestNGClass { public void method1(String value) throws Exception { ...

Has the recent update to the latest Google Chrome version (Version 87.0.4280.66 (Official Build) (64-bit)) included a change where the "chromedriver.exe" process is now named "Google Chrome"?

In previous versions of the chromedriver software, I used to observe a process labeled as chromedriver.exe in my task manager. However, in the newer version (87), it appears that this process has been renamed to Google Chrome. As a result, I would typical ...

Using Parseint in a Vue.js method

For instance, let's say this.last is 5 and this.current is 60. I want the sum of this.last + this.current to be 65, not 605. I attempted using parseInt(this.last + this.current) but it did not work as expected. <button class="plus" @click="plus"&g ...

successful callback after passport registration

router.post('/register', function(req, res, next){ var name = req.body.name; var email = req.body.email; var username = req.body.username; var password = req.body.password; var password2 ...

The jQuery draggable feature ceases to function after it has been dropped

I have a scenario with two divs, each housing a list of quantities and items. These items are draggable, and the div containing them is droppable. The condition here is if an item with the same name exists in the div, it cannot be dropped on that div again ...

Instructions for setting up a TestNG XML file to simultaneously initiate two drivers - the Chrome Driver and Android Driver - while utilizing QAF

Task Process: Launch a web browser to perform certain operations Use an Android device with Appium to carry out specific tasks. Note: Both of these steps need to be executed within a single test in TestNG XML. ...

I am having trouble installing the latest version of Bun on my Windows operating system

When attempting to install Bun on my Windows laptop using the command npm install -g bun, I encountered an error in my terminal. The error message indicated that the platform was unsupported and specified the required operating systems as darwin or linux w ...

Tips for handling catch errors in fetch POST requests in React Native

I am facing an issue with handling errors when making a POST request in React Native. I understand that there is a catch block for network connection errors, but how can I handle errors received from the response when the username or password is incorrec ...

Currently, I am attempting to retrieve text input through the use of AngularJS

Having trouble retrieving text input values using Angular JS? The console keeps showing undefined. <div ng-controller="favouritesController" class="col-xs-12 favList"> <input type="text" ng-model="newFav" ng-keyup= "add($event)" class="col-xs-1 ...

Tips for transferring an array between two applications using AngularJS

I have two applications, appA for front end and appB for admin end. In appA, I am building an array called queries using a service through a controller. However, when I try to retrieve this array list in a controller in appB, it appears empty. Everytime ...

What is the best way to execute npx commands without the need to add 'npx' before each

For instance, I am able to simply run gulp instead of having to type npx gulp. I can skip the npx and just use gulp How can I achieve this for my own package? Even though I've included mycommand in the package npm bin configuration, I still constan ...

Nuxt.js has exceeded the maximum call stack size

Recently, I started working on a nuxt.js/vue project using a pre-built starter template. However, I have been encountering numerous error messages such as "Maximum call stack size exceeded." It's quite challenging to pinpoint the exact source of these ...

Information failed to load into the datatable

i've implemented this code snippet to utilize ajax for loading data into a datatable. However, I'm encountering an issue where the data is not being loaded into the database. $('#new_table').DataTable({ "processing": true, "ser ...

How to Use Selenium Python to Locate and Print a Specific Cookie

I'm attempting to retrieve a specific value from a list of cookies. I attempted the following code, but it didn't work as expected: cookies_list = driver.get_cookies() for cookie in cookies_list: print(cookie['cookiename']) Any su ...

Lamenting the Perils of Losing AngularJS Rootscope Data upon Refresh

Currently, I am facing an issue in AngularJS 1.x. When I save a value in the $rootScope and pass to the next page or router, unfortunately, the $rootScope value gets lost upon refreshing the page (F5/Window Reload). I need a solution that doesn't inv ...

The Chevron icon is not pointing downwards even though it has already gone upwards

I want to toggle a chevron icon, but nothing seems to be happening. $("span:last").removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up"); When I add this code below the slideToggle function without an if-else section, the icon changes to ...

Guide on sending a post request to a PHP component

var table = $('#accessRequest').DataTable({ "dom": 'Blfrtip', "ajax": "Editor-PHP-1.5.1/php/editor-serverside.php", "columns": [ { //special column for detail view ...