1

As part of my nightwatchjs test script, I have the following code

var dealerName = sellerWebsiteAddressQueryPage.Website.filter(val => typeof val === 'string')

and what I'd like to do is use this as part of an assertion, as below

.assert.elementPresent('li.contact-seller__action a[href*="dealerName"]');

But I can't seem to find the correct way to include the variable dealerName within this assertion.

Any help would be greatly appreciated.

2
  • 2
    Just use string concatenation or template strings. Commented Aug 14, 2019 at 21:07
  • 1
    The same way you'd include a variable inside any other JavaScript string. Commented Aug 14, 2019 at 21:07

2 Answers 2

1

try

.assert.elementPresent(`li.contact-seller__action a[href*="${dealerName}"]`);
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, this works. Many thanks @Kamil Kiełczewski
1

I'm not quite sure how your two expressions fit together, so here I assume that dealerName comes from the array you're filtering. Use querySelector, Array.filter, and template literals to get the dealerNames that fulfills your condition.

const sellerWebsiteAddressQueryPage = {
  Website: [1, "Alice", undefined, "Bob"]
};
const dealerNames = sellerWebsiteAddressQueryPage.Website.filter(
  val => typeof val === "string"
).filter(dealerName =>
  document.querySelector(`li.contact-seller__action a[href*="${dealerName}"]`)
);

console.log(dealerNames);
<ul>
  <li class="contact-seller__action">
    <a href="site.com/dealers/Bob">Bob</a>
  </li>
<ul>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.