0

I have this in source on one page:

<span class="price-length price-length--4">666</span>

And I have this in source on another page

<span class="price-length price-length--7">777</span>

I want to grab that 666 value, store it somewhere, and compare to 777. How to do that using codeceptjs?

I tried I.grabValueFrom('some_xpath_to_value'), but I don't understand how to reuse it. How to see value that I.grabValueFrom function returns in codeceptjs?

paveltk
  • 3
  • 1
  • 4

3 Answers3

3

You can use a generator function to return values via 'yield' from functions like so:

Scenario('Yield', function* (I) {
  let value = yield I.grabValueFrom(some_xpath_to_value);


  let assert = require('assert');
  assert.equal(value, '777');
});
Garrett
  • 69
  • 4
2

All functions in Codeceptjs return Promises, not values. So to get value from I.grab... functions, you should get Promise result.

You can use Garret solution with yield. Or the same, but with async/await (required for Codecept Node.js 8.9.1 have async/await support)

const assert = require('assert');

Scenario('async', async function(I) {
  let value = await I.grabValueFrom(some_xpath_to_value);

  assert.equal(value, '777');
});
1

Use the console to check the grab method:

import assert from "assert";

Scenario("Example", async ({ I }) => {
  const firstValue = await I.grabValueFrom("some_xpath_to_value");
  console.log(firstValue);
  const secondValue = await I.grabValueFrom("some_xpath_to_value");
  console.log(secondValue);

  assert.ok(firstValue == secondValue, `Values don't match`);
});