Guidelines for writing Selenium automation
Guidelines for writing Selenium automation

Automated testing is the best way to increase the effectiveness, efficiency, and coverage of your web application. Automation can be helpful not only to debug the code but also to find the root cause of the defect by identifying the code section that is error-prone. This can be carried out through tests with Selenium WebDriver for web applications and Appium for native and mobile web applications.

Selenium is a testing framework to perform web application testing across various platforms like Windows, Mac, and Linux. Selenium supports only web-based applications that need a browser open. To execute the script on different browsers, we need the driver of that browser.

It supports various programming languages such as Java, PHP, C#, Python, Groovy, Ruby, and Perl for writing the test automation scripts. It offers recording and playback action features to write tests.

Below are the basic requirements for running a Selenium Java script.

Importing Packages

Two important packages that a tester needs to import when starting to code a script are:

  • openqa.selenium.*- This package includes the WebDriver class needed to instantiate a new browser loaded with a specific driver.
  • openqa.selenium.firefox.FirefoxDriver – FirefoxDriver class instantiates a Firefox-specific driver.

As the project would grow, it is evident and logical that we may need to introduce several other packages for more complex and distinct functionalities like database connectivity, manipulating external files, logging, assertions, accessing another class, taking browser screenshots etc.

Instantiating objects and variables

For Selenium automation, the very first line of code comes as:

WebDriver driver = new FirefoxDriver();

To initiate a driver, we create a reference variable for the WebDriver interface and instantiate it using FirefoxDriver class. It means that we are defining a reference variable (driver) whose type is an interface. Any object we assign to it must be an instance of class FireFoxDriver or any other drivers that implement that interface.

A default Firefox profile will be launched which means that no extensions and plugins would be loaded with the Firefox instance and that it runs in the safe mode.

Selenium WebDriver makes direct calls to the browser using each browser’s native support. How these direct calls are made, and the features they support depends on the browser you are using. Selenium standalone server uses this driver to start the browser of your choice. Normally to run webdriver, we just need a browser and a selenium server jar file. Selenium supports to run webdriver on the browsers by adding a .exe path of the driver server for the individual browsers.

Here are the code snippets for Firefox, Chrome and IE:

Firefox

System.setProperty(“webdriver.firefox.bin”,”F:\\Program Files(x86)\\Firefox\\Firefox.exe”);

WebDriver driver = new FirefoxDriver();

driver.navigate().to(“https://www.google.com”);

Chrome

System.setProperty(“webdriver.chrome.driver”,”G:\\Lib\\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

driver.navigate().to(“https://www.google.com”);

Internet Explorer

System.setProperty(“webdriver.ie.driver”,”G:\\Lib\\internetexplorerdriver.exe”);

WebDriver driver = new InternetExplorerDriver();

driver.navigate().to(“https://www.google.com”);

Launching the Web browser

driver.get (baseURL);

To launch a fresh web browser instance the get() method is called on the WebDriver instance. The string character sequence passed as a parameter into the get() method redirects the launched web browser instance to the URL that you specify as its parameter.

Comparison between Expected and Actual Values:

Comparison code simply uses a basic if-else structure to compare the actual condition with the expected one.

if (actualCondition.contentEquals (expectedCondition)) {

System.out.printIn(“Success”);

} else {

System.out.printIn(“Fail”);

}

Terminating a Browser Session:

The “close()” method is used to close the browser window.

driver.close();

The scripts can be saved and re-run at any time. Hope this blog helps you in writing your first of many selenium test scripts.

Muriel Fernandes

Muriel Fernandes

Post Comments

* marked fields are mandatory.