Search code examples
javascriptnightwatch.js

Extracting part of element source using nightwatchjs


I'm currently using nightwatchjs to create my test scripts and have hit a stumbling block.

What I need to do it 'extract' a certain part of an element attribute, in order to use this value later on in my tests.

So, here is the element under test;

<img class="rsTmb" src="https://parkers-images.dev.bauersecure.com/gallery-thumbnail/wp-images/20320/cut-out/79x79/lexus-ux-001.jpg

and what I'd like to do is extract the lexus-ux-001.jpg part of the src attribute so I can use it later.

Firstly, is it possible to extract parts of an attribute using nightwatch and/or javascript, and if so any ideas how I would go about this?

Many thanks.


Solution

  • This is possible using the below two steps:

    1. Get the href attribute value using getAttribute method from nightwatchjs. The example in the page already have the code to do that.

    2. You can then process your URL using js methods. I found this to be a simple way.