Selenium 简明教程

Selenium IDE - Emitting Code

Selenium IDE 是一个重要的工具,因其录制和回放功能而被广泛使用。它由两部分组成,即使用操作和事件在浏览器中回放以及使用命令行运行器使用命令行模式回放。

Selenium IDE is an important tool used extensively for its record and play back feature. It comprises of two critical components namely playback within the browser using the actions and events and playback using the command-line mode with the help of the command-line runner.

Selenium SIDE Runner Environment

Selenium 运行器建立在 Node 上。它可以用来发布代码。其他要求包含 −

The Selenium runner is built on Node. This can be used for emitting the code. The other requirements include −

  1. Node.js Version 8 and the Above Versions

  2. Jest

  3. Jest Environment Selenium

  4. Selenium Webdriver

How to Emit Code?

在发布代码时,需要小心处理某些措施。因为只有插件才会发布代码。它也不负责排放流。为了确保插件不会相互干扰执行,采取了某些措施 −

While emitting the code, certain measures need to be taken care of. As the plugins are not the only ones that are emitting the code. It also does not take care of the flow of emissions. To confirm that the plugins do not interfere in each other’s execution, certain measures are taken −

Avoid Using Return

使用 return 关键字之后的代码永远不会被执行,并且会停止其他插件的工作。例如,

The code after the usage of return keyword is never executed and stop the working of other plugins. For example,

return promise1();
plugin1func(); // after the usage of above return code, this is unreachable.

相反,下面的解决方案使用 await 函数可以正常工作 −

Instead the below solution with the await function works −

await promise1();
plugin1func(); // this should work

Avoid Defining Variables Globally

在全局级别定义变量表明,如果另一个插件或 Selenium IDE 已经定义了相同的变量,则会生成错误消息或使调试变得困难。例如,

Defining variables at the global level points to the fact that if another plugin or Selenium IDE has defined the same variable, either an error message will be generated or make debugging difficult. For example,

  1. store | button | ele

  2. plugin click | button

  3. assert element present | xpath=${ele}

在定义变量时,代码将为:

On defining the variable, the code will be −

let ele = "button";
let ele = await driver.findElement();
await ele.click();
expect(ele).toBePresent(); // ambiguity on which variable

相反,使用 Promise 的 then 函数的以下解决方案有效:

Instead the below solution with the then function of Promise works −

let ele = "button";
await driver.findElement().then(ele => {
  return ele.click();
});
expect(ele).toBePresent(); // hold the correct defined button

Conclusion

这就是我对 Selenium IDE - 代码发射教程的全面理解。我们从介绍 Selenium SIDE Runner 环境和如何发射代码开始。这使你具备对 Selenium IDE 代码发射的深入了解。明智的做法是继续练习你所学到的内容,并探索与 Selenium 相关的其他内容,以加深你的理解并拓展你的视野。

This concludes our comprehensive take on the tutorial on Selenium IDE - Emitting Code. We’ve started with describing Selenium SIDE Runner Environment, and How to emit code. This equips you with in-depth knowledge of the Selenium IDE Emitting Code. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.