Selenium 简明教程
Selenium IDE - Code Export
Selenium IDE 为应用程序记录一个测试后生成的代码可以导出到其他语言,如 Java 、 C# 等。生成的代码可以导入到其他 IDE(如 Eclipse、IntelliJ 等)中,并用于其他自动化测试。
Selenium IDE code generated by recording a test for an application can be exported to other languages like Java, C#, and so on. The generated code can be imported into other IDEs like Eclipse, IntelliJ, and so on and be used for other automation tests.
Steps To Code Export
Step 1 - 按照 Selenium IDE Test Creation 中讨论的步骤 1 到 8 操作。
Step 1 − Follow the Steps 1 to 8 as discussed in Selenium IDE Test Creation.
Step 2 - 将项目保存在某个位置。
Step 2 − Save the project in a location.
Step 3 - 使用 Run current test 运行测试。
Step 3 − Run the test with the help Run current test.
Run current test - 执行完成后,消息 <TEST NAME> 完成成功出现在日志选项卡下。
Step 4 − As the execution completed, the message - <TEST NAME> completed successfully appeared under the Log tab.
Step 5 - 测试执行成功完成后,我们可以推断出测试工作正常,并且其代码可以重复使用。为了重复使用,我们需要导出为此测试生成的代码。
Step 5 − As test execution successfully completed, we can infer that the test is working fine, and its code can be reused. For this purpose of reuse, we would need to export the code generated for this test.
点击左上角测试用例名称旁边的三个点,如下图所示。
Click on the three dots appearing beside the test case name in the upper left corner as shown in the below image.
Step 6 - 从可见的下拉列表中选择导出选项。
Step 6 − Select the option Export from the dropdown visible.
Step 7 - 选择语言,然后点击导出按钮。
Step 7 − Select the language, and click on the Export button.
Step 8 - 指定 Selenium IDE 代码要导出的名称和位置。然后,点击保存按钮。
Step 8 − Specify the name, and the location where the Selenium IDE code would be exported. Then click on the Save button.
如果所选语言选择为 Java JUnit,则导出的版本应具有 .java 扩展名。
If the selected language is chosen as Java JUnit, the exported version should have the extension as .java.
Step 7 - 将此文件导入 IntelliJ 或 Eclipse,并添加必要的依赖项以支持从 Selenium IDE 生成的 Junit 测试。目前,Selenium IDE 代码可导出为以下语言和框架:-
Step 7 − Import this file to IntelliJ or Eclipse, and add the required dependencies to support the JUnit test generated from the Selenium IDE. Currently, Selenium IDE code can be exported to the below languages and frameworks −
-
C# Unit
-
Java JUnit
-
JavaScript Mocha
-
Python pytest
-
Ruby RSpec
Process Exported Code and Add Dependencies to Test
Step 1 - 导出的 Java Junit 代码适用于 Java 版本 8 及以上、JUnit 4.12,并支持最新的 Selenium 版本。
Step 1 − The Java JUnit code exported would work with Java version 8 and above, JUnit 4.12, and support the latest version of Selenium available.
Step 2 - 为了重用从 Selenium IDE 导出的代码,请从链接 Selenium Java 和 JUnit 中将 Selenium 和 Junit Maven 依赖项添加到 pom.xml 文件中。
Step 2 − In order to reuse the code exported from Selenium IDE, add the Selenium, and JUnit Maven dependencies to the pom.xml file from the links Selenium Java and JUnit.
请注意,我们已经添加了 Selenium 4.x 版本依赖项以使代码兼容。使用 IntelliJ 时,系统中的 Java 版本应为 17 或更高版本。然而,对于 Eclipse,Java 版本 8 或更高版本即可。
Please note, we have added the Selenium 4.x version dependencies to make the code compatible. With IntelliJ, the Java version in the system should be 17 or higher. However with Eclipse, Java version 8 or above would be fine.
Conclusion
本教程讲述Selenium IDE 代码导出的内容到此结束。我们从描述导出代码的步骤开始。这使您可以深入了解 Selenium IDE 中的代码导出。明智的做法是持续实践您所学内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽您的视野。
This concludes our comprehensive take on the tutorial on Selenium IDE Code Export. We’ve started with describing steps to code export. This equips you with in-depth knowledge of the Code Export in Selenium IDE. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.