如何使用 Webdriver IO 填充登录提示?

How to fill login prompt with Webdriver IO?

我正在使用 OCLIF. In one of the commands, I need to simulate a couple of clicks on a web page (using the WebdriverIO framework 开发 CLI)。在您能够到达所需页面之前,会重定向到带有登录提示的页面。当我使用与 browser.getAlertText()browser.sendAlertText()browser.acceptAlert 等警报相关的 WebdriverIO 方法时,我总是会收到错误 no such alert.

作为替代方案,我尝试在显示登录提示的页面上获取 URL。使用 URL,我想做一些类似 browser.url(https://<username>:<password>@<url>) 的事情来绕过提示。但是,当我在该页面上时,browser.url() returns chrome-error://chromewebdata/ 和 URL 一样。我猜是因为焦点在提示上并且没有 URL。在进入该页面之前,我也不知道 URL。重定向时,在我需要的URL中添加了一个包含token的查询字符串参数。

提示截图:

是否可以使用 WebdriverIO 处理这种情况?如果是这样,怎么做?

您走在正确的轨道上,可能需要进行一些微调才能使其正常工作。

首先,关于 chrome-error://chromewebdata 错误,引用 Chrome DOC:

If you see errors with a location like chrome-error://chromewebdata/ in the error stack, these errors are not from the extension or from your app - they are usually a sign that Chrome was not able to load your app.

When you see these errors, first check whether Chrome was able to load your app. Does Chrome say "This site can't be reached" or something similar? You must start your own server to run your app. Double-check that your server is running, and that the url and port are configured correctly.

很多词总结为:Chrome 无法加载您在 browser.url() 命令中使用的 URL。

我在 The Internet - Basic Auth 页面上尝试了自己。它就像一个魅力。

  1. URL 没有基本身份验证凭据:

  1. URL 具有基本身份验证凭据:

  1. 使用的代码:
  it('Bypass HTTP basic auth', () => {
    browser.url('https://admin:admin@the-internet.herokuapp.com/basic_auth');
    browser.waitForReadyState('complete');
    const banner = $('div.example p').getText().trim();
    expect(banner).to.equal('Congratulations! You must have the proper credentials.');
  });

我要做的是手动执行每个步骤,尝试在您使用的脚本中模拟相同的流程。从历史上我可以告诉你,我处理了一些 HTTP 网络应用程序,这些应用程序在发出基本身份验证 browser.url() 调用后需要 刷新


解决此问题的另一种方法是使用一些自定义浏览器配置文件 (Firefox | Chrome) . I know I wrote a tutorial on it somewhere on SO, but I'm too lazy to find it. I reference a similar post .

简而言之,在隐身模式下手动完成基本身份验证流程(使用凭据登录)window(以隔离配置).在该会话的另一个选项卡中打开 chrome://version/ 并存储 配置文件路径 的内容。该文件夹将保留您的所有会话并保留 cookie 和其他浏览器数据。

最后,在您的 currentCapabilities 中,通过 '--user-data-dir=/path/to/your/custom/profile 更新特定于浏览器的选项以使用自定义配置文件启动会话。它应该看起来像这样:

'goog:chromeOptions': {
    args: [
        '--user-data-dir=/Users/iamdanchiv/Desktop/scoped_dir18256_17319',
    ],
}

祝你好运!