木偶:通过ID选择的输入字段返回未定义

时间:2020-02-04 13:46:20

标签: javascript node.js puppeteer jest-puppeteer

enter image description here

上面,您可以使用#account_email选择器查看输入元素。但是,等待选择和在此字段中键入的笑话功能测试每次都会失败。我不明白为什么。

下面是否存在语法错误?这种选择容易出错吗?欢迎解决此问题的任何建议。

// A functonal test file being run by jest.js
// jest-puppeteer

test('[functional] log into shopify"', async () => {

      const browser = await puppeteer.launch({headless: false});
      const page = await browser.newPage();

      // go to login page
      await page.goto("https://partners.shopify.com/1185756/apps/3207477/test", {waitUntil : "load"});
      console.log(page.url, `=====arrived at shopify login screen=====`);

      // fill and submit form
      const emailInput = await page.focus("#account_email");
      await emailInput.type(process.env.SHOPIFY_PARTNER_EMAIL);
// error seen in terminal
TypeError: Cannot read property 'type' of undefined

      22 |       // email screen
      23 |       const emailInput = await page.focus("#account_email");
    > 24 |       await emailInput.type(process.env.SHOPIFY_PARTNER_EMAIL);
         |                        ^
      25 |       

// 

1 个答案:

答案 0 :(得分:1)

focus不返回元素句柄。您可以执行以下操作:

const emailInput = await page.waitForSelector("#account_email");
await emailInput.focus();
await emailInput.type(process.env.SHOPIFY_PARTNER_EMAIL);