1

I want to use an element across a test case

Call an element across test case: orderIDReplace is element from the selected element in test create order (fixture app). I want to use/call orderIDReplace in test receive order (fixture backend)

fixture `app`
.page `https://example.com/`
.beforeEach(async t => {
await t
.click(`#username`)
.typeText(`#username`, `test`, {paste : true})  
.click(`#password`)
.typeText(`#password`, `test`, {paste : true})  
.click('#submit')
.wait(3000)

})

test('Create Order', async t => {
.await t
......
.click(Selector('div').withAttribute('class','vBtnContent').withText('Apply'))

let orderID = await Selector('p').withAttribute('class', 'g-invoice--code').nth(0).innerText;
let orderIDReplace = (lib.replaceCharacter(orderID));

})

fixture `backend`
.page `https://contoh.com/`
.beforeEach(async t => {
await t
.click(`#name`)
.typeText(`#name`, `coba`, {paste : true})  
.click(`#password`)
.typeText(`#password`, `coba`, {paste : true})  
.click('#submit')
.wait(3000)

})

test('receive order', async t => {
.await t
.click('#txtSearch')
.typeText('#txtSearch', orderIDReplace, {paste: true})
.click('#filter')  

Expected result: The result is order id

Actual result: Error: The "text" argument is expected to be a non-empty string, but it was "".

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
herawati
  • 11
  • 2
  • 1
    It's not clear from your question what behavior you want to achieve. Please take a look at [a fixture context object](https://devexpress.github.io/testcafe/documentation/test-api/test-code-structure.html#sharing-variables-between-fixture-hooks-and-test-code) functionality. It might suit your needs. If not, describe your scenario in greater detail. The full code of your tests will be very helpful. – aleks-pro Sep 26 '19 at 09:17
  • thanks for your advice @AleksandrProkhorov edited my post above – herawati Sep 27 '19 at 02:58

1 Answers1

1

You can define your variable outside the test and use it in several tests:

import { Selector } from 'testcafe';

let title = '';

fixture('MyFixture')
    .page('https://devexpress.github.io/testcafe/');

test('Test 1', async t => {
    title = await Selector('.title').innerText;
});

test('Test 2', async t => {
    console.log(title);
});
Dmitry Ostashev
  • 2,305
  • 1
  • 5
  • 21