0

当我按照jest-expo-puppeteer 文档jest-expo-puppeteer中的指南运行一个非常简单的示例测试时,我不断收到奇怪错误的日志,而且似乎找不到任何预期的元素。所以我尝试打印出页面:

// Import the config so we can find the host URL
import config from '../../jest-puppeteer.config'

let response

beforeEach(async () => {
  // Jest puppeteer exposes the page object from Puppeteer
  response = await page.goto(config.url)
})

it(`should have welcome string`, async () => {
  const html = await page.evaluate(() => document.documentElement.outerHTML)
  console.log(html)
  await expect(page).toMatchElement('div[data-testid="welcome"]', { text: 'Welcome!' })
})

页面结果显示文本Oh no! It looks like JavaScript is not enabled in your browser.

...
      <body>
          <!-- 
            A generic no script element with a reload button and a message.
            Feel free to customize this however you'd like.
          -->
          <noscript>
            <form
              action=""
              style="background-color:#fff;position:fixed;top:0;left:0;right:0;bottom:0;z-index:9999;"
            >
              <div
                style="font-size:18px;font-family:Helvetica,sans-serif;line-height:24px;margin:10%;width:80%;"
              >
                <p>Oh no! It looks like JavaScript is not enabled in your browser.</p>
                <p style="margin:20px 0;">
                  <button
                    type="submit"
                    style="background-color: #4630EB; border-radius: 100px; border: none; box-shadow: none; color: #fff; cursor: pointer; font-weight: bold; line-height: 20px; padding: 6px 16px;"
                  >
                    Reload
                  </button>
                </p>
              </div>
            </form>
          </noscript>
          <!-- The root element for your Expo app. -->
          <div id="root"><div class="css-view-1dbjc4n r-flex-13awgt0 r-pointerEvents-12vffkv"><div class="css-view-1dbjc4n r-flex-13awgt0 r-pointerEvents-12vffkv"></div></div></div>
        <script src="/static/js/bundle.js"></script>
      
      </body></html>
...

无头执行和浏览器内执行都会发生这种情况。

所以看起来Javascript已关闭。该page对象是由内部创建的jest-expo-puppeteer,所以我想知道:

  • 如何在 中打开 JavaScript jest-expo-puppeteer
  • 默认情况下不应该打开吗?也许我在设置中错过了一些东西?
4

1 回答 1

0

我自己找到了答案:

  • 内容Oh no! It looks like JavaScript is not enabled in your browser.始终存在于 react-native 文件中。它似乎作为备份服务器(如果浏览器确实没有启用 JavaScript,它将被显示)。
  • 因此无需打开 JavaScript。事实上 JavaScript 默认是打开的。
  • 我无法检测到页面中的元素的原因是 JavaScript 执行尚未完成。当我输入等待语句时,它可以正常工作,例如:
it(`should have welcome string`, async () => {
  const welcome = await page.waitForSelector('div[data-testid="welcome"]')
  const text = await (await welcome.getProperty('innerHTML')).jsonValue()
  await expect(text).toMatch('Welcome!')
})
于 2020-09-18T07:13:56.993 回答