Selenium WebDriver Scripting Guidelines

Learn Selenium scripting guidelines using real time examples, while implementing selenium Test scripts, Preparing Selenium framework using Java or any other language it is very important to follow some important selenium guidelines for scripting, follow below simple and important guidelines to prepare robust Selenium frameworks.

Selenium WebDriver Scripting Guidelines


1. Export Project as a JAR file


After creating Selenium Test Scripts for web application projects, just convert project into JAR file and it is easy to run without opening Eclipse IDE or any other tools, which you are using. Why JAR file means, in order to run your Selenium scripts across the different platforms such as windows, mac OS etc.

You can run Selenium project JAR file in command prompt as below

1.     Go to Project folder path where JAR file is exported
2.     Now Enter java -jar runnablejar.jar
3.     Hit Enter

It'll start executing your Test Scripts


2. Length of the Selenium Script:


Selenium Script should be identical, contains Test Cases, where it should not have any junk code and repeated lines of codes,by this you can know that your Test Script should have reusable code.You'can write Selenium script as per prepare test cases but should not contain any unusual code.

While preparing Selenium Scripts it is very important that your script should have dynamic test data not a static test data, You can use Test NG @Dataprovider annotation to read the test data from Xls file and use Apache POI library to read the test data  from xlsx files.


3. Finding UI Elements


As per Selenium Scripting Guidelines, We should not click directly on UI Elements because we should follow some steps before clicking on UI Elements, those are

1.     Wait for Element to Load
2.     Use condition to verify UI Element is enable or not using isEnable() method.
3.     Use Condition to Verify UI Elements are displaying or not using isDisplayed() method.
4.     Make sure UI Element is Clickable.
5.     Perform Click Action once Element is Clickable.

This is not at all Recommended


driver.findElement(By.xpath(//input[id='firstName']))

In above code we're directly clicking on UI Elements using Selenium test code but we're not providing any condition to verify whether Element is visible or not, Element is clickable or not. Just follow below sample code before identifying the Elements in UI.

Recommended Code:


WebDriverWait wait = new WebDriverWait(driver, 10);   
WebElement element = wait.until(ExpectedConditions.presenceOfElement(By.xpath("//input[id='firstName']"));
 (new Actions(driver)).moveToElement(element).perform();
wait.until(ExpectedConditions.elementToBeClickable(element);
element.click();

As per above code we're verifying the UI Element is present or not ,then we're performing Click Operation on UI Elements.

4. Main Method


It is always recommended to Run Selenium Test Scripts using main method, if you're using any Framework like TestNG, JUnit, Using simple code you can run your Selenium Scripts using TestNG OR JUnit Test cases under main method. So it is easy to convert your project into Runnable JAR file.

Example:

public static void main(String[] args) {                
                   TestListenerAdapter testla = new TestListenerAdapter();
                   TestNG testng = new TestNG();
                   testng.setTestClasses(new Class[]{SampleTests.class});
                   testng.run();
}

In Above main method I’m running TestNG @Test methods using simple Test script under main method, you people also can follow the same to run your Selenium Test Scripts.

By Above important Guidelines will make you from beginners to Expert level in Selenium Scripting, In case you like my Post, please share with your friends and provide your valuable comments on this Selenium Scripting Guidelines.

No comments:

Post a Comment