Selenium 简明教程
Selenium IDE - Assertions Commands
Selenium IDE 中的所有命令都称为 Selenese。借助 Selenese 命令,可以在 Web 应用程序上执行所有操作。Selenese 命令有多种变体 −
All commands in Selenium IDE are called the Selenese. With the help of the Selenese commands, all actions can be performed on the web application. Selenese commands are of multiple variants −
-
Actions − The action commands are used to change the state of the application. For example, close, click, type, open, and so on.
-
Accessors − The accessor commands are used to gauge the state of the application and store it in variables. For example, store, store title, store text, store value, and so on.
-
Assertions − The assertion commands are used to verify the state of the application. It has sub-types like the assert, verify, and waitfor.
Basic Assertions Commands
下面列出各种类型的断言命令 −
The various types of assertions commands are listed below −
-
assert(variable, expected value) − This command is used to verify if the variable is matching the expected value. The variable is converted to string for comparison. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert alert(alert text) − This command is used to verify if an alert has the text matching the alert text. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert checked(locator) − This command is used to verify if the target element with the locator value is checked. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert confirmation(text) − This command is used to verify if a confirmation has been rendered. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert editable(locator) − This command is used to verify if the target element with the locator value is editable. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert element present(locator) − This command is used to verify if the target element with the locator value is available on the web page. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert element not present(locator) − This command is used to verify if the target element with the locator value is not available on the web page. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert not checked(locator) − This command is used to verify if the target element with the locator value is not checked. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert not editable(locator) − This command is used to verify if the target element with the locator value is not editable. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert not selected value(locator, text) − .This command is used to verify if the value attribute of the selected option of the target dropdown element does not have the given text. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert not text(locator, text) − This command is used to verify if the target element with the locator value does not have the given text.
-
assert prompt(text) − This command is used to verify if a prompt has been rendered. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert selected value(locator, text) − This command is used to verify if the value attribute of the selected option of the target dropdown element has the given text. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert selected label(locator, text) − This command is used to verify if the label of the selected option of the target dropdown element has the given text. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert text(locator, text) − This command is used to verify if the target element with the locator value has the given text. If assert did not pass, then the execution will stop at the point of mismatch.
-
assert value(locator, text) − This command is used to verify the value of an edit box. For a radio button/checkbox, the value will be set to on/off provided it is selected or not. If assert did not pass, then the execution will stop at the point of mismatch.
-
verify(variable, expected value) − This command is used to verify if the variable is matching the expected value. The variable is converted to string for comparison. It is a soft assert and execution will not stop in case of a mismatch.
-
verify checked(locator) − This command is used to verify if the target element with the locator value is checked. It is a soft assert and execution will not stop in case of a mismatch.
-
verify editable(locator) − This command is used to verify if the target element with the locator value is editable. It is a soft assert and execution will not stop in case of a mismatch.
-
verify element present(locator) − This command is used to verify if the target element with the locator value is available on the web page. It is a soft assert and execution will not stop in case of a mismatch.
-
verify element not present(locator) − This command is used to verify if the target element with the locator value is not available on the web page. It is a soft assert and execution will not stop in case of a mismatch.
-
verify not editable(locator) − This command is used to verify if the target element with the locator value is not editable. It is a soft assert and execution will not stop in case of a mismatch.
-
verify not selected value(locator, text) − This command is used to verify if the value attribute of the selected option of the target dropdown element does not have the given text. It is a soft assert and execution will not stop in case of a mismatch.
-
verify not text(locator, text) − This command is used to verify if the target element with the locator value does not have the given text. It is a soft assert and execution will not stop in case of a mismatch.
-
verify selected label(locator, text) − This command is used to verify if the label of the selected option of the target dropdown element has the given text. It is a soft assert and execution will not stop in case of a mismatch.
-
verify selected value(locator, text) − This command is used to verify if the value attribute of the selected option of the target dropdown element has the given text. It is a soft assert and execution will not stop in case of a mismatch.
-
verify text(locator, text) − This command is used to verify if the target element with the locator value has the given text. It is a soft assert and execution will not stop in case of a mismatch.
-
assert title(text) − This command is used to verify if the alert generated has the given alert text. If assert did not pass, then the execution will stop at the point of mismatch.
-
verify value(locator, text) − This command is used to verify the value of an edit box. For a radio button/checkbox, the value will be set to on/off provided it is selected or not. It is a soft assert and execution will not stop in case of a mismatch.
-
wait for element editable(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value to be editable.
-
wait for element not editable(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value not to be editable.
-
wait for element not present(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value to be unavailable on the web page.
-
wait for element not visible(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value to be invisible on the web page.
-
wait for element present(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value to be available on the web page.
-
wait for element visible(locator, wait time) − This command is used to wait for the given wait time(in milliseconds) for the target element with the locator value to be visible on the web page.
Example
让我们以下面页面的示例为例,其中我们将会单击 Created 链接。
Let us take an example of the below page, where we would click on the Created link.
我们将会验证其页面标题为 Selenium Practice - Links 。点击创建链接之后,我们还将验证文本 - Link has responded with status 201 and status text Created 。
We would verify its page title which is Selenium Practice - Links. After clicking the Created link, we would also verify the text - Link has responded with status 201 and status text Created.
要遵循的步骤如下所示 -
The steps to be followed are listed below −
Step 1 - 执行链接 * Selenium IDE Store Variables* 中描述的步骤 1 到 4
Step 1 − Perform the Steps 1 to 4 as described in the link Selenium IDE Store Variables
Step 2 - 在命令字段中输入 open,在目标字段中输入 * Selenium Automation Practice Form* 以启动应用程序。
Step 2 − Enter open in the Command field, and Selenium Automation Practice Form in the Target field, to launch the application.
Step 3 - 在命令字段中输入 verify title,在目标字段中输入 Selenium Practice - Links。
Step 3 − Enter verify title in the Command field and Selenium Practice - Links in the Target field.
Step 4 - 在命令字段中输入 click,xpath=// [@id="created"] in the Target field. Please note, xpath=// [@id="created"] 是链接的定位器值。
Step 4 − Enter click in the Command field, xpath=//[@id="created"] in the Target field. Please note, xpath=//[@id="created"] is the locator value of the link.
Step 5 - 在命令字段中输入 wait for element visible,在目标字段中输入 xpath=/html/body/main/div/div/div[2]/div[1],在值字段中输入 30000。请注意,xpath=/html/body/main/div/div/div[2]/div[1] 是要显示的文本的定位器值。
Step 5 − Enter wait for element visible in the Command field, xpath=/html/body/main/div/div/div[2]/div[1] in the Target field and 30000 in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/div[1] is the locator value of the text to be visible.
Step 6 - 在命令字段中输入 assert text,在目标字段中输入 xpath=/html/body/main/div/div/div[2]/div[1],在值字段中输入 Link has responded with status 201 and status text Created。请注意,xpath=/html/body/main/div/div/div[2]/div[1] 是文本的定位器值。
Step 6 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/div[1] in the Target field and Link has responded with status 201 and status text Created in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/div[1] is the locator value of the text.
Step 7 - 在命令字段中输入 close 以关闭浏览器。
Step 7 − Enter close in the Command field to close browser.
Step 8 - 单击顶部的“运行所有测试”,并等待测试执行结束。
Step 8 − Click on Run all tests from the top, and hold back for the test execution to end.
在上面的示例中,我们启动了应用程序并使用 assertions 命令 verify title 验证了其页面标题 Selenium Practice - Links 。然后单击了 Created 链接,并使用 assertions 命令 wait for element visible 等待文本 Link has responded with status 201 and status text Created 可见,并使用另一个 assertions 命令 assert text 验证了文本。
In the example above, we had launched the application and verified its page title Selenium Practice - Links using the assertions command verify title. Then clicked on the Created link, and waited for text Link has responded with status 201 and status text Created to be visible with the assertion command wait for element visible and verified the text with another assertion command assert text.
Conclusion
这结束了我们对 Selenium IDE 断言命令教程的全面介绍。我们从描述各种断言命令和示例开始,逐步了解如何将断言命令与 Selenium 一起使用。这使你具备了对 Selenium IDE 中的断言命令的深入了解。明智的做法是不断练习你学到的知识,并探索与 Selenium 相关的其他知识,以加深理解并拓宽视野。
This concludes our comprehensive take on the tutorial on Selenium IDE Assertion Commands. We’ve started with describing various assertions commands and an example to walk through how to use the assertions commands along with Selenium. This equips you with in-depth knowledge of the assertions commands 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.