Activity: Selenium

(no submission)

Purpose: Be familiar with test automation framework, practice Selenium, get ready to work on homework assignment.

Instruction

You may work alone or with another student in this course (max team size = 2).

Consider Compute GPA app. Use the following examples to help you find and extract information from the app and imitate users' interactions.

Develop at least 5 tests (i.e., 5 test methods) — be sure to use proper test oracles. Each test must target different purpose or verify different facts.

You may create a Selenium test file from scratch or use the provided computeGPATest_template.java.


(Some) examples to help you find HTML elements from a web page

The following examples assume that a driver is an instance of a WebDriver and an element is an instance of a WebElement.

private WebDriver driver;
private WebElement element;

To find an HTML element by a tag name

driver.findElement(By.tagName("name-of-the-html-tag"))        

   Note: findElement() returns the first matched element.
   To find all elements that match, use findElements(). 

To find all HTML elements with as specified tag name

driver.findElements("name-of-the-html-tag"))        

   Note: findElements() returns a list of all WebElements that match the condition,
   and thus we can iterate over the list to extract information about the elements.
   For example:        

   List<WebElement> lstElem = driver.findElements(By.tagName("input")); 
   for (int i=0; i&lr;lstElem.size(); i++)
   {
      System.out.println(i + " : " + lstElem.get(i).getAttribute("name") + " : " + 
             lstElem.get(i).getAttribute("type") + " : " + lstElem.get(i).getAttribute("value"));
   }  

To find an HTML element by a name attribute

driver.findElement(By.name("name-of-the-element"))

   Note: Each HTML element comes with a set of attributes. 
   Some attributes are applicable to certain kinds of HTML elements.   

To find an HTML element by an id attribute

driver.findElement(By.id("id-of-the-element"))

To find an HTML element using xpath (~regular expression)

// find an HTML element of type text (i.e., a textbox) 
driver.findElement(By.xpath("(//input[@type='text'])"))

// find the 2nd textbox of the page
driver.findElement(By.xpath("(//input[@type='text'])[2]"))
// an alternative way 
driver.findElement(By.xpath("(//input[@type='text'])[position()=2]"));  

// find the last textbox of the page
driver.findElement(By.xpath("(//input[@type='text'])[last()]"))

// find the one before last textbox of the page
driver.findElement(By.xpath("(//input[@type='text'])[last()-1]"))

// find an input element whose name starts with 'some-string'
driver.findElement(By.xpath("(//input[starts-with(@name, 'some-string')])"));     

// find an input element whose id starts with 'some-string'
driver.findElement(By.xpath("(//input[starts-with(@id, 'some-string')])"));     
 
// find an input element whose id = 'something'
driver.findElement(By.xpath("(//input[@id='something'])"));      

// find an element (without specifying the kind element) whose name = 'something'
driver.findElement(By.xpath("(//*[@name='something'])"));      

// find an element (without specifying the kind element) whose id = 'something'
driver.findElement(By.xpath("(//*[@id='something'])"));      
 
// find an input element whose name attribute contains 'some-string'
driver.findElement(By.xpath("//input[contains(@name, 'some-string')]"));     
 
// find an input element whose name attribute ='something1' and value attribute ='something2'
driver.findElement(By.xpath("//input[@name='something1' and @value='something2']"));     

// find an input element whose name attribute ='something1' or value attribute ='something2'
driver.findElement(By.xpath("//input[@name='something1' or @value='something2']"));     

// find all input elements of type radio (i.e, radio buttons)
driver.findElements(By.xpath("//input[@type='radio']"))

// find all elements (without specifying the kind element) of type radio (i.e, radio buttons)
driver.findElements(By.xpath("//*[@type='radio']")) 

// find all input elements of type checkbox 
driver.findElements(By.xpath("//input[@type='checkbox']"))

// find a button by text on the button
driver.findElement(By.xpath("//button[text()='text-on-the-button']"))

// find a button by partial text on the button
driver.findElement(By.xpath("//button[contains(text(), 'partial-text-on-the-button')]"))

// find a link by a link text
driver.findElement(By.linkText("link-text"))

// find a link by partial link text
driver.findElement(By.partialLinkText("partial-link-text"))  

(Some) examples to help you extract information

To find the page's title

driver.getTitle()

To retrieve the entire page page source

driver.getPageSource()

  Note: getPageSource() returns a string and 
  thus string's operations are applicable (e.g., contains("some string")) 

To get textual information of an element

element.getText() 

  Note: getText() returns a string and 
  thus string's operations are applicable (e.g., contains("some string")) 

To get a tag name of an element

element.getTagName()

To get a value of an attribute

element.getAttribute("name-of-attribute")

  Note: Some commonly used attributes are name, value, type. 

To get (x,y) coordinate of an element

element.getLocation()      
element.getLocation().getX()   
element.getLocation().getY() 

(Some) examples to imitate users' interactions

To enter input in a textbox

element.sendKeys("input-to-be-entered")

  Note: sendKeys() allows us to imitate the situation when 
  a user clicks in a textbox and enter an input. 
  Thus, we first need to find the target element before entering an input.  

To click a button, a radio button, a checkbox, or a link

element.click()


Some brief info that may help you familiarize yourself with basic web app concepts / syntax: HTML overview and intro to HTML and CSS

For a complete set of elements and attributes, please refer to W3C HTML.




Copyright © 2025 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2025-09-14 9:37