Selenium – Use Assertions to Verify the Test Output
2014-09-24 10:16
495 查看
In
Software Testing we need to verify the output of test case against a predefined set of Test Data. Selenium provides various Selenese commands to achieve this. Let us try to explore some of the situations we come across in Test Automation.
Verify whether an object is visible (such as a button, link, etc)
Verify whether a checkbox is checked or unchecked.
Check
if an object is present on the screen
Check
if an Editbox is editable (i.e. we can key in values into edit box)
Check
if a particular value is selected in the dropdown box
Selenium provides several different APIs for automating the Assertions.
I have created a sample web site and uploaded to
Google docs. Download link
Download the sample web site and unzip into a folder. I will be using the same web site in my sample Selenium code.
Following are some of the Selenium assertions:
assertAlertPresent – checks whether an alert is present
assertAllButtons – compares the buttons present on the screen with the provided values in the test
assertAllLinks – compares all the links present on the screen with the one’s given in the test
assertChecked – verifies whether the particular checkbox is checked
assertEditable – verifies the Edit box whether it is possible to key in values into the edit box
assertSelectedValue – compares the given value with the selected value in the specified dropdown
Let us try to use these assertions in the Selenium Test by recording a test. After recording we will export them into JUnit format and
compare them to the recorded test in the table format in Selenium IDE.
In the above picture we can see the exact commands used in the Selenium IDE for assertions whereas in the below code which is exported in JUnit format from the Selenium IDE, only assertTrue and assertEquals are present. Here, other selenium methods such
as selenium.getSelectedLabel("city"), selenium.isTextPresent("Name of the Customer"), and selenium.getAlert() are used for assertions.
Use the following Selenium APIs for assertions:
selenium.isTextPresent() – To verify whether the specified text is present
selenium.getAlert() – To close the Javascript alert
selenium.getSelectedLabel() – To get the selected label (dropdown option) from the dropdown
selenium.chooseOkOnNextConfirmation() – To click the OK button on the Javascript alert
selenium.chooseCancelOnNextConfirmation() – To click the Cancel button on the Javascript alert
selenium.doubleClick(“Locator”) – double click an object
selenium.isChecked("locator") – Verify whether a checkbox is checked or not
selenium.isEditable("locator") – Verify whether an Editbox is editable or not
selenium.isElementPresent("locator") – This is a very much useful Selenium method. Whenever there is a need to wait for some time to load the
page then we can use this API in conjunction with Thread.sleep()
selenium.isVisible() – To verify whether the object is visible
There are some more Selenium APIs available but above are the one’s which are most regularly used.
In
Software Testing we need to verify the output of test case against a predefined set of Test Data. Selenium provides various Selenese commands to achieve this. Let us try to explore some of the situations we come across in Test Automation.
Verify whether an object is visible (such as a button, link, etc)
Verify whether a checkbox is checked or unchecked.
Check
if an object is present on the screen
Check
if an Editbox is editable (i.e. we can key in values into edit box)
Check
if a particular value is selected in the dropdown box
Selenium provides several different APIs for automating the Assertions.
I have created a sample web site and uploaded to
Google docs. Download link
Download the sample web site and unzip into a folder. I will be using the same web site in my sample Selenium code.
Following are some of the Selenium assertions:
assertAlertPresent – checks whether an alert is present
assertAllButtons – compares the buttons present on the screen with the provided values in the test
assertAllLinks – compares all the links present on the screen with the one’s given in the test
assertChecked – verifies whether the particular checkbox is checked
assertEditable – verifies the Edit box whether it is possible to key in values into the edit box
assertSelectedValue – compares the given value with the selected value in the specified dropdown
Let us try to use these assertions in the Selenium Test by recording a test. After recording we will export them into JUnit format and
compare them to the recorded test in the table format in Selenium IDE.
In the above picture we can see the exact commands used in the Selenium IDE for assertions whereas in the below code which is exported in JUnit format from the Selenium IDE, only assertTrue and assertEquals are present. Here, other selenium methods such
as selenium.getSelectedLabel("city"), selenium.isTextPresent("Name of the Customer"), and selenium.getAlert() are used for assertions.
package com.selftechy.assertions; /* * Author - Seetaram Hegde */ import com.thoughtworks.selenium.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.Assert; public class SeleniumAssertions extends SeleneseTestCase { @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.sqajobs.com/"); selenium.start(); selenium.windowMaximize(); } @Test public void testAssertions() throws Exception { //change the below directory path to the one where you have unzipped the sample website files selenium.open("file:///F:/Helios-Workspace/Sample%20Website/CreateAccount.htm"); selenium.type("FirstName", "seetaram"); selenium.type("Lname", "hegde"); selenium.type("street", "HSR Layout"); selenium.click("male"); selenium.click("kannada"); selenium.click("english"); selenium.click("hindi"); selenium.click("Save"); assertEquals("New Account Created", selenium.getAlert()); selenium.click("link=Click to View Customer Information"); selenium.waitForPageToLoad("30000"); Thread.sleep(200); assertTrue(selenium.isTextPresent("Below table contains the information about the Customer's transactions")); assertTrue(selenium.isTextPresent("Name of the Customer")); selenium.click("link=Go to Account Creation Screen"); selenium.waitForPageToLoad("30000"); Thread.sleep(200); assertEquals("Bangalore", selenium.getSelectedLabel("city")); } @After public void tearDown() throws Exception { //selenium.stop(); } }
Use the following Selenium APIs for assertions:
selenium.isTextPresent() – To verify whether the specified text is present
selenium.getAlert() – To close the Javascript alert
selenium.getSelectedLabel() – To get the selected label (dropdown option) from the dropdown
selenium.chooseOkOnNextConfirmation() – To click the OK button on the Javascript alert
selenium.chooseCancelOnNextConfirmation() – To click the Cancel button on the Javascript alert
selenium.doubleClick(“Locator”) – double click an object
selenium.isChecked("locator") – Verify whether a checkbox is checked or not
selenium.isEditable("locator") – Verify whether an Editbox is editable or not
selenium.isElementPresent("locator") – This is a very much useful Selenium method. Whenever there is a need to wait for some time to load the
page then we can use this API in conjunction with Thread.sleep()
selenium.isVisible() – To verify whether the object is visible
There are some more Selenium APIs available but above are the one’s which are most regularly used.
相关文章推荐
- verify the text views its easy to use getView from the adapter
- How to use the CONVERSION_EXIT_PARVW_INPUT and CONVERSION_EXIT_PARVW_OUTPUT
- [Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)
- The XML Litmus Test Understanding When and Why to Use XML
- How to Use the Visual Studio .NET Application Center Test
- How to let "test2.c" use the variable in "test1.c"
- use dynamic sql to output the result
- [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter
- Python use Selenium to control the webdriver
- How to use the Install from Media feature to promote Windows Server 2003-based domain controllers
- Use the Visual Studio 2005 Bootstrapper to Kick-Start Your Installation
- Use the GridView to insert a new record
- How to Use the Eclipse API
- [导入]Thow to use the AspnetUpload control 2.1
- use stack to initial a treeview with the data in a database
- Don’t Use the Win32 API PostThreadMessage() to Post Messages to UI Threads(翻译)
- Use the ItemDataBound event to customize the DataGrid control.
- Using RTest to Create Test Harness(use in Symbian OS)
- [导入]Thow to use the AspnetUpload control 2.1(2)
- Use orabm To test system CPU