create-selector-query.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
  2. const isMP = platformInfo.startsWith('mp')
  3. const isWeb = platformInfo.startsWith('web')
  4. const PAGE_PATH = '/pages/API/create-selector-query/create-selector-query'
  5. const RECT_LEFT = 15;
  6. const RECT_WIDTH = 150;
  7. const RECT_HEIGHT = 100;
  8. describe('nodes-info', () => {
  9. let page
  10. beforeAll(async () => {
  11. page = await program.reLaunch(PAGE_PATH)
  12. await page.waitFor('view')
  13. })
  14. it("screenshot", async () => {
  15. const image = await program.screenshot({ fullPage: true });
  16. expect(image).toSaveImageSnapshot();
  17. })
  18. it('get-root-node-info', async () => {
  19. // 测试 class 选择器
  20. await getRootNode('.page')
  21. // 测试 id 选择器
  22. await getRootNode('#page')
  23. // 测试 标签 选择器
  24. // await getRootNode('page')
  25. })
  26. it('get-node-info', async () => {
  27. const btnGetNodeInfo = await page.$('.btn-get-node-info')
  28. await btnGetNodeInfo.tap()
  29. await page.waitFor(50)
  30. const data = await page.data()
  31. // TODO 和浏览器的计算存在差异
  32. const nodeInfo = data.nodeInfoList[0]
  33. expect(Math.round(nodeInfo.left)).toBe(RECT_LEFT)
  34. expect(Math.round(nodeInfo.width)).toBe(RECT_WIDTH)
  35. expect(Math.round(nodeInfo.height)).toBe(RECT_HEIGHT)
  36. })
  37. it('get-all-node-info', async () => {
  38. const btnGetAllNodeInfo = await page.$('.btn-get-all-node-info')
  39. await btnGetAllNodeInfo.tap()
  40. await page.waitFor(50)
  41. const data = await page.data()
  42. const nodeInfo1 = data.nodeInfoList[0]
  43. expect(Math.round(nodeInfo1.left)).toBe(RECT_LEFT)
  44. expect(nodeInfo1.top > 220).toBe(true)
  45. expect(Math.round(nodeInfo1.width)).toBe(RECT_WIDTH)
  46. expect(Math.round(nodeInfo1.height)).toBe(RECT_HEIGHT)
  47. const nodeInfo2 = data.nodeInfoList[1]
  48. expect(nodeInfo2.left > 180).toBe(true)
  49. expect(nodeInfo2.top > 220).toBe(true)
  50. expect(Math.round(nodeInfo2.width)).toBe(RECT_WIDTH)
  51. expect(Math.round(nodeInfo2.height)).toBe(RECT_HEIGHT)
  52. })
  53. if(!isMP) {
  54. // 小程序端启用了虚拟host,无法获取到子组件
  55. it('get-node-info-child', async () => {
  56. const child = await page.$('.node-child')
  57. const childData = await child.data()
  58. console.log('get-node-info-child.childData.top', childData.top);
  59. expect(childData.top > 100).toBe(true)
  60. })
  61. }
  62. it('multi-child', async () => {
  63. const pageData = await page.data()
  64. expect(pageData.selectCount).toBe(1)
  65. expect(pageData.selectAllCount).toBe(2)
  66. })
  67. // #ifdef APP
  68. //检测onResize获取BoundingClientRect信息是否有效
  69. /* it('check_resizeRectValid', async () => {
  70. const resizeRectValid = await page.data('resizeRectValid')
  71. expect(resizeRectValid).toBe(true)
  72. }) */
  73. // #endif
  74. if (!(isWeb || isMP)) {
  75. it('test fields', async () => {
  76. const pageData = await page.data()
  77. expect(pageData.fieldsResultContainNode).toBe(true)
  78. })
  79. it('test node', async () => {
  80. const pageData = await page.data()
  81. expect(pageData.nodeResultContainNode).toBe(true)
  82. })
  83. }
  84. })
  85. async function getRootNode(selector) {
  86. const page = await program.currentPage()
  87. await page.setData({
  88. rootNodeInfo: null,
  89. })
  90. await page.waitFor(100)
  91. await page.callMethod('getRootNodeInfo', selector)
  92. await page.waitFor(100)
  93. const data = await page.data()
  94. expect(data.rootNodeInfo != null).toBe(true)
  95. }