Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cy.window() to access clipboard immediately results in 'document is not focused' error #28488

Closed
aosama94 opened this issue Dec 10, 2023 · 16 comments
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period type: bug

Comments

@aosama94
Copy link

aosama94 commented Dec 10, 2023

Current behavior

I used cy.window() to acces the clipboard, and it doesn't let me do any cypress command till i use cy.wait()
and there is no option instead the wait:

    cy.window().then((win) => {
      win.navigator.clipboard.readText().then((data) => {
        // console.log(data);
        cy.wait(1000);
        cy.writeFile('cypress/genericTestData/importedService.json', data);
        cy.log(data);
      });
    });

If i used cy.log() without wait it will throw an error (document is not focused) so there is no way to use any cypress command inside it

if I used cy.get('element').type('test); it will not see this command or will not throw any errors just like it doesn't exist

conclusion:
cy.log(somthine). will throw error (document is not focused)
cy.get.type. will be considered as not existing command

I just need to get the clipboard text to use it ino an input filed to continue my testing

I hope that you got my point

Desired behavior

Should be any way to not show the error (document is not focused) instead of the cy.wait()!!

Test code to reproduce

    cy.window().then((win) => {
      win.navigator.clipboard.readText().then((data) => {
        // console.log(data);
        cy.wait(1000);
        cy.writeFile('cypress/genericTestData/importedService.json', data);
        cy.log(data);
      });
    });

Cypress Version

"cypress": "^13.6.0

Node version

v20.9.0

Operating System

Version 14.0 (23A344)

Debug Logs

No response

Other

No response

@saarimshaikh
Copy link

Can you please specify the path of file in which this code is written?

@aosama94
Copy link
Author

e2e folder for sure
/cypress/e2e/services/exportService/exportService.cy.js

and there is another point I forgot to mention

when i write this code in (it) it will not run any cy command inside the the window function
it will run only inside beforeEach with wait to prevent error document is not focused

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Dec 11, 2023

@aosama94 We changed some of the behavior around tab focusing in recent versions. I suspect this may have introduced the change in behavior: #28334

  • Could you try version 13.5.1 to see if the issue is resolved in that version?
  • What browser are you testing this in?
  • Is this during cypress run and cypress open?
  • Could you print the exact error message you are seeing or a screenshot of it?

I've been unable to reproduce the issue, so want to understand the circumstances for us to address it.

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Dec 11, 2023
@jennifer-shehane jennifer-shehane changed the title cy.window() cy.window() to access clipboard immediately results in 'document is not focused' error Dec 11, 2023
@jennifer-shehane
Copy link
Member

Hmm, I found this thread so now I wonder if this has always been a problem and isn't new: #18198 There is a workaround in that issue.

@aosama94
Copy link
Author

aosama94 commented Dec 12, 2023

This is the full code :

describe('Export Service', () => {
  const service = new ServicesPage();
  const util = new Util();

  before('login', () => {
    Cypress.on('uncaught:exception', () => false);
    util.navigate();
    util.login(loginCredentials.userName.user1, loginCredentials.password);
  });

  it('E2E Service Flow - Export Service', () => {
    Cypress.on('uncaught:exception', () => false);
    util.componentSelect(flowData.id);
    util.getStoredData((text) => {
      util.searchBy(flowData.searchBy.label, text);
    });
    util.editIcon(flowData.component).click();
    service.exportBtn().click();
    cy.get('div.ant-modal-content > div > section > div > div > div.overflow-guard > textarea').type('{esc}', { force: true });
    cy.window().then((win) => {
      win.navigator.clipboard.readText().then((data) => {
        // console.log(data);
        cy.wait(1000);
        cy.writeFile('cypress/genericTestData/importedService.json', data);
        cy.log(data);
      });
    });
  });
  it('E2E Service Flow - Export Service', () => {
    Cypress.on('uncaught:exception', () => false);

  });
});

In above implementation (The cy.window is inside it() ) the results will be like this :
look it didn't see the cy.writeFile() command
Screenshot 2023-12-12 at 1 03 34 PM

If we replace the it() with beforeEach() the results will be like this :
look it saw the cy.writeFile() command
Screenshot 2023-12-12 at 1 05 45 PM

so it wokrs with beforeEach() and didn't work with it()

and with beforeEach() if I remove the wait(1000) it will throw the folloing error
Screenshot 2023-12-12 at 1 16 49 PM

@aosama94
Copy link
Author

question here

  • Why it doesn't work inside it() ?? why it can't see the command ???
  • Why it only works inside beforeEach()?? why he saw the command here only !!
    it doesn't make sense!

And why wee need a wait ? and what we can use instead of wait here

@aosama94
Copy link
Author

Other thing related to the main code in the above comment

As you see the last it doesn't have any commands inside
if i decided to continue my testing at the last it it will throw the following error
Screenshot 2023-12-12 at 1 29 42 PM

I should continue my test in a new spec file !!
so this file will only contain beforeEach hook ?!!

@jennifer-shehane jennifer-shehane added stage: needs investigating Someone from Cypress needs to look at this type: bug and removed stage: needs information Not enough info to reproduce the issue labels Dec 19, 2023
@bharanidharan439
Copy link

any improvement in this issue?

@johanFuturice
Copy link

Hi - @jennifer-shehane I'm facing similar issues as reported here.

In this case we're writing text to the clipboard, which is then read in the actual test down the line. The issue however seems to be around cy.window() since this fails in the beforeEach() before we can read the clipboard content. The runs always fail with open mode (i.e. headed) when the window is not focused, but never fail in headless mode. I'm running on Windows with Chrome 121. I've upgraded to latest cypress 13.6.4 and this issue persists still (note: had to apply this fix as well #28392)

I've created the simplest possible use case that is fairly generic below. Note that the application of window.focus() after writing to clipboard seems to be the key to hold this together in headless mode (and headed with window focused). Without it the test always breaks with the Document is not focused error. Tried a bit of wait and other ideas but nothing here seems to resolve this.

describe('Create test that reliably fails with Document Not focused', () => {
  beforeEach(() => {
    // Allow clipboard access
    cy.wrap(
      Cypress.automation('remote:debugger:protocol', {
        command: 'Browser.grantPermissions',
        params: {
          permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
          origin: window.location.origin,
        },
      })
    );

    // Write any gibberish to clipbaord
    cy.window().then((window) => {
      window.navigator.clipboard.writeText('gibberish');
      window.focus(); // Important: without this the test will fail also in both headed (with focus) and headless mode
    });
    cy.visit('/');
  });

  it('Should not fail', () => {
    expect(true).to.eq(true);
  });
});

ends up with
image

Let me know if I can provide more details.

@jennifer-shehane
Copy link
Member

Could you all try to update to 13.6.6 and let us know if you're still experiencing this issue? We fixed an issue in 13.6.5 that was introduced in 13.6.0 and we want to understand if this has also resolved this issue. Thank you!

@jennifer-shehane jennifer-shehane added stage: awaiting response Potential fix was proposed; awaiting response and removed stage: needs investigating Someone from Cypress needs to look at this labels Feb 26, 2024
@johanFuturice
Copy link

Updated to 13.6.6 but unfortunately the same issue persists for me (with the same example test as in my prev post). The test only passes if the cypress browser window is focused (or in headless), but unfocused window does not

@jennifer-shehane
Copy link
Member

That's unfortunate. 😞

@jennifer-shehane jennifer-shehane added stage: needs investigating Someone from Cypress needs to look at this and removed stage: awaiting response Potential fix was proposed; awaiting response labels Feb 27, 2024
@carms-ng
Copy link

carms-ng commented Apr 2, 2024

Just to note that, updated to 13.7.1, I'm still experiencing the same issue!

@trananhchuong
Copy link

I found a solution using navigator.clipboard.writeText instead of calling the function from the window variable.

It worked for me.

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Feb 12, 2025
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period type: bug
Projects
None yet
Development

No branches or pull requests

8 participants