testcafe icon indicating copy to clipboard operation
testcafe copied to clipboard

InvalidStateError: XMLHttpRequest has an invalid context.

Open edenadler opened this issue 8 years ago • 4 comments

Are you requesting a feature or reporting a bug?

Reporting a bug

What is the current behavior?

Sometimes (not consistent), this error: Error in Role initializer - An error occurred in ClientFunction code: InvalidStateError: XMLHttpRequest has an invalid context. occurs when trying to use a ClientFunction inside of a Role. When the error does appear, it happens to a batch of tests one after another, while other tests continue to run without this error. I am running the tests with 4 concurrent browser instances.

What is the expected behavior?

ClientFunction should behave consistently in Role

How would you reproduce the current behavior (if this is a bug)?

Errors happen when run on Firefox inside a docker container. I was not able to reproduce this outside of the container in Firefox. It's possible though that it doesn't have to do with the test being run in the container, and that just by chance it didn't reproduce for me. But figured it was worth noting.

After researching this a bit it seems like its an issue with the timing of the xhr's open(), onreadystatechange(), and send() methods (https://xhr.spec.whatwg.org/).

Provide the test code and the tested page URL (if applicable)

Tested page URL: (unfortunately cannot provide)

Test code

//test.js
...
const signin = new SignIn();
await signin.handleSignIn();
...
//sign-in.js

export default class SignIn {
     async handleSignIn() {
           await t
               .useRole(signInRole)
               .navigateTo(_____url with same origin______)
    }
}
//utils.js

export const signInRole = Role(_____url with same origin______, async t => {

    const headers = await getSignInCredentials()
        .then(result => result.headers)
        .catch(error => console.log(error))

    await t.expect(headers).ok()

    const cookieSetter = function setCookies(cookieObj) {
        const cookies = cookieObj['set-cookie']; 
        for (let value of cookies) {
            const cookie = value.split(';')[0];
            document.cookie = cookie;
        }
        return true
    }

    const setSignInCookies = ClientFunction(() => {
        return cookieSetter(headers);
    }, { dependencies: { cookieSetter, headers }});

    const cookiesSet = await setSignInCookies();    //<--- here is where the error is occurring 

    await t.expect(cookiesSet).ok()

}, { preserveUrl: true });
//api.js

const token = ClientFunction(() => document.getElementsByName('csrf-token')[0].getAttribute('content'));

axios.defaults.headers.common['X-CSRF-Token'] = token();
axios.defaults.headers.common['Content-Type'] = 'application/json';
axios.defaults.headers.common.Accept = 'application/json';
axios.defaults.withCredentials = true;
axios.defaults.auth = { username: ________ , password: _____________ };
axios.defaults.timeout = 10000;

export async function getSignInCredentials() {
    return await axios.post(_____route where url has same origin______)
        .then(response => response)
        .catch((error) => console.log(error));
}

Specify your

  • operating system: Linux
  • testcafe version: 0.20.5
  • node.js version: 8.10.0

edenadler avatar Aug 19 '18 12:08 edenadler

Hi, i don't use Roles, but i have exactly the same issue with running tests on Firefox inside of the Docker container.

  • Firefox 61.0.0 / Mac OS X 10.13.0
  • testcafe v. 0.21.1
  • node.js v. 10.9.0

it starts happening since i'm using function to set cookies in beforeEach hook

export async function setCookie() { const setCookie = ClientFunction(() => { document.cookie = "multiproxy=blabla" }); const setter = await setCookie(); return setter; }

it works without errors on Chrome v. 68.0.3440

Sashikus avatar Sep 07 '18 14:09 Sashikus

@edenadler @Sashikus I am afraid this information is not sufficient to find the cause of the issue. Could you please provide a page or a small example so that we can reproduce it on our side?

miherlosev avatar Sep 17 '18 15:09 miherlosev

i'll try to provide more info next days

Sashikus avatar Sep 17 '18 15:09 Sashikus

I will close the issue because no activity has been encountered for a long time. Feel free to reopen the issue if the problem persists and you can provide an example to reproduce it.

miherlosev avatar Oct 02 '18 12:10 miherlosev