browser.pause() 和 browser.enterRepl() 有什么区别?

What is the difference between browser.pause() and browser.enterRepl()?

量角器中有browser.pause() function:

Beta (unstable) pause function for debugging webdriver tests. Use browser.pause() in your test to enter the protractor debugger from that point in the control flow.

element(by.id('foo')).click();
browser.pause();
// Execution will stop before the next click action.
element(by.id('bar')).click();

还有一个鲜为人知的 - browser.enterRepl():

Beta (unstable) enterRepl function for entering the repl loop from any point in the control flow. Use browser.enterRepl() in your test. Does not require changes to the command line (no need to add 'debug').

element(by.id('foo')).click();
browser.enterRepl();
// Execution will stop before the next click action.
element(by.id('bar')).click();

从提供的文档和示例中可以看出,它们都是用于调试测试的。但是,目前还不清楚,两者之间有什么区别。

我们什么时候应该使用 pause() 什么时候应该使用 enterRepl()

docs 中对此进行了概括性的解释,但我会尝试更深入一些。

Protractor 有 two modes 用于调试:DebuggerReplCommandRepl

Repl 这里代表 Read-eval-print-loop 这通常意味着无论你输入什么命令,它都会得到正确的评估离开当前上下文,您会立即得到结果。例如,Chrome Developer Tools 中的控制台有点像 REPL for Chrome's JavaScript/DOM 的实现,或者当你在终端中 运行 node 时,你会得到一个 REPL 对于 Node.js 的 JavaScript 上下文 - 您可以键入命令并获得结果。


当您使用 browser.pause() 时,您正在激活 DebuggerRepl。它为您带来一个 Repl ,您可以在其中执行此模式的命令。您通常会在终端中看到以下命令列表:

press c to continue to the next webdriver command
press d to continue to the next debugger statement
type "repl" to enter interactive mode
type "exit" to break out of interactive mode
press ^C to exit

因此您可以使用 c 命令转到下一个 WebDriver 命令,或者使用 d 命令跳转到测试中的下一个 browser.pause() 语句。它们会在您使用时立即执行。所以这种模式基本上允许您 跳过页面状态 并探索结果。 (注意:此模式提供 more commands;它们确实有效,但我不确定它们输出的含义是什么以及它们是否对 Protractor 用户有用。)


当您使用 browser.enterRepl() 时,您正在激活 CommandRepl 模式。它允许您使用 Protractor 方法,您将在测试中使用这些方法,但在交互模式下。您可以访问 elementbrowserprotractor 对象,因此您可以 运行 例如:

> $('.hello').getText();
> 'World'

它会立即将结果打印出来,所以它有点像沙盒,您可以在其中查询 DOM 当前页面状态 并查看结果。

您可能已经注意到,browser.pause() 命令列表中有一行:

type "repl" to enter interactive mode

表示当你处于DebuggerRepl模式时,你可以执行repl命令激活CommandRepl模式 当前页面状态,您刚刚 运行 browser.pause(),因此您可以像使用 [=18] 一样玩 DOM =].您可以使用 exit 命令返回 DebuggerRepl 模式。但是如果你使用browser.enterRepl()进入CommandRepl模式,你不能切换到DebuggerRepl模式.

此外,CommandRepl 模式 可以通过名为 elementExplorer 的功能激活。无需任何笔试即可使用;它只是在 CommandRepl 模式 .

中打开一个 URL

tl;博士

总而言之,我认为应该根据它们的称呼来使用它们。

browser.pause() - 我希望浏览器正好停在那个地方,这样我就可以看到页面上发生了什么。然后,根据我的命令,我希望它跳转到下一个状态,这样我就可以看到这里发生了什么。如果我需要当前状态的更多信息,我可以 运行 repl 并使用 Protractor API (browser, element, protractor) 进行调查。然后我可以 exit 这种模式并继续跳转状态。

browser.enterRepl() - 我希望浏览器恰好在那个地方暂停,让我使用 Protractor APIbrowser, element, protractor),我不需要能够在页面状态之间跳转。

量角器版本 5.3 和节点版本 8.10 不再支持 browser.pause()。更多信息 here

您有机会用 protractor async aswait 做您需要的事。