How do we write Selenium Test cases?

How do we write Selenium Test cases?, Using Web/HTML Element Locators, Selenium WebDriver methods, Java Programming, and TestNG Testing Framework.

Writing Test cases is an important activity or task in Automated Testing, Test engineers need to write instructions using test tool features and programming concepts. Test execution is a simple task in Automated testing, test tools can execute series of test cases with fewer human user interactions.

Writing Selenium Test Cases

Steps for writing Selenium WebDriver test cases (Selenium WebDriver, Java Programming, and TestNG Testing Framework):

1. Selecting Manual Test Cases

Generally, we don’t write test cases using requirements or user stories, we select Manual test cases and automate, and our Test lead selects manual test cases for automation with the contribution of team members.

No limitations to select Manual test cases for Automation, but generally, we select ‘Sanity’ Test cases, ‘Data driven’ test cases, and ‘Regression’ test cases, for automation.

2. Web or HTML Element Locators

First, Test Tool (Ex: Selenium) has to locate elements ( buttons, links, edit boxes, etc,) then it can perform the test operations, to locate web elements, Selenium uses HTML locators to locate elements in web pages.

Selenium supports eight types of locators to locate elements, we can use any one of those locators to locate an element uniquely.

Selenium supports:

1. id()

2. name()

3. className()

4. tagName()

5. linkText()

6. partialLinkText()

7. cssSelector()

8. xpath()

Note: We use a browser (any browser, Google Chrome or Mozilla Firefox, or MS Edge) to inspect Web or HTML elements.

3. Selenium WebDriver Methods

Selenium WebDriver methods are used to perform actions on web elements.

Example: 

Click a link,

Click a button,

Enter a value into an Edit box,

Select an item in the Dropdown box,

Select a Radio Button

Check/Uncheck a Checkbox,

etc,

Important Selenium WebDriver methods:

1. get(arg/url)
2. getTitle()
3. getCurrentUrl()
4. manage()
5. navigate()
.to(arg/url)
.back()
.forward()
.refresh()

6. close()
7. quit()

8. findElement()
9. findElements()
10. sendKeys()
11. click()
12. clear()
13. getAttribute()
14. getText()
15. isDisplayed()
16. isEnabled()
17. isSelected()

4. Programming (Java) Concepts

We use programming concepts to enhance the Selenium test cases,

1. Programming syntax for writing Selenium test steps

2. Programming comment syntax to write comments in Test cases

3. Programming Data types and Variables to handle different types of data in the Test cases

4. Programming Loop statements for Data-driven testing

5. Programming methods for creating reusable functions for Test cases.

6. Programming exception handling mechanism for handling run-time errors in Test cases.

Etc,

5 Testing Framework (TestNG) concepts

Testing Framework (Ex: TestNG) is used as ‘Test Runner’ for Selenium Test cases.

By integrating a Testing framework with Selenium we can,

1. Create Test batches

2. Prioritize Test cases

3. Group Test cases

4. Insert verification points in Test cases and generate Test Reports,

Etc,

Automated a Manual Test case using Selenium

a. Manual Test Case

Test case name: Verify Admin Login functionality in gcrShop application Admin interface

Test steps:

Test Steps:

1. Launch Browser
2. Navigate to “http://gcreddy.com/project/admin/login.php”
3. Enter Username
4. Enter Password
5. Click the “Login” Button
6. Capture the current URL/ return the displayed status of the “Logoff” link
7. Close the Browser window

Input/Test Data:
1. Username: gcreddy
2. Password: Temp@2020

Verification point:
Verify the existence of the “Logoff” link after clicking on the ‘Login’ button.

Expected: Availability of “Logoff” Link

Actual Result: *After Test Case Execution

Test Result: Pass/Fail (*After Test Case Execution)

Comments:

Elements involved in thiis functionality (navigation)

1. Username – Edit box – name() – “username”
2. Password – Edit box – name() – “password”
3. Login – Button – id() – “tdb1”
4. Logoff – Link linkText() – “Logoff”

Required Selenium WebDriver Methods for this functionality:

Navigate to gcrShop application admin interface home page – get(“URL”)
Enter a value into ‘Username’ Edit box – sendKeys(“value”)
Enter a value into ‘Password’ Edit box – sendKeys(“value”)
Click ‘Login’ Button – click()
Return a Link displayed status – isDisplayed()

Java Programming:

We use Java programming syntax to write the test case (ex: every test step should end with a semicolon and statements are case sensitive, etc,)

We use Java Data type and variable to return the ‘Logoff” link displayed status.

TestNG Testing Framework

We use the TestNG framework ‘Assert’ methods to insert verification point/s and generating test reports.

Selenium Test Case:

@Test
public void verifyAdminLogin() {
driver.get(“http://gcreddy.com/project/admin/login.php”);

driver.findElement(By.name(“username”)).sendKeys(“gcreddy”);
driver.findElement(By.name(“password”)).sendKeys(“Temp@2020”);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(3000);

boolean status = driver.findElement(By.linkText(“Logoff”)).click;

Assert.assertEquals(status , true);

driver.close();

How do we write Selenium Test cases?


Useful Links
1. Java Programming for Beginners
2. Selenium Tutorial for Beginners
3. Selenium Automation Framework
4. Writing Selenium Test cases
5. Selenium Live Project
Follow me on social media: