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

Feature Request: findAll #10

Open
msteller-connyun opened this issue Feb 26, 2019 · 9 comments
Open

Feature Request: findAll #10

msteller-connyun opened this issue Feb 26, 2019 · 9 comments
Labels
enhancement New feature or request feature: wip

Comments

@msteller-connyun
Copy link

At the moment it’s only possible to query a single element, after the find method based on querySelector.

For testing components with tables, or lists it would be very usefull if there would be an findAll all query, based on querySelectorAll which returns an array.

Example:

    const result = await (await showroom.utils.findAll('//td')).getProperty('innerText‘);
    expect(result).toBe(['hello', 'world']);
@msteller-connyun
Copy link
Author

I found a workaround with https://www.npmjs.com/package/query-selector-shadow-dom

const showroom = require('showroom/puppeteer')();
const path = require('path');

describe('Calendar graph', async () => {

  beforeAll(async () => {
    await showroom.start();
    await showroom.page.addScriptTag({
      path: path.join(process.cwd(), 'node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js')
    })
  });

  beforeEach(async () => {
    await showroom.utils.setTestSubject('calendar-graph');
  });

  afterAll(async ()=>{
    await showroom.stop();
  });

  test('should show data', async () => {
    await showroom.utils.trigger('initStatusMapping');
    await showroom.utils.trigger('initData');

    const foo = await showroom.page.evaluate(()=>querySelectorShadowDom.querySelectorAllDeep('calendar-graph .css-td').map(item=>item.innerText));

    expect(foo).toBe(['hello', 'world']);
  });
});

``

@eavichay eavichay added the enhancement New feature or request label Feb 27, 2019
@eavichay
Copy link
Owner

eavichay commented May 23, 2019

@msteller-connyun I'm glad you found a workaround. If I would add findAll with shadow path then the recursive search would be very laggy and might take too much time.

Showroom is meant for running unit-tests on a single-component level.
If you would like to add utils I would be happy for PRs or suggestions (via issues).

Happy testing.

P.S. I have started working on findAll with one major limitation: until the last '//' shadow path in the search it performs showroom.find and then concatenates the rest of the query as querySelectorAll. Will branch this pretty soon.

@msteller-connyun
Copy link
Author

I think your major limitation is fine for me, because I need findAll for unit testing data strucutres, like tables.

@eavichay
Copy link
Owner

eavichay commented Sep 5, 2019

@msteller-connyun I would like to know what was your technique of injecting the deep-query script. Was it in the .showroom/config.js file?

@msteller-connyun
Copy link
Author

Hello @eavichay

I did it this way

  beforeAll(async () => {
    await showroom.start();
    await loadLibs();
  });

  test('should show banner', async () => {
    const expectedResult = ['no-display'];
    await showroom.utils.trigger('init');
    const result = await showroom.page.evaluate(() => [...querySelectorShadowDom.querySelectorDeep('cy-google-analytics .ga-banner').classList]);
    expect(result).toEqual(expect.not.arrayContaining(expectedResult));
  });

async function loadLibs() {
  return new Promise(async (resolve) => {
    await showroom.page.addScriptTag({
      path: path.join(process.cwd(), 'node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js')
    });
    resolve();
  });
}


@eavichay
Copy link
Owner

eavichay commented Sep 5, 2019

Awesome.
Basically any utils/scripts can be loaded using the config.js file. You can declare script sources that will be added as tags in the HTML.

I know there are yet undocumented features, but this eventually will be fully documented, perhaps with help from the community.

Thanks for the heads-up.

@msteller-connyun
Copy link
Author

yeahh... but it’s still painful to use for the selectors an evaluate().
The load a third party library for the findAll is just a workaround.

@eavichay
Copy link
Owner

eavichay commented Sep 5, 2019

👍

@eavichay eavichay closed this as completed Sep 5, 2019
@msteller-connyun
Copy link
Author

So.. you closed that ticket. Does this mean there will be no native findAll support in showroom?

@eavichay eavichay reopened this Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature: wip
Projects
None yet
Development

No branches or pull requests

2 participants