如何在webdriver的browser.url调用中隐藏网页元素。

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

How to hide a webpage element from a webdriver browser.url call

问题

以下是您要翻译的内容:

"Our webdriver test needs to hide an element that is getting recorded multiple times in screenshots as the browser scrolls down the page. It is a floating element.

I have:

it('should save Food blog page screenshotsscreenshots', async () => {
const adjustedURL = new browser();
await adjustedURL
.url('en-au/blog/elements-and-templates-food-sustainability/')
.execute(() => {document.getElementByID("_hjSafeContext_7335155").style["opacity"] = "0";})
.pause(4000)
.saveFullPageScreen('AU-Food-blog', {});
});

but receive an error TypeError: browser is not a constructor.

If I use:

it('should save Food blog page screenshotsscreenshots', async () => {
await browser
.url('en-au/blog/elements-and-templates-food-sustainability/')
.execute(() => {document.getElementByID("_hjSafeContext_7335155").style["opacity"] = "0";})
.pause(4000)
.saveFullPageScreen('AU-Food-blog', {});
});

I get browser.url(...).execute is not a function

Edit2, perhaps the Intercom HTML changed, and I now need to target .intercom-lightweight-app:

it('should save Food blog page', async () => {
await browser.url('en-au/blog/elements-and-templates-food-sustainability/');
await browser.pause(7000)
await browser.execute(() => {document.querySelector(".intercom-lightweight-app").style.display = "none";});
await browser.execute(() => {document.querySelector(".intercom-lightweight-app").style.position = "relative";});
await browser.saveFullPageScreen('AU-Food-blog', {});
});

If I set the opacity of .intercom-lightweight-app-launcher to 0 manually, the Intercom widget becomes invisible, but the above changes to the webdriver code mean the Intercom widget is still baked into the screenshot."

英文:

Our webdriver test needs to hide an element that is getting recorded multiple times in screenshots as the browser scrolls down the page. It is a floating element.

I have:

 it('should save Food blog page screenshotsscreenshots', async () => {
    const adjustedURL = new browser();
    await adjustedURL
      .url('en-au/blog/elements-and-templates-food-sustainability/')
      .execute(() => {document.getElementByID("_hjSafeContext_7335155").style["opacity"] = "0";})
      .pause(4000)
      .saveFullPageScreen('AU-Food-blog', {});
  });

but receive an error TypeError: browser is not a constructor.

If I use:

  it('should save Food blog page screenshotsscreenshots', async () => {
    await browser
      .url('en-au/blog/elements-and-templates-food-sustainability/')
      .execute(() => {document.getElementByID("_hjSafeContext_7335155").style["opacity"] = "0";})
      .pause(4000)
      .saveFullPageScreen('AU-Food-blog', {});
  });

I get browser.url(...).execute is not a function

Edit2, perhaps the Intercom HTML changed, and I now need to target .intercom-lightweight-app:

  it('should save Food blog page', async () => {
    await browser.url('en-au/blog/elements-and-templates-food-sustainability/');
    await browser.pause(7000)
    await browser.execute(() => {document.querySelector(".intercom-lightweight-app").style.display = "none";});
    await browser.execute(() => {document.querySelector(".intercom-lightweight-app").style.position = "relative";});
    await browser.saveFullPageScreen('AU-Food-blog', {});
  });

If I set the opacity of .intercom-lightweight-app-launcher to 0 manually, the Intercom widget becomes invisible, but the above changes to the webdriver code mean the Intercom widget is still baked into the screenshot.

答案1

得分: 1

不像WebdriverIO可以链式调用浏览器方法。相反,您可以重复使用browser对象,并使用await关键字等待每个方法,如下所示:

  it('应保存Food博客页面的截图', async () => {
    await browser.url('en-au/blog/elements-and-templates-food-sustainability/');
    // intercom可能使用不同的类名加载,我们要选择所有类名
    var intercom = await browser.$('.intercom-app, .intercom-lightweight-app');
    // 等待最多30秒,直到元素被添加到DOM
    await intercom.waitForExist({ timeout: 30000 });
    await browser.execute(() => {document.querySelector(".intercom-app, .intercom-lightweight-app").style.opacity = "0";});
    await browser.pause(4000);
    await browser.saveFullPageScreen('AU-Food-blog', {});
    // 这里进行一些测试
  });
英文:

It doesn't look like you can chain browser methods with WebdriverIO. Instead you can reuse the browser object, and await each method, like so:

  it('should save Food blog page screenshots', async () => {
    await browser.url('en-au/blog/elements-and-templates-food-sustainability/');
    // intercom might be loaded with different class names, we want to select all of them
    var intercom = await browser.$('.intercom-app, .intercom-lightweight-app');
    // waits up to 30 seconds for the element to be added to DOM
    await intercom.waitForExist({ timeout: 30000 });
    await browser.execute(() => {document.querySelector(".intercom-app, .intercom-lightweight-app").style.opacity = "0";});
    await browser.pause(4000);
    await browser.saveFullPageScreen('AU-Food-blog', {});
    // some test here
  });

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

发表评论

匿名网友

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

确定