Selenium 简明教程
Selenium IDE - Locating Strategies
Selenium IDE 具有多种定位策略,有助于自动化测试用例。然而,默认情况下,Selenium IDE 无法识别网页上的元素。要定位元素,我们需要 id、name、css 和 xpath 等定位器的帮助。
Selenium IDE has multiple locating strategies which help to automate test cases. However, Selenium IDE can not identify elements on a web page by default. In order to locate elements, we would need the help of the locators like id, name, css, and xpath.
除了上述定位器外,旧版本的 Selenium IDE 还支持其他定位器,如 DOM 和标识符,但是它们在最新版本中已被弃用。
In addition to the above locators, the older versions of Selenium IDE supported other locators like DOM, and identifier, however, they are deprecated in the latest versions.
Example
我们举个下面的页面的例子,我们将在 First Name: 标签旁边输入文本 Selenium 。这可以通过使用各种定位器来首先定位元素来实现。
Let us take an example of the below page, where we would enter the text Selenium beside the First Name: label. This can be achieved by first locating the elements using various locators.
Identify Web Elements
右键单击网页,然后在 Chrome 浏览器中单击“检查”按钮,在此操作后,网页的 HTML 代码将可访问。要检查页面上的一个元素,请单击如下突出显示的左上方箭头。
Right click on the webpage, and click on the Inspect button in the Chrome browser, post this action the HTML code for the web page will be accessible. For inspecting an element on a page, click on the left upward arrow as highlighted below.
一旦我们单击并将箭头指向输入框(在下图中突出显示),其 HTML 代码就出现了。
Once we had clicked and pointed the arrow to the input box(highlighted in the below image), its HTML code appeared.
Features to Locate Element in Selenium IDE
一旦在 Selenium IDE 中录制后创建一个测试,我们就可以通过单击它来获取该测试中任何特定步骤的详细信息。在目标字段中单击下拉菜单,Selenium IDE 在录制时会成功捕获一个元素的所有定位器,如 id、name、css、xpath 等。
Once a test would be created after recording in Selenium IDE, we would be able to get the details of any particular step in that test, by clicking on it. Click on the dropdown in the Target field, Selenium IDE while recording successfully captures all the locators like id, name, css, xpath, and so on for an element.
单击第 4 步,将 Command, Target 和 Value 字段的值分别设置为 type, id=name 和 Selenium 。该元素的名称、css 等其他定位器值在目标下拉菜单中可见。
Click on the 4th step, having the values of Command, Target, and Value fields as type, id=name, and Selenium respectively. The other locator values like the name, css, and so on for that element were visible within the Target dropdown.
我们在应用程序上执行的所有步骤都将记录在 Selenium IDE 中,在命令、目标和值字段下,右上角有一个 REC 按钮。
All the steps that we had performed on the application would be recorded in the Selenium IDE under the Command, Target, and Value fields with a REC button to the top right.
下面图片中突出显示的部分表示文本脚本编辑器窗格。它包含所有在启用录制时以测试步骤形式记录的用户交互。
The section highlighted below image denotes the Text Script Editor Pane. It has all the user interactions that had been recorded in the form of test steps when the recording was enabled.
要在 Selenium IDE 中默认识别一个 Web 元素,请单击下图中突出显示的 Select target in page 按钮。
To identify a web element by default in Selenium IDE, click on the Select target in page button as highlighted in the below image.
单击 Select target in page 按钮后,用户将在网页上获得 Select an element 按钮。一旦选择一个元素,它的定位器值就会被 Selenium IDE 填充。
After clicking the Select target in page button, the user would get the button Select an element on the web page. Once an element is selected, its locator value gets populated by the Selenium IDE.
所选 web 元素的相应定位器值在目标中生成。然后,如果单击 Find target in page 按钮,则定位器值已填入的元素将在页面上突出显示。
The corresponding locator value of the selected web element was generated in the Target. Then, if the Find target in page button is clicked, the element whose locator value is populated, gets highlighted on the page.
请注意,只有当命令字段中有有效值时,按钮 - Find target in page 和 Select target in page 才启用,否则它们将保持禁用状态,如下面图片中突出显示的那样。
Please note that, the buttons - Find target in page, and Select target in page, become enabled only if we have a valid value in the Command field else they remain disabled as highlighted in the below image.
Locator ID
在 Selenium IDE 中使用 id 定位器定位输入框,然后输入文本 Selenium 的步骤如下:
The steps to locate the input box using id locator and then enter the text Selenium in Selenium IDE are listed below −
Step 1 −执行 * Storing and Accessing Variables* 的第1步至第4步。
Step 1 − Perform the Steps 1 to 4 of Storing and Accessing Variables.
Step 2 −在Command字段中输入open,在Target字段中输入 * 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 −在Command字段中输入type,在Target字段中输入id=firstname,在Value字段中输入Selenium。请注意,id=firstname是id定位符及其值,作为输入框的firstname,而Selenium是要输入的值。
Step 3 − Enter type in the Command field, id=firstname in the Target field, and Selenium in the Value field. Please note, id=firstname is the id locator and its value as firstname for the input box and Selenium is the value to be entered.
Step 4 −在Command字段中输入close以终止浏览器。
Step 4 − Enter close in the Command field to terminate the browser.
Step 5 −单击“Run all tests”(运行全部测试)按钮。
Step 5 − Click on the Run all tests button.
在上面的示例中,我们在输入框中使用 First Name: 标签旁边的id定位符输入了文本 Selenium ,该标签出现在Log(日志)下方。此外,我们在顶部获得了一条绿色的勾选,表示测试已通过
In the example above, we entered the text Selenium in the input box using the id locator beside the First Name: label which appeared under the Log. Besides, we got a green tick at the top, signifying a passed test
Locator Name
以下列出了使用name定位符来定位输入框然后在Selenium IDE中输入文本Selenium的步骤:
The steps to locate the input box using name locator and then enter the text Selenium in Selenium IDE are listed below −
Step 1 −单击上面示例中创建的第二个步骤,在Command字段中输入type,在Target字段中输入name=firstname,在Value字段中输入Selenium。请注意,name=firstname是name定位符,其值为输入框的firstname,而Selenium是要输入的值。
Step 1 − Click on the second step created in the above example and enter type in the Command field, name=firstname in the Target field, and Selenium in the Value field. Please note, name=firstname is the name locator, and its value is firstname of the input box and Selenium is the value to be entered.
Step 2 −单击“Run all tests”(运行全部测试),并等待测试执行完成。
Step 2 − Click on Run all tests, and hold back for the test execution to finish.
在上面的示例中,我们在输入框中使用 First Name: 标签旁边的name定位符输入了文本 Selenium ,该标签出现在Log(日志)下方。
In the example above, we entered the text Selenium in the input box using the name locator beside the First Name: label which appeared under the Log.
Locator CSS
以下列出了使用css定位符来定位输入框然后在Selenium IDE中输入文本Selenium的步骤:
The steps to locate the input box using css locator and then enter the text Selenium in Selenium IDE are listed below −
Step 1 −单击上面示例中创建的第二个步骤,在Command字段中输入type,在Target字段中输入css=input#firstname,在Value字段中输入Selenium。请注意,css=input#firstname是css定位符,其值为输入框的input#firstname,而Selenium是要输入的值。
Step 1 − Click on the second step created in the above example and enter type in the Command field, css=input#firstname in the Target field, and Selenium in the Value field. Please note, css=input#firstname is the css locator and its value is input#firstname for the input box and Selenium is the value to be entered.
Step 2 −单击“Run all tests”(运行全部测试)。
Step 2 − Click on Run all tests.
在上面的示例中,我们在输入框中使用 First Name: 标签旁边的css定位符输入了文本 Selenium ,该标签出现在Log(日志)下方。
In the example above, we entered the text Selenium in the input box using the css locator beside the First Name: label which appeared under the Log.
Locator Xpath
以下列出了使用xpath定位符来定位输入框然后在Selenium IDE中输入文本Selenium的步骤:
The steps to locate the input box using xpath locator and then enter the text Selenium in Selenium IDE are listed below −
Step 1 −单击上面示例中创建的第二个步骤,在Command字段中输入type,在Target字段中输入xpath=//input[@id='firstname'],在Value字段中输入Selenium。请注意,xpath=//input[@id='firstname']是xpath定位符,其值为输入框的//input[@id='firstname'],而Selenium是要输入的值。
Step 1 − Click on the second step created in the above example and enter type in the Command field, xpath=//input[@id='firstname'] in the Target field, and Selenium in the Value field. Please note, xpath=//input[@id='firstname'] is the xpath locator and its value is //input[@id='firstname'] of the input box and Selenium is the value to be entered.
Step 2 −在顶部单击“Run all tests”(运行全部测试)。
Step 2 − Click on Run all tests at the top.
在上面的示例中,我们在输入框中使用 First Name: 标签旁边的xpath定位符输入了文本 Selenium ,该标签出现在Log(日志)下方。
In the example above, we entered the text Selenium in the input box using the xpath locator beside the First Name: label which appeared under the Log.
Conclusion
至此,我们对Selenium IDE定位策略教程的全面讲解结束。我们从描述一个示例开始,然后逐步介绍了不同的定位策略,并说明了如何将它们与Selenium一起使用。这将让你深入了解Selenium IDE中的定位策略。明智的做法是不断实践你学到的知识,并探索与Selenium相关的其他知识,以加深你的理解并拓展你的视野。
This concludes our comprehensive take on the tutorial on Selenium IDE Locating Strategies. We’ve started with describing an example and walked through the different locating strategies, and illustrated how to use them along with Selenium. This equips you with in-depth knowledge of the locating Strategies 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.