Selenium Webdriver ( Junit ) Tips and Tricks

I have started working with Selenium Web Driver to Automate testing for a web application that I have been working on. Selenium is a great tool to Automate testing for your software when there are many frequent updates going on in the site.

BxcvvQhIQAALq7m

Yes, this is what exactly happens when you are working on a buggy software, I highly recommend running Selenium Automation Test Cases after each and every patch you make, to make sure you have not introduced new bugs in the system.

Following are the tips and tricks you can use while automating the web application with Selenium Web Driver via Junit.

Livewire Component Library

1. Appending timeStamp to the unique form fields.

There are form fields that should be unique, you can make sure you entering the new data in the system every time when your automation test runs by appending the unix timestamp to the field. For example If I want to have unique First Name and Last Name

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.
String timestamp = String.valueOf(System.currentTimeMillis());
driver.findElement(By.name("subject_fname")).clear();
driver.findElement(By.name("subject_fname")).sendKeys("firstName"+timestamp);
driver.findElement(By.name("subject_lname")).clear();
driver.findElement(By.name("subject_lname")).sendKeys("lastName"+timestamp);

2. Selecting Date of Birth for particular Number of Years or Months Old

Selecting Date of Birth or and Date that is particular years or months back from the current date can be done in the following way

Calendar dateoFBirth = Calendar.getInstance();  //Instantiate calendar
dateoFBirth.add(Calendar.YEAR, -16);  //Going back in date for 16 years
new Select(driver.findElement(By.id("DOBmon"))).selectByVisibleText(new SimpleDateFormat("MMM").format(dateoFBirth.getTime()));
new Select(driver.findElement(By.id("DOBday"))).selectByVisibleText(new SimpleDateFormat("d").format(dateoFBirth.getTime()));
new Select(driver.findElement(By.id("DOByear"))).selectByVisibleText(new SimpleDateFormat("yyyy").format(dateoFBirth.getTime()));

Similarly you can go back in number of month from current date through

dateoFBirth.add(Calendar.MONTH, -10);  //Going back 10 monthss

3. Selecting random values from the Form Drop Down Field.

Selecting random values from Drop Down can make your automation test case more robust. This is how can you do it.

//Create a HashMap for all the values that are present in the Drop Down
public static final Map<String,String> genderMap = new HashMap<String, String>();
genderMap.put("s", "-- Select --");
genderMap.put("F", "Female");
genderMap.put("M", "Male");

//select random value from Drop Down
new Select(driver.findElement(By.name("subject_sex"))).selectByVisibleText(getRandomValueFromMap(Constant.genderMap));

public String getRandomValueFromMap(Map<String, String> mapObject){
		Object randomKey = mapObject.keySet().toArray()[new Random().nextInt(mapObject.keySet().toArray().length)];
		return (String) mapObject.get(randomKey);
		
	}

4. Assert if a Element contains some particular text

If you want to check if particular element contains some text you can use AssertTrue function from Junit along with the org.hamcrest For example

assertThat(driver.findElement(By.xpath("//span[@id='copytext']/table/tbody/tr[2]/td/p[2]")).getText(), containsString("foo"));

You’ll need to do the following static import in your page for assertThat and containsString to work

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;

Keep watching this space for more tips, I will keep this post updating.

Site Footer