puppeteer: 在多个 shadowRoot 中的输入字段内输入。

huangapple go评论122阅读模式
英文:

puppeteer: type inside input field which is in multiple shadowRoot's

问题

我有一个名为address-search的Web组件,其中包含输入字段,而这个组件位于另一个名为move-property的Web组件内部。我正在我的Angular应用程序中使用move-property,现在我想在address-search的输入字段中键入并调用Google Map API或某个函数。

我的代码

import * as puppeteer from 'puppeteer';

const delay = (milliseconds: number | undefined) =>
  new Promise((resolve) => setTimeout(resolve, milliseconds));

describe('workspace-project App', () => {
  let browser: puppeteer.Browser;
  
  it('检查标记是否在DOM中', async () => {
    browser = await puppeteer.launch({
      headless: false,
    });
    const page = await browser.newPage();
    await page.goto('http://localhost:4200');
    
    const comp = await page.evaluate(() => (document.querySelector('body > app-root > main > nest-container > app-home > nest-tiles > move-property-tile')?.shadowRoot?.querySelector('nest-tile > nest-btn') as HTMLElement)?.click());
    delay(4000);
    
    const comp2 = await page.evaluate(() => document.querySelector('body > app-root > main > nest-container > app-home > nest-tiles > div')?.innerHTML);
    
    delay(4000);

    const comp3 = await page.evaluate(() => document.querySelector('body > app-root > main > nest-container > app-home > nest-tiles > div > move-property')?.shadowRoot?.querySelector('nest-popup > div.address-input > address-search'));

    if (comp3 !== null && comp3 !== undefined) {
      await (comp3 as unknown as typeof page).type('form > #address', "LL");
    } else {
      null;
    }

    console.log(comp3);

    await browser.close();
  });
});

DOM SS
puppeteer: 在多个 shadowRoot 中的输入字段内输入。

我尝试过的

我知道我可以使用puppeteerpage.type()方法,但它与shadowRoot不兼容。我尝试获取父组件的shadowRoot,然后尝试使用comp.type(),但它会报错,因为type是页面的方法。

Puppeteer版本:19.7.2
Angular版本:14

我的期望

使用puppeteer在Angular应用程序中键入深度嵌套的输入字段。

英文:

I have a web component address-search which has input field and this component is inside another web component move-property and i am using the move-property in my angular app , now I want to type in the input field of address-search and call google Map-Api or some function

My code

 import * as puppeteer from 'puppeteer';

 const delay = (milliseconds: number | undefined) =>
 new Promise((resolve) => setTimeout(resolve, milliseconds));
 describe('workspace-project App', () => {
 let browser: puppeteer.Browser;
 it('Test to check if tag is on the DOM or not ', async () => {
 browser = await puppeteer.launch({
  headless: false,
  });
 const page = await browser.newPage();
 await page.goto('http://localhost:4200');
 const comp = await page.evaluate(() => (document.querySelector('body > app-root > main > nest-container > app-home > nest-tiles >move-property-tile')?.shadowRoot?.querySelector('nest-tile > nest-btn') as HTMLElement)?.click());
  delay(4000);
  const comp2 = await page.evaluate(() => document.querySelector('body > app-root > main > nest-container > app-home > nest-tiles >div')?.innerHTML);
// expect(comp2).not.toBeNull()
delay(4000);

const comp3 = await page.evaluate(() => document.querySelector('body > app-root > main > nest-container> app-home > nest-tiles >div>move-property')?.shadowRoot?.querySelector('nest-popup > div.address-input>address-search'));


comp3 !== null && comp3 !== undefined ? await (comp3 as unknown as typeof page).type('form > #address', "LL") : null

console.log(comp3);

 await browser.close();
 
});});

DOM SS
puppeteer: 在多个 shadowRoot 中的输入字段内输入。

What I tried

I know I can use puppeteer page.type() method but it's not working with shadowRoot and I tried getting the shadowRoot of parent component and try comp.type() but it say error type is not a function as because its a function for page only

Puppeteer version: 19.7.2
Angular version : 14

What i expect

Type in the input field which is multiple shadowRoot deep, using puppeteer in angular app

答案1

得分: 0

所以我找到了答案

    async function typeInInput(inputElement) {
      inputElement.setAttribute('type', 'text');
    
      inputElement.focus();
      inputElement.value = '';
      const numberString = "Some Text";
      for (let i = 0; i < numberString.length; i++) {
        const digit = numberString[i];
    
        const keyboardEvent = new KeyboardEvent('keydown', { key: digit });
        inputElement.dispatchEvent(keyboardEvent);
    
        inputElement.value += digit;
    
        const inputEvent = new InputEvent('input', {
          inputType: 'insertText',
          data: digit,
        });
        inputElement.dispatchEvent(inputEvent);
    
        const changeEvent = new Event('change');
        inputElement.dispatchEvent(changeEvent);
    
        await new Promise(resolve => setTimeout(resolve, 50));
      }
    
    }

我们只需要用我们的目标元素调用这个函数
英文:

So i found the answer

async function typeInInput(inputElement) {
  inputElement.setAttribute(&#39;type&#39;, &#39;text&#39;);

  inputElement.focus();
  inputElement.value = &#39;&#39;;
  const numberString = &quot;Some Text&quot;;
  for (let i = 0; i &lt; numberString.length; i++) {
    const digit = numberString[i];

    const keyboardEvent = new KeyboardEvent(&#39;keydown&#39;, { key: digit });
    inputElement.dispatchEvent(keyboardEvent);

    inputElement.value += digit;

    const inputEvent = new InputEvent(&#39;input&#39;, {
      inputType: &#39;insertText&#39;,
      data: digit,
    });
    inputElement.dispatchEvent(inputEvent);

    const changeEvent = new Event(&#39;change&#39;);
    inputElement.dispatchEvent(changeEvent);

    await new Promise(resolve =&gt; setTimeout(resolve, 50));
  }

}

We just need to call this function with our target element

huangapple
  • 本文由 发表于 2023年3月7日 19:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定